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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 CLI ``service-templates`` sub-commands.
23 from .. import service_template_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
33 DESCRIPTION_FIELD_LENGTH_LIMIT = 20
34 SERVICE_TEMPLATE_COLUMNS = \
35 ('id', 'name', 'description', 'main_file_name', 'created_at', 'updated_at')
38 @aria.group(name='service-templates')
39 @aria.options.verbose()
40 def service_templates():
42 Manage service templates
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
57 def show(service_template_name, model_storage, mode_full, mode_types, format_json, format_yaml,
60 Show information for a stored service template
62 SERVICE_TEMPLATE_NAME is the unique name of the stored service template.
64 service_template = model_storage.service_template.get_by_name(service_template_name)
66 if format_json or format_yaml:
70 consumption.ConsumptionContext()
72 console.puts(formatting.json_dumps(collections.prune(service_template.as_raw)))
74 console.puts(formatting.yaml_dumps(collections.prune(service_template.as_raw)))
76 console.puts(topology.Topology().dump(service_template))
78 console.puts(topology.Topology().dump_types(service_template=service_template))
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',)
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)
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 '',
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))
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
107 def list(sort_by, descending, model_storage, logger):
109 List all stored service templates
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))
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)
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
132 def store(service_template_path, service_template_name, service_template_filename,
133 model_storage, resource_storage, plugin_manager, logger):
135 Parse and store a service template archive
137 SERVICE_TEMPLATE_PATH is the path to the service template archive.
139 SERVICE_TEMPLATE_NAME is the unique name to give to the service template in storage.
141 logger.info('Storing service template {0}...'.format(service_template_name))
143 service_template_path = service_template_utils.get(service_template_path,
144 service_template_filename)
145 core = Core(model_storage, resource_storage, plugin_manager)
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)
153 logger.info('Service template {0} stored'.format(service_template_name))
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
164 def delete(service_template_name, model_storage, resource_storage, plugin_manager, logger):
166 Delete a stored service template
168 SERVICE_TEMPLATE_NAME is the unique name of the stored service template.
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))
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
183 def inputs(service_template_name, model_storage, logger):
185 Show stored service template inputs
187 SERVICE_TEMPLATE_NAME is the unique name of the stored service template.
189 logger.info('Showing inputs for service template {0}...'.format(service_template_name))
190 print_service_template_inputs(model_storage, service_template_name, logger)
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
202 def validate(service_template, service_template_filename,
203 model_storage, resource_storage, plugin_manager, logger):
205 Validate a service template archive
207 SERVICE_TEMPLATE_PATH is the path to the service template archive.
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')
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()
222 def create_archive(service_template_path, destination, logger):
224 Create a CSAR archive from a service template source
226 SERVICE_TEMPLATE_PATH is the path to the service template source.
228 DESTINATION is the path to the created CSAR archive.
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))
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)
240 logger.info('Service template inputs:')
241 if service_template.inputs:
242 logger.info(utils.get_parameter_templates_as_string(service_template.inputs))
244 logger.info('\tNo inputs')