vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / commands / service_templates.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 ``service-templates`` sub-commands.
18 """
19
20 import os
21
22 from .. import csar
23 from .. import service_template_utils
24 from .. import table
25 from .. import utils
26 from ..core import aria
27 from ...core import Core
28 from ...storage import exceptions as storage_exceptions
29 from ...parser import consumption
30 from ...utils import (formatting, collections, console)
31 from ... orchestrator import topology
32
33 DESCRIPTION_FIELD_LENGTH_LIMIT = 20
34 SERVICE_TEMPLATE_COLUMNS = \
35     ('id', 'name', 'description', 'main_file_name', 'created_at', 'updated_at')
36
37
38 @aria.group(name='service-templates')
39 @aria.options.verbose()
40 def service_templates():
41     """
42     Manage service templates
43     """
44     pass
45
46
47 @service_templates.command(name='show',
48                            short_help='Show information for a stored service template')
49 @aria.argument('service-template-name')
50 @aria.options.verbose()
51 @aria.pass_model_storage
52 @aria.options.service_template_mode_full
53 @aria.options.mode_types
54 @aria.options.format_json
55 @aria.options.format_yaml
56 @aria.pass_logger
57 def show(service_template_name, model_storage, mode_full, mode_types, format_json, format_yaml,
58          logger):
59     """
60     Show information for a stored service template
61
62     SERVICE_TEMPLATE_NAME is the unique name of the stored service template.
63     """
64     service_template = model_storage.service_template.get_by_name(service_template_name)
65
66     if format_json or format_yaml:
67         mode_full = True
68
69     if mode_full:
70         consumption.ConsumptionContext()
71         if format_json:
72             console.puts(formatting.json_dumps(collections.prune(service_template.as_raw)))
73         elif format_yaml:
74             console.puts(formatting.yaml_dumps(collections.prune(service_template.as_raw)))
75         else:
76             console.puts(topology.Topology().dump(service_template))
77     elif mode_types:
78         console.puts(topology.Topology().dump_types(service_template=service_template))
79     else:
80         logger.info('Showing service template {0}...'.format(service_template_name))
81         service_template_dict = service_template.to_dict()
82         service_template_dict['#services'] = len(service_template.services)
83         columns = SERVICE_TEMPLATE_COLUMNS + ('#services',)
84         column_formatters = \
85             dict(description=table.trim_formatter_generator(DESCRIPTION_FIELD_LENGTH_LIMIT))
86         table.print_data(columns, service_template_dict, 'Service-template:',
87                          column_formatters=column_formatters, col_max_width=50)
88
89         if service_template_dict['description'] is not None:
90             logger.info('Description:')
91             logger.info('{0}{1}'.format(service_template_dict['description'].encode('UTF-8') or '',
92                                         os.linesep))
93
94         if service_template.services:
95             logger.info('Existing services:')
96             for service_name in service_template.services:
97                 logger.info('\t{0}'.format(service_name))
98
99
100 @service_templates.command(name='list',
101                            short_help='List all stored service templates')
102 @aria.options.sort_by()
103 @aria.options.descending
104 @aria.options.verbose()
105 @aria.pass_model_storage
106 @aria.pass_logger
107 def list(sort_by, descending, model_storage, logger):
108     """
109     List all stored service templates
110     """
111
112     logger.info('Listing all service templates...')
113     service_templates_list = model_storage.service_template.list(
114         sort=utils.storage_sort_param(sort_by, descending))
115
116     column_formatters = \
117         dict(description=table.trim_formatter_generator(DESCRIPTION_FIELD_LENGTH_LIMIT))
118     table.print_data(SERVICE_TEMPLATE_COLUMNS, service_templates_list, 'Service templates:',
119                      column_formatters=column_formatters)
120
121
122 @service_templates.command(name='store',
123                            short_help='Parse and store a service template archive')
124 @aria.argument('service-template-path')
125 @aria.argument('service-template-name')
126 @aria.options.service_template_filename
127 @aria.options.verbose()
128 @aria.pass_model_storage
129 @aria.pass_resource_storage
130 @aria.pass_plugin_manager
131 @aria.pass_logger
132 def store(service_template_path, service_template_name, service_template_filename,
133           model_storage, resource_storage, plugin_manager, logger):
134     """
135     Parse and store a service template archive
136
137     SERVICE_TEMPLATE_PATH is the path to the service template archive.
138
139     SERVICE_TEMPLATE_NAME is the unique name to give to the service template in storage.
140     """
141     logger.info('Storing service template {0}...'.format(service_template_name))
142
143     service_template_path = service_template_utils.get(service_template_path,
144                                                        service_template_filename)
145     core = Core(model_storage, resource_storage, plugin_manager)
146     try:
147         core.create_service_template(service_template_path,
148                                      os.path.dirname(service_template_path),
149                                      service_template_name)
150     except storage_exceptions.StorageError as e:
151         utils.check_overriding_storage_exceptions(e, 'service template', service_template_name)
152         raise
153     logger.info('Service template {0} stored'.format(service_template_name))
154
155
156 @service_templates.command(name='delete',
157                            short_help='Delete a stored service template')
158 @aria.argument('service-template-name')
159 @aria.options.verbose()
160 @aria.pass_model_storage
161 @aria.pass_resource_storage
162 @aria.pass_plugin_manager
163 @aria.pass_logger
164 def delete(service_template_name, model_storage, resource_storage, plugin_manager, logger):
165     """
166     Delete a stored service template
167
168     SERVICE_TEMPLATE_NAME is the unique name of the stored service template.
169     """
170     logger.info('Deleting service template {0}...'.format(service_template_name))
171     service_template = model_storage.service_template.get_by_name(service_template_name)
172     core = Core(model_storage, resource_storage, plugin_manager)
173     core.delete_service_template(service_template.id)
174     logger.info('Service template {0} deleted'.format(service_template_name))
175
176
177 @service_templates.command(name='inputs',
178                            short_help='Show stored service template inputs')
179 @aria.argument('service-template-name')
180 @aria.options.verbose()
181 @aria.pass_model_storage
182 @aria.pass_logger
183 def inputs(service_template_name, model_storage, logger):
184     """
185     Show stored service template inputs
186
187     SERVICE_TEMPLATE_NAME is the unique name of the stored service template.
188     """
189     logger.info('Showing inputs for service template {0}...'.format(service_template_name))
190     print_service_template_inputs(model_storage, service_template_name, logger)
191
192
193 @service_templates.command(name='validate',
194                            short_help='Validate a service template archive')
195 @aria.argument('service-template')
196 @aria.options.service_template_filename
197 @aria.options.verbose()
198 @aria.pass_model_storage
199 @aria.pass_resource_storage
200 @aria.pass_plugin_manager
201 @aria.pass_logger
202 def validate(service_template, service_template_filename,
203              model_storage, resource_storage, plugin_manager, logger):
204     """
205     Validate a service template archive
206
207     SERVICE_TEMPLATE_PATH is the path to the service template archive.
208     """
209     logger.info('Validating service template: {0}'.format(service_template))
210     service_template_path = service_template_utils.get(service_template, service_template_filename)
211     core = Core(model_storage, resource_storage, plugin_manager)
212     core.validate_service_template(service_template_path)
213     logger.info('Service template validated successfully')
214
215
216 @service_templates.command(name='create-archive',
217                            short_help='Create a CSAR archive from a service template source')
218 @aria.argument('service-template-path')
219 @aria.argument('destination')
220 @aria.options.verbose()
221 @aria.pass_logger
222 def create_archive(service_template_path, destination, logger):
223     """
224     Create a CSAR archive from a service template source
225
226     SERVICE_TEMPLATE_PATH is the path to the service template source.
227
228     DESTINATION is the path to the created CSAR archive.
229     """
230     logger.info('Creating a CSAR archive')
231     if not destination.endswith(csar.CSAR_FILE_EXTENSION):
232         destination += csar.CSAR_FILE_EXTENSION
233     csar.write(service_template_path, destination, logger)
234     logger.info('CSAR archive created at {0}'.format(destination))
235
236
237 def print_service_template_inputs(model_storage, service_template_name, logger):
238     service_template = model_storage.service_template.get_by_name(service_template_name)
239
240     logger.info('Service template inputs:')
241     if service_template.inputs:
242         logger.info(utils.get_parameter_templates_as_string(service_template.inputs))
243     else:
244         logger.info('\tNo inputs')