diff options
Diffstat (limited to 'azure/multicloud_azure')
-rw-r--r-- | azure/multicloud_azure/pub/aria/__init__.py | 11 | ||||
-rw-r--r-- | azure/multicloud_azure/pub/aria/service.py | 159 | ||||
-rw-r--r-- | azure/multicloud_azure/pub/aria/util.py | 40 | ||||
-rw-r--r-- | azure/multicloud_azure/settings.py | 3 | ||||
-rw-r--r-- | azure/multicloud_azure/swagger/urls.py | 14 | ||||
-rw-r--r-- | azure/multicloud_azure/swagger/views/infra_workload/__init__.py | 11 | ||||
-rw-r--r-- | azure/multicloud_azure/swagger/views/infra_workload/views.py | 82 | ||||
-rw-r--r-- | azure/multicloud_azure/tests/test_aria_view.py | 171 |
8 files changed, 489 insertions, 2 deletions
diff --git a/azure/multicloud_azure/pub/aria/__init__.py b/azure/multicloud_azure/pub/aria/__init__.py new file mode 100644 index 0000000..a952e9e --- /dev/null +++ b/azure/multicloud_azure/pub/aria/__init__.py @@ -0,0 +1,11 @@ +# Copyright (c) 2018 Amdocs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/azure/multicloud_azure/pub/aria/service.py b/azure/multicloud_azure/pub/aria/service.py new file mode 100644 index 0000000..637858a --- /dev/null +++ b/azure/multicloud_azure/pub/aria/service.py @@ -0,0 +1,159 @@ +# Copyright (c) 2018 Amdocs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import logging +import json +import tempfile +import time +import os + +from multicloud_azure.pub.aria import util +from aria.cli.core import aria +from aria.cli import utils +from aria.core import Core +from aria.cli import service_template_utils +from aria.storage import exceptions as storage_exceptions +from aria.utils import threading +from aria.orchestrator.workflow_runner import WorkflowRunner as Runner + +LOG = logging.getLogger(__name__) + +execution_state = util.SafeDict() + + +class AriaServiceImpl(object): + + def deploy_service(self, template_name, template_body, inputs, logger): + + service_template_name = template_name + "-template" + \ + time.strftime('%Y%m%d%H%M%S') + status = self.install_template_private(service_template_name, + template_body) + if (status[1] != 200): + logger.error("Error while installing the service-template") + return status[0], status[1] + else: + logger.info("service template {0} valiadated and stored".format( + service_template_name)) + status = self.create_service( + status, template_name + time.strftime('%Y%m%d%H%M%S'), inputs) + if (status[1] != 200): + return status[0], status[1] + execution_id = time.strftime('%Y%m%d%H%M%S') + thread = threading.ExceptionThread(target=self.start_execution, + args=(status[2].id, execution_id, + inputs, 'install')) + thread.start() + return execution_id, 200 + + @aria.pass_model_storage + @aria.pass_resource_storage + @aria.pass_plugin_manager + @aria.pass_logger + def install_template_private(self, service_template_name, template_body, + model_storage, + resource_storage, + plugin_manager, + logger): + service_template_filename = "MainServiceTemplate.yaml" + fileSp = template_body + f = tempfile.NamedTemporaryFile(suffix='.csar', + delete=False) + f.write(fileSp.read()) + f.seek(fileSp.tell(), 0) + service_template_path = f.name + fileSp.close() + file_path = service_template_utils.get( + service_template_path, service_template_filename) + + core = Core(model_storage, resource_storage, plugin_manager) + logger.info("service-template file {}".format(file_path)) + + try: + service_template_id = core.create_service_template( + file_path, + os.path.dirname(file_path), + service_template_name) + except storage_exceptions.StorageError as e: + logger.error("storage exception") + utils.check_overriding_storage_exceptions( + e, 'service template', service_template_name) + return e.message, 500 + except Exception as e: + logger.error("catchall exception") + return e.message, 500 + return "service template installed", 200, service_template_id + + @aria.pass_model_storage + @aria.pass_resource_storage + @aria.pass_plugin_manager + @aria.pass_logger + def create_service(self, template_id, service_name, input, + model_storage, + resource_storage, + plugin_manager, + logger): + """ + Creates a service from the specified service template + """ + input = input['sdnc_directives'] if'sdnc_directives'in input else None + core = Core(model_storage, resource_storage, plugin_manager) + service = core.create_service(template_id, input, service_name) + logger.info("service {} created".format(service.name)) + return "service {} created".format(service.name), 200, service + + @aria.pass_model_storage + @aria.pass_resource_storage + @aria.pass_plugin_manager + @aria.pass_logger + def start_execution(self, service_id, execution_id, input, workflow_name, + model_storage, + resource_storage, + plugin_manager, + logger): + """ + Start an execution for the specified service + """ + input = input['sdnc_directives'] if'sdnc_directives'in input else None + runner = Runner(model_storage, resource_storage, plugin_manager, + execution_id=execution_id, + service_id=service_id, + workflow_name=workflow_name, + inputs=input) + + service = model_storage.service.get(service_id) + tname = '{}_{}_{}'.format(service.name, workflow_name, + runner.execution_id) + thread = threading.ExceptionThread(target=runner.execute, + name=tname) + thread.start() + execution_state[str(runner.execution_id)] = [runner, thread] + logger.info("execution {} started".format(runner.execution_id)) + return json.dumps({"id": runner.execution_id}), 202 + + @aria.pass_model_storage + @aria.pass_logger + def show_execution(self, execution_id, model_storage, logger): + """ + Return details of specified execution/Stack + """ + try: + execution = model_storage.execution.get(execution_id) + except BaseException: + return "Execution {} not found".format(execution_id), 404 + logger.info("showing details of execution id {}".format(execution_id)) + return json.dumps({"execution_id": execution_id, + "service_name": execution.service_name, + "service_template_name": + execution.service_template_name, + "workflow_name": execution.workflow_name, + "status": execution.status}), 200 diff --git a/azure/multicloud_azure/pub/aria/util.py b/azure/multicloud_azure/pub/aria/util.py new file mode 100644 index 0000000..7dc415e --- /dev/null +++ b/azure/multicloud_azure/pub/aria/util.py @@ -0,0 +1,40 @@ +# Copyright (c) 2018 Amdocs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import threading + + +def make_template_name(user, template_name): + return "{}.{}".format(user, template_name) + + +class SafeDict(dict): + def __init__(self, *args): + self._lockobj = threading.Lock() + dict.__init__(self, args) + + def __getitem__(self, key): + try: + self._lockobj.acquire() + except Exception as ex: + raise ex + finally: + self._lockobj.release() + + def __setitem__(self, key, value): + try: + self._lockobj.acquire() + dict.__setitem__(self, key, value) + except Exception as ex: + raise ex + finally: + self._lockobj.release() diff --git a/azure/multicloud_azure/settings.py b/azure/multicloud_azure/settings.py index 5078754..4db77bc 100644 --- a/azure/multicloud_azure/settings.py +++ b/azure/multicloud_azure/settings.py @@ -14,6 +14,7 @@ import os import sys from logging import config from onaplogging import monkey +from aria import install_aria_extensions monkey.patch_all() # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -96,3 +97,5 @@ if 'test' in sys.argv: TEST_OUTPUT_VERBOSE = True TEST_OUTPUT_DESCRIPTIONS = True TEST_OUTPUT_DIR = 'test-reports' + +install_aria_extensions() diff --git a/azure/multicloud_azure/swagger/urls.py b/azure/multicloud_azure/swagger/urls.py index a3de04a..dde553a 100644 --- a/azure/multicloud_azure/swagger/urls.py +++ b/azure/multicloud_azure/swagger/urls.py @@ -1,5 +1,4 @@ # Copyright (c) 2018 Amdocs -# Copyright (c) 2018 Amdocs # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,13 +15,15 @@ from rest_framework.urlpatterns import format_suffix_patterns from multicloud_azure.swagger.views.swagger_json import SwaggerJsonView - # Registry from multicloud_azure.swagger.views.registry.views import Registry from multicloud_azure.swagger.views.registry.views import UnRegistry from multicloud_azure.swagger.views.registry.views import APIv1Registry from multicloud_azure.swagger.views.registry.views import APIv1UnRegistry +from multicloud_azure.swagger.views.infra_workload.views import InfraWorkload +from multicloud_azure.swagger.views.infra_workload.views import GetStackView + urlpatterns = [ # swagger url(r'^api/multicloud-azure/v0/swagger.json$', SwaggerJsonView.as_view()), @@ -42,6 +43,15 @@ urlpatterns = [ r'/(?P<cloud_region_id>[0-9a-zA-Z_-]+)$', APIv1UnRegistry.as_view()), + url(r'^api/multicloud-azure/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)' + r'/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/infra_workload$', + InfraWorkload.as_view()), + + url(r'^api/multicloud-azure/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/' + r'(?P<cloud_region_id>[0-9a-zA-Z_-]+)/infra_workload/' + r'(?P<workload_id>[0-9a-zA-Z\-\_]+)$', + GetStackView.as_view()), + ] urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/azure/multicloud_azure/swagger/views/infra_workload/__init__.py b/azure/multicloud_azure/swagger/views/infra_workload/__init__.py new file mode 100644 index 0000000..a952e9e --- /dev/null +++ b/azure/multicloud_azure/swagger/views/infra_workload/__init__.py @@ -0,0 +1,11 @@ +# Copyright (c) 2018 Amdocs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/azure/multicloud_azure/swagger/views/infra_workload/views.py b/azure/multicloud_azure/swagger/views/infra_workload/views.py new file mode 100644 index 0000000..c44eba2 --- /dev/null +++ b/azure/multicloud_azure/swagger/views/infra_workload/views.py @@ -0,0 +1,82 @@ +# Copyright (c) 2018 Amdocs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import logging +import json + +from rest_framework import status +from rest_framework.response import Response +from rest_framework.views import APIView + +from multicloud_azure.pub.aria.service import AriaServiceImpl + +logger = logging.getLogger(__name__) + + +class InfraWorkload(APIView): + + def post(self, request, cloud_owner, cloud_region_id): + data = request.data + template_data = data["infra-template"] + payload = data["infra-payload"] + inputs = json.loads(payload) + template_name = inputs['template_data']['stack_name'] + service_op = AriaServiceImpl() + try: + stack = service_op.deploy_service(template_name, template_data, + inputs, logger) + if stack[1] != 200: + return Response(data=stack[0], status=stack[1]) + except Exception as e: + + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + rsp = { + "template_type": "heat", + "workload_id": stack[0] + } + return Response(data=rsp, status=status.HTTP_202_ACCEPTED) + + +class GetStackView(APIView): + + def get(self, request, cloud_owner, cloud_region_id, workload_id): + service_op = AriaServiceImpl() + try: + stack = service_op.show_execution(workload_id) + if stack[1] != 200: + return Response(data=stack[0], status=stack[1]) + body = json.loads(stack[0]) + stack_status = body["status"] + response = "unknown" + if stack_status == "pending" or stack_status == "started": + response = "CREATE_IN_PROGRESS" + elif stack_status == "succeeded": + response = "CREATE_COMPLETE" + elif stack_status == "failed" or stack_status == "cancelled": + response = "CREATE_FAILED" + rsp = { + "template_type": "heat", + "workload_id": workload_id, + "workload_status": response + } + return Response(data=rsp, status=stack[1]) + except Exception as e: + + if hasattr(e, "http_status"): + return Response(data={'error': str(e)}, status=e.http_status) + else: + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/azure/multicloud_azure/tests/test_aria_view.py b/azure/multicloud_azure/tests/test_aria_view.py new file mode 100644 index 0000000..69c18e7 --- /dev/null +++ b/azure/multicloud_azure/tests/test_aria_view.py @@ -0,0 +1,171 @@ +# Copyright (c) 2018 Amdocs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import mock +import json +from rest_framework import status +from aria.cli.core import aria + +from multicloud_azure.swagger.views.infra_workload.views import InfraWorkload +from multicloud_azure.swagger.views.infra_workload.views import GetStackView +from multicloud_azure.pub.aria.service import AriaServiceImpl + + +class InfraViewTest(unittest.TestCase): + + def setUp(self): + self.fsv = InfraWorkload() + + def tearDown(self): + pass + + def test_service_get_fail(self): + req = mock.Mock() + dict = {'infra-template': 'aria', 'infra-payload': json.dumps( + {'name': 'abc', 'template_data': {'stack_name': 'stack'}})} + req.data = dict + resp = self.fsv.post(req, "abc", "def") + self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, + resp.status_code) + + +class StackViewTest(unittest.TestCase): + + def setUp(self): + self.fsv = GetStackView() + + def tearDown(self): + pass + + def test_service_get_fail(self): + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertNotEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, + self.fsv.get(req, "abc", "def", 123)) + + +class WorkoadViewTest(unittest.TestCase): + + def setUp(self): + self.fsv = AriaServiceImpl() + + def tearDown(self): + pass + + @mock.patch.object(AriaServiceImpl, 'deploy_service') + def test_deploy_service(self, mock_service_info): + + class Service: + def __init__(self, name, body, input, logger): + self.name = name + self.body = body + self.input = input + self.logger = logger + s = Service("abc", "def", "ghi", "OK") + mock_service_info.return_value = s + service_op = AriaServiceImpl() + self.assertNotEqual(200, service_op.deploy_service("a1", "b1", "c1", + "OK")) + + @mock.patch.object(AriaServiceImpl, 'install_template_private') + @aria.pass_model_storage + @aria.pass_resource_storage + @aria.pass_plugin_manager + @aria.pass_logger + def test_install_template(self, mock_template_info, model_storage, + resource_storage, plugin_manager, logger): + + class Workload: + def __init__(self, name, body): + self.name = name + self.body = body + service = Workload("a", "w1") + mock_template_info.return_value = service + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertNotEqual(200, + self.fsv.install_template_private(req, "a1", "b1", + model_storage, + resource_storage, + plugin_manager, + logger)) + + @mock.patch.object(AriaServiceImpl, 'create_service') + @aria.pass_model_storage + @aria.pass_resource_storage + @aria.pass_plugin_manager + @aria.pass_logger + def test_create_service(self, mock_template_info, model_storage, + resource_storage, plugin_manager, logger): + class Workload: + def __init__(self, id, name, input): + self.id = id + self.name = name + self.input = input + + f1 = Workload(1, "a", "w1") + f2 = Workload(2, "b", "w2") + service = [f1, f2] + mock_template_info.return_value = service + + class Request: + def __init__(self, query_params): + self.query_params = query_params + + req = Request({'k': 'v'}) + self.assertNotEqual(200, + self.fsv.create_service(req, 123, "a1", "b1", + model_storage, + resource_storage, + plugin_manager, + logger)) + + @mock.patch.object(AriaServiceImpl, 'start_execution') + @aria.pass_model_storage + @aria.pass_resource_storage + @aria.pass_plugin_manager + @aria.pass_logger + def test_start_execution(self, mock_template_info, model_storage, + resource_storage, plugin_manager, logger): + class Workload: + def __init__(self, status_id, execution_id, name, input): + self.status_id = status_id + self.execution_id = execution_id + self.input = input + self.name = name + + service = Workload(1, 2, "a", "w") + mock_template_info.return_value = service + + class Request: + def __init__(self, query_params): + self.query_params = query_params + + req = Request({'k': 'v'}) + self.assertNotEqual(200, + self.fsv.start_execution(req, 123, 456, "a1", "b1", + model_storage, + resource_storage, + plugin_manager, + logger)) + + def test_show_execution(self): + service_op = AriaServiceImpl() + self.assertNotEqual(200, + service_op.show_execution(123)) |