From ad05cd6c21d73d23943ae24423bd038d3e2da88a Mon Sep 17 00:00:00 2001 From: Minho Kim Date: Mon, 21 Nov 2022 20:45:16 +0900 Subject: [PATCH] feat: add frame code for Function App cloud service (#32) Signed-off-by: Minho Kim --- pkg/pip_requirements.txt | 3 +- .../inventory/conf/cloud_service_conf.py | 1 + src/spaceone/inventory/connector/__init__.py | 1 + .../connector/function_app/__init__.py | 1 + .../connector/function_app/connector.py | 12 ++++++ src/spaceone/inventory/manager/__init__.py | 1 + .../manager/function_app/__init__.py | 0 .../manager/function_app/instance_manager.py | 40 ++++++++++++++++++ .../inventory/model/function_app/__init__.py | 0 .../model/function_app/cloud_service.py | 34 +++++++++++++++ .../model/function_app/cloud_service_type.py | 42 +++++++++++++++++++ .../inventory/model/function_app/data.py | 15 +++++++ test/api/test_cloud_service_api.py | 2 +- 13 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 src/spaceone/inventory/connector/function_app/__init__.py create mode 100644 src/spaceone/inventory/connector/function_app/connector.py create mode 100644 src/spaceone/inventory/manager/function_app/__init__.py create mode 100644 src/spaceone/inventory/manager/function_app/instance_manager.py create mode 100644 src/spaceone/inventory/model/function_app/__init__.py create mode 100644 src/spaceone/inventory/model/function_app/cloud_service.py create mode 100644 src/spaceone/inventory/model/function_app/cloud_service_type.py create mode 100644 src/spaceone/inventory/model/function_app/data.py diff --git a/pkg/pip_requirements.txt b/pkg/pip_requirements.txt index 54069794..480aa326 100644 --- a/pkg/pip_requirements.txt +++ b/pkg/pip_requirements.txt @@ -16,4 +16,5 @@ azure-keyvault-secrets azure-mgmt-rdbms azure-mgmt-cosmosdb azure-mgmt-containerinstance -azure-mgmt-webpubsub \ No newline at end of file +azure-mgmt-webpubsub +azure-mgmt-web \ No newline at end of file diff --git a/src/spaceone/inventory/conf/cloud_service_conf.py b/src/spaceone/inventory/conf/cloud_service_conf.py index 1a409bdd..541f0791 100644 --- a/src/spaceone/inventory/conf/cloud_service_conf.py +++ b/src/spaceone/inventory/conf/cloud_service_conf.py @@ -25,4 +25,5 @@ 'VirtualNetworks': 'VirtualNetworksManager', 'VMScaleSets': 'VmScaleSetsManager', 'WebPubSubService': 'WebPubSubServiceManager', + 'FunctionApp': 'FunctionAppManager' } diff --git a/src/spaceone/inventory/connector/__init__.py b/src/spaceone/inventory/connector/__init__.py index 72309635..66e761e9 100644 --- a/src/spaceone/inventory/connector/__init__.py +++ b/src/spaceone/inventory/connector/__init__.py @@ -19,3 +19,4 @@ from spaceone.inventory.connector.sql_databases import SQLDatabasesConnector from spaceone.inventory.connector.container_instances import ContainerInstancesConnector from spaceone.inventory.connector.web_pubsub_service import WebPubSubServiceConnector +from spaceone.inventory.connector.function_app import FunctionAppConnector \ No newline at end of file diff --git a/src/spaceone/inventory/connector/function_app/__init__.py b/src/spaceone/inventory/connector/function_app/__init__.py new file mode 100644 index 00000000..134b45a6 --- /dev/null +++ b/src/spaceone/inventory/connector/function_app/__init__.py @@ -0,0 +1 @@ +from spaceone.inventory.connector.function_app.connector import FunctionAppConnector \ No newline at end of file diff --git a/src/spaceone/inventory/connector/function_app/connector.py b/src/spaceone/inventory/connector/function_app/connector.py new file mode 100644 index 00000000..73323c33 --- /dev/null +++ b/src/spaceone/inventory/connector/function_app/connector.py @@ -0,0 +1,12 @@ +import logging +from spaceone.inventory.libs.connector import AzureConnector + +__all__ = ['FunctionAppConnector'] +_LOGGER = logging.getLogger(__name__) + + +class FunctionAppConnector(AzureConnector): + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.set_connect(kwargs.get('secret_data')) diff --git a/src/spaceone/inventory/manager/__init__.py b/src/spaceone/inventory/manager/__init__.py index 1ff8a3ed..335ece26 100644 --- a/src/spaceone/inventory/manager/__init__.py +++ b/src/spaceone/inventory/manager/__init__.py @@ -18,3 +18,4 @@ from spaceone.inventory.manager.virtual_machines.instnace_manger import VirtualMachinesManager from spaceone.inventory.manager.container_instances.container_manager import ContainerInstancesManager from spaceone.inventory.manager.web_pubsub_service.service_manager import WebPubSubServiceManager +from spaceone.inventory.manager.function_app.instance_manager import FunctionAppManager diff --git a/src/spaceone/inventory/manager/function_app/__init__.py b/src/spaceone/inventory/manager/function_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/spaceone/inventory/manager/function_app/instance_manager.py b/src/spaceone/inventory/manager/function_app/instance_manager.py new file mode 100644 index 00000000..6e7edaf3 --- /dev/null +++ b/src/spaceone/inventory/manager/function_app/instance_manager.py @@ -0,0 +1,40 @@ +import time +import logging +from spaceone.inventory.libs.manager import AzureManager +from spaceone.inventory.libs.schema.base import ReferenceModel +from spaceone.core.utils import * +from spaceone.inventory.model.web_pubsub_service.cloud_service_type import CLOUD_SERVICE_TYPES + +_LOGGER = logging.getLogger(__name__) + + +class FunctionAppManager(AzureManager): + connector_name = 'FunctionAppConnector' + cloud_service_types = CLOUD_SERVICE_TYPES + + def collect_cloud_service(self, params): + """ + Args: + params (dict): + - 'options' : 'dict' + - 'schema' : 'str' + - 'secret_data' : 'dict' + - 'filter' : 'dict' + - 'zones' : 'list' + - 'subscription_info' : 'dict' + + Response: + CloudServiceResponse (list) : list of azure function app data resource information + ErrorResourceResponse (list) : list of error resource information + + + """ + + _LOGGER.debug(f'** Function App START **') + start_time = time.time() + subscription_info = params['subscription_info'] + container_instances_responses = [] + error_responses = [] + + _LOGGER.debug(f'** Function App Service Finished {time.time() - start_time} Seconds **') + return container_instances_responses, error_responses \ No newline at end of file diff --git a/src/spaceone/inventory/model/function_app/__init__.py b/src/spaceone/inventory/model/function_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/spaceone/inventory/model/function_app/cloud_service.py b/src/spaceone/inventory/model/function_app/cloud_service.py new file mode 100644 index 00000000..4beb8e16 --- /dev/null +++ b/src/spaceone/inventory/model/function_app/cloud_service.py @@ -0,0 +1,34 @@ +from schematics.types import ModelType, StringType, PolyModelType, FloatType, DateTimeType +from spaceone.inventory.model.function_app.data import FunctionApp +from spaceone.inventory.libs.schema.metadata.dynamic_field import TextDyField, DateTimeDyField +from spaceone.inventory.libs.schema.metadata.dynamic_layout import ItemDynamicLayout +from spaceone.inventory.libs.schema.cloud_service import CloudServiceResource, CloudServiceResponse, CloudServiceMeta + +''' +Function App +''' +# TAB - Function App +function_app_info_meta = ItemDynamicLayout.set_fields('Web PubSub Service', fields=[ +]) + + +function_app_meta = CloudServiceMeta.set_layouts( + [function_app_info_meta]) + + +class ComputeResource(CloudServiceResource): + cloud_service_group = StringType(default='WebPubSubService') + + +class FunctionAppResource(ComputeResource): + cloud_service_type = StringType(default='ScaleSet') + data = ModelType(FunctionApp) + _metadata = ModelType(CloudServiceMeta, default=function_app_meta, serialized_name='metadata') + name = StringType() + account = StringType(serialize_when_none=False) + instance_type = StringType(serialize_when_none=False) + instance_size = FloatType(serialize_when_none=False) + + +class WebPubSubServiceResponse(CloudServiceResponse): + resource = PolyModelType(FunctionAppResource) \ No newline at end of file diff --git a/src/spaceone/inventory/model/function_app/cloud_service_type.py b/src/spaceone/inventory/model/function_app/cloud_service_type.py new file mode 100644 index 00000000..61e3aa5e --- /dev/null +++ b/src/spaceone/inventory/model/function_app/cloud_service_type.py @@ -0,0 +1,42 @@ +import os +from spaceone.inventory.libs.utils import * +from spaceone.inventory.libs.schema.metadata.dynamic_widget import CardWidget, ChartWidget +from spaceone.inventory.libs.schema.metadata.dynamic_field import TextDyField, SearchField, DateTimeDyField, ListDyField, \ + EnumDyField +from spaceone.inventory.libs.schema.cloud_service_type import CloudServiceTypeResource, CloudServiceTypeResponse, \ + CloudServiceTypeMeta + +current_dir = os.path.abspath(os.path.dirname(__file__)) + +function_app_by_account_conf = os.path.join(current_dir, 'widget/function_app_by_account.yaml') +function_app_by_region_conf = os.path.join(current_dir, 'widget/function_app_by_region.yaml') +function_app_by_resource_group_conf = os.path.join(current_dir, 'widget/function_app_by_resource_group.yaml') +function_app_total_count_count_conf = os.path.join(current_dir, 'widget/function_app_total_count.yaml') + +cst_function_app = CloudServiceTypeResource() +cst_function_app.name = 'Instance' +cst_function_app.group = 'FunctionApp' +cst_function_app.service_code = 'Microsoft.Web/sites' +cst_function_app.labels = ['Compute'] +cst_function_app.is_major = True +cst_function_app.is_primary = True +cst_function_app.tags = { + 'spaceone:icon': '', +} + +cst_function_app._metadata = CloudServiceTypeMeta.set_meta( + fields=[ + ], + search=[ + + ], + widget=[ + + ] + +) + + +CLOUD_SERVICE_TYPES = [ + CloudServiceTypeResponse({'resource': cst_function_app}), +] \ No newline at end of file diff --git a/src/spaceone/inventory/model/function_app/data.py b/src/spaceone/inventory/model/function_app/data.py new file mode 100644 index 00000000..1cf83229 --- /dev/null +++ b/src/spaceone/inventory/model/function_app/data.py @@ -0,0 +1,15 @@ +from schematics import Model +from schematics.types import StringType +from spaceone.inventory.libs.schema.resource import AzureCloudService + + +class FunctionApp(AzureCloudService): + id = StringType(serialize_when_none=False) + name = StringType(serialize_when_none=False) + location = StringType(serialize_when_none=False) + + def reference(self): + return { + "resource_id": self.id, + "external_link": f"https://portal.azure.com/#@.onmicrosoft.com/resource{self.id}/overview", + } \ No newline at end of file diff --git a/test/api/test_cloud_service_api.py b/test/api/test_cloud_service_api.py index 8d8cb183..fbd498a8 100644 --- a/test/api/test_cloud_service_api.py +++ b/test/api/test_cloud_service_api.py @@ -33,7 +33,7 @@ def test_verify(self): def test_collect(self): - options = {'cloud_service_types': ['WebPubSubService']} + options = {'cloud_service_types': ['FunctionApp']} # options = {} filter = {} resource_stream = self.inventory.Collector.collect({'options': options, 'secret_data': self.azure_credentials,