2c9961138182962023b5d3bb3d54f1cc99d3e8ca
[multicloud/azure.git] / azure / multicloud_azure / pub / aria / service.py
1 # Copyright (c) 2018 Amdocs
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import logging
14 import json
15 import tempfile
16 import time
17 import os
18
19 from multicloud_azure.pub.aria import util
20 from multicloud_azure.pub.utils.restcall import call_aria_rest
21 from aria.cli.core import aria
22 from aria.cli import utils
23 from aria.core import Core
24 from aria.cli import service_template_utils
25 from aria.storage import exceptions as storage_exceptions
26
27 LOG = logging.getLogger(__name__)
28
29 execution_state = util.SafeDict()
30
31
32 class AriaServiceImpl(object):
33
34     def deploy_service(self, template_name, template_body, inputs, logger):
35
36         service_template_name = template_name + "-template" + \
37                         time.strftime('%Y%m%d%H%M%S')
38         status = self.install_template_private(service_template_name,
39                                                template_body)
40         if (status[1] != 200):
41             logger.error("Error while installing the service-template")
42             return status[0], status[1]
43         else:
44             logger.info("service template {0} valiadated and stored".format(
45                 service_template_name))
46         status = self.create_service(
47             status, template_name + time.strftime('%Y%m%d%H%M%S'), inputs)
48         if (status[1] != 200):
49             return status[0], status[1]
50         status = call_aria_rest(status[2].id, 'install')
51         if (status[2] != "202"):
52             return status[1], status[2]
53         return json.loads(status[1])['id'], 200
54
55     @aria.pass_model_storage
56     @aria.pass_resource_storage
57     @aria.pass_plugin_manager
58     @aria.pass_logger
59     def install_template_private(self, service_template_name, template_body,
60                                  model_storage,
61                                  resource_storage,
62                                  plugin_manager,
63                                  logger):
64         service_template_filename = "MainServiceTemplate.yaml"
65         fileSp = template_body
66         f = tempfile.NamedTemporaryFile(suffix='.csar',
67                                         delete=False)
68         f.write(fileSp.read())
69         f.seek(fileSp.tell(), 0)
70         service_template_path = f.name
71         fileSp.close()
72         file_path = service_template_utils.get(
73             service_template_path, service_template_filename)
74
75         core = Core(model_storage, resource_storage, plugin_manager)
76         logger.info("service-template file {}".format(file_path))
77
78         try:
79             service_template_id = core.create_service_template(
80                 file_path,
81                 os.path.dirname(file_path),
82                 service_template_name)
83         except storage_exceptions.StorageError as e:
84             logger.error("storage exception")
85             utils.check_overriding_storage_exceptions(
86                 e, 'service template', service_template_name)
87             return e.message, 500
88         except Exception as e:
89             logger.error("catchall exception")
90             return e.message, 500
91         return "service template installed", 200, service_template_id
92
93     @aria.pass_model_storage
94     @aria.pass_resource_storage
95     @aria.pass_plugin_manager
96     @aria.pass_logger
97     def create_service(self, template_id, service_name, input,
98                        model_storage,
99                        resource_storage,
100                        plugin_manager,
101                        logger):
102         """
103         Creates a service from the specified service template
104         """
105         input = input['sdnc_directives'] if'sdnc_directives'in input else None
106         core = Core(model_storage, resource_storage, plugin_manager)
107         service = core.create_service(template_id, input, service_name)
108         logger.info("service {} created".format(service.name))
109         return "service {} created".format(service.name), 200, service
110
111     @aria.pass_model_storage
112     @aria.pass_logger
113     def show_execution(self, execution_id, model_storage, logger):
114         """
115         Return details of specified execution/Stack
116         """
117         try:
118             execution = model_storage.execution.get(execution_id)
119         except BaseException:
120             return "Execution {} not found".format(execution_id), 404
121         logger.info("showing details of execution id {}".format(execution_id))
122         return json.dumps({"execution_id": execution_id,
123                            "service_name": execution.service_name,
124                            "service_template_name":
125                                execution.service_template_name,
126                            "workflow_name": execution.workflow_name,
127                            "status": execution.status}), 200