vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / commands / services.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 CLI ``services`` sub-commands.
18 """
19
20 import os
21 from StringIO import StringIO
22
23 from . import service_templates
24 from .. import helptexts
25 from .. import table
26 from .. import utils
27 from ..core import aria
28 from ...core import Core
29 from ...modeling import exceptions as modeling_exceptions
30 from ...storage import exceptions as storage_exceptions
31 from ...parser import consumption
32 from ...utils import (formatting, collections, console)
33 from ...orchestrator import topology
34
35
36 DESCRIPTION_FIELD_LENGTH_LIMIT = 20
37 SERVICE_COLUMNS = ('id', 'name', 'description', 'service_template_name', 'created_at', 'updated_at')
38
39
40 @aria.group(name='services')
41 @aria.options.verbose()
42 def services():
43     """
44     Manage services
45     """
46     pass
47
48
49 @services.command(name='show',
50                   short_help='Show information for a service')
51 @aria.argument('service-name')
52 @aria.options.verbose()
53 @aria.options.service_mode_full
54 @aria.options.mode_graph
55 @aria.options.format_json
56 @aria.options.format_yaml
57 @aria.pass_model_storage
58 @aria.pass_logger
59 def show(service_name, model_storage, mode_full, mode_graph, format_json, format_yaml, logger):
60     """
61     Show information for a service
62
63     SERVICE_NAME is the unique name of the service.
64     """
65     service = model_storage.service.get_by_name(service_name)
66
67     if format_json or format_yaml:
68         mode_full = True
69
70     if mode_full:
71         consumption.ConsumptionContext()
72         if format_json:
73             console.puts(formatting.json_dumps(collections.prune(service.as_raw)))
74         elif format_yaml:
75             console.puts(formatting.yaml_dumps(collections.prune(service.as_raw)))
76         else:
77             console.puts(topology.Topology().dump(service))
78     elif mode_graph:
79         console.puts(topology.Topology().dump_graph(service))
80     else:
81         logger.info('Showing service {0}...'.format(service_name))
82         service_dict = service.to_dict()
83         columns = SERVICE_COLUMNS
84         column_formatters = \
85             dict(description=table.trim_formatter_generator(DESCRIPTION_FIELD_LENGTH_LIMIT))
86         table.print_data(columns, service_dict, 'Service:',
87                          column_formatters=column_formatters, col_max_width=50)
88
89         if service_dict['description'] is not None:
90             logger.info('Description:')
91             logger.info('{0}{1}'.format(service_dict['description'].encode('UTF-8') or '',
92                                         os.linesep))
93
94
95 @services.command(name='list', short_help='List services')
96 @aria.options.service_template_name()
97 @aria.options.sort_by()
98 @aria.options.descending
99 @aria.options.verbose()
100 @aria.pass_model_storage
101 @aria.pass_logger
102 def list(service_template_name,
103          sort_by,
104          descending,
105          model_storage,
106          logger):
107     """
108     List services
109
110     If `--service-template-name` is provided, list services based on that service template.
111     Otherwise, list all services.
112     """
113     if service_template_name:
114         logger.info('Listing services for service template {0}...'.format(
115             service_template_name))
116         service_template = model_storage.service_template.get_by_name(service_template_name)
117         filters = dict(service_template=service_template)
118     else:
119         logger.info('Listing all services...')
120         filters = {}
121
122     services_list = model_storage.service.list(
123         sort=utils.storage_sort_param(sort_by=sort_by, descending=descending),
124         filters=filters)
125     table.print_data(SERVICE_COLUMNS, services_list, 'Services:')
126
127
128 @services.command(name='create',
129                   short_help='Create a service')
130 @aria.argument('service-name', required=False)
131 @aria.options.service_template_name(required=True)
132 @aria.options.inputs(help=helptexts.SERVICE_INPUTS)
133 @aria.options.verbose()
134 @aria.pass_model_storage
135 @aria.pass_resource_storage
136 @aria.pass_plugin_manager
137 @aria.pass_logger
138 def create(service_template_name,
139            service_name,
140            inputs,  # pylint: disable=redefined-outer-name
141            model_storage,
142            resource_storage,
143            plugin_manager,
144            logger):
145     """
146     Create a service
147
148     SERVICE_NAME is the unique name to give to the service.
149     """
150     logger.info('Creating new service from service template {0}...'.format(
151         service_template_name))
152     core = Core(model_storage, resource_storage, plugin_manager)
153     service_template = model_storage.service_template.get_by_name(service_template_name)
154
155     try:
156         service = core.create_service(service_template.id, inputs, service_name)
157     except storage_exceptions.StorageError as e:
158         utils.check_overriding_storage_exceptions(e, 'service', service_name)
159         raise
160     except modeling_exceptions.ParameterException:
161         service_templates.print_service_template_inputs(model_storage, service_template_name,
162                                                         logger)
163         raise
164     logger.info("Service created. The service's name is {0}".format(service.name))
165
166
167 @services.command(name='delete',
168                   short_help='Delete a service')
169 @aria.argument('service-name')
170 @aria.options.force(help=helptexts.IGNORE_AVAILABLE_NODES)
171 @aria.options.verbose()
172 @aria.pass_model_storage
173 @aria.pass_resource_storage
174 @aria.pass_plugin_manager
175 @aria.pass_logger
176 def delete(service_name, force, model_storage, resource_storage, plugin_manager, logger):
177     """
178     Delete a service
179
180     SERVICE_NAME is the unique name of the service.
181     """
182     logger.info('Deleting service {0}...'.format(service_name))
183     service = model_storage.service.get_by_name(service_name)
184     core = Core(model_storage, resource_storage, plugin_manager)
185     core.delete_service(service.id, force=force)
186     logger.info('Service {0} deleted'.format(service_name))
187
188
189 @services.command(name='outputs',
190                   short_help='Show service outputs')
191 @aria.argument('service-name')
192 @aria.options.verbose()
193 @aria.pass_model_storage
194 @aria.pass_logger
195 def outputs(service_name, model_storage, logger):
196     """
197     Show service outputs
198
199     SERVICE_NAME is the unique name of the service.
200     """
201     logger.info('Showing outputs for service {0}...'.format(service_name))
202     service = model_storage.service.get_by_name(service_name)
203
204     if service.outputs:
205         outputs_string = StringIO()
206         for output_name, output in service.outputs.iteritems():
207             outputs_string.write(' - "{0}":{1}'.format(output_name, os.linesep))
208             outputs_string.write('     Description: {0}{1}'.format(output.description, os.linesep))
209             outputs_string.write('     Value: {0}{1}'.format(output.value, os.linesep))
210         logger.info(outputs_string.getvalue())
211     else:
212         logger.info('\tNo outputs')
213
214
215 @services.command(name='inputs',
216                   short_help='Show service inputs')
217 @aria.argument('service-name')
218 @aria.options.verbose()
219 @aria.pass_model_storage
220 @aria.pass_logger
221 def inputs(service_name, model_storage, logger):
222     """
223     Show service inputs
224
225     SERVICE_NAME is the unique name of the service.
226     """
227     logger.info('Showing inputs for service {0}...'.format(service_name))
228     service = model_storage.service.get_by_name(service_name)
229
230     if service.inputs:
231         inputs_string = StringIO()
232         for input_name, input_ in service.inputs.iteritems():
233             inputs_string.write(' - "{0}":{1}'.format(input_name, os.linesep))
234             inputs_string.write('     Description: {0}{1}'.format(input_.description, os.linesep))
235             inputs_string.write('     Value: {0}{1}'.format(input_.value, os.linesep))
236         logger.info(inputs_string.getvalue())
237     else:
238         logger.info('\tNo inputs')