Debian Codegraph @

heatclient/osc/v1/template.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12 #
13 # Copyright 2015 IBM Corp.
14
15 import logging
16
17 from cliff import columns
18 from osc_lib.command import command
19 from osc_lib import utils
20
21 from heatclient._i18n import _
22 from heatclient.common import format_utils
23 from heatclient.common import http
24 from heatclient.common import template_utils
25 from heatclient.common import utils as heat_utils
26 from heatclient import exc
27
28
29 class ListColumn(columns.FormattableColumn):
30 def human_readable(self):
31 return ','.join(self._value)
32
33
34 class VersionList(command.Lister):
35 """List the available template versions."""
36
37 log = logging.getLogger(__name__ + '.VersionList')
38
39 def take_action(self, parsed_args):
40 self.log.debug('take_action(%s)', parsed_args)
41
42 client = self.app.client_manager.orchestration
43
44 versions = client.template_versions.list()
45 try:
46 versions[1].aliases
47
48 fields = ['Version', 'Type', 'Aliases']
49 formatters = {'Aliases': ListColumn}
50 except AttributeError:
51 fields = ['Version', 'Type']
52 formatters = None
53
54 items = (utils.get_item_properties(s, fields,
55 formatters=formatters)
56 for s in versions)
57
58 return (fields, items)
59
60
61 class FunctionList(command.Lister):
62 """List the available functions."""
63
64 log = logging.getLogger(__name__ + '.FunctionList')
65
66 def get_parser(self, prog_name):
67 parser = super().get_parser(prog_name)
68 parser.add_argument(
69 'template_version',
70 metavar='<template-version>',
71 help=_('Template version to get the functions for')
72 )
73 parser.add_argument(
74 '--with_conditions',
75 default=False,
76 action='store_true',
77 help=_('Show condition functions for template.')
78 )
79
80 return parser
81
82 def take_action(self, parsed_args):
83 self.log.debug('take_action(%s)', parsed_args)
84
85 client = self.app.client_manager.orchestration
86
87 version = parsed_args.template_version
88 try:
89 functions = client.template_versions.get(
90 version, with_condition_func=parsed_args.with_conditions)
91 except exc.HTTPNotFound:
92 msg = _('Template version not found: %s') % version
93 raise exc.CommandError(msg)
94
95 fields = ['Functions', 'Description']
96 return (
97 fields,
98 (utils.get_item_properties(s, fields) for s in functions)
99 )
100
101
102 class Validate(format_utils.YamlFormat):
103 """Validate a template"""
104
105 log = logging.getLogger(__name__ + ".Validate")
106
107 def get_parser(self, prog_name):
108 parser = super().get_parser(prog_name)
109 parser.add_argument(
110 '-e', '--environment',
111 metavar='<environment>',
112 action='append',
113 help=_('Path to the environment. Can be specified multiple times')
114 )
115 parser.add_argument(
116 '--show-nested',
117 action='store_true',
118 help=_('Resolve parameters from nested templates as well')
119 )
120 parser.add_argument(
121 '--parameter',
122 metavar='<key=value>',
123 action='append',
124 help=_('Parameter values used to create the stack. This can be '
125 'specified multiple times')
126 )
127 parser.add_argument(
128 '-s', '--files-container',
129 metavar='<files-container>',
130 help=_('Swift files container name. Local files other than '
131 'root template would be ignored. If other files are not '
132 'found in swift, heat engine would raise an error.')
133 )
134 parser.add_argument(
135 '--ignore-errors',
136 metavar='<error1,error2,...>',
137 help=_('List of heat errors to ignore')
138 )
139 parser.add_argument(
140 '-t', '--template',
141 metavar='<template>',
142 required=True,
143 help=_('Path to the template')
144 )
145 return parser
146
147 def take_action(self, parsed_args):
148 self.log.debug("take_action(%s)", parsed_args)
149
150 heat_client = self.app.client_manager.orchestration
151 return _validate(heat_client, parsed_args)
152
153
154 def _validate(heat_client, args):
155 tpl_files, template = template_utils.process_template_path(
156 args.template,
157 object_request=http.authenticated_fetcher(heat_client),
158 fetch_child=args.files_container is None)
159
160 env_files_list = []
161 env_files, env = template_utils.process_multiple_environments_and_files(
162 env_paths=args.environment, env_list_tracker=env_files_list,
163 fetch_env_files=args.files_container is None)
164
165 fields = {
166 'template': template,
167 'parameters': heat_utils.format_parameters(args.parameter),
168 'files': dict(list(tpl_files.items()) + list(env_files.items())),
169 'environment': env,
170 }
171
172 if args.ignore_errors:
173 fields['ignore_errors'] = args.ignore_errors
174
175 # If one or more environments is found, pass the listing to the server
176 if env_files_list:
177 fields['environment_files'] = env_files_list
178
179 if args.show_nested:
180 fields['show_nested'] = args.show_nested
181
182 if args.files_container:
183 fields['files_container'] = args.files_container
184
185 validation = heat_client.stacks.validate(**fields)
186 data = list(validation.values())
187 columns = list(validation.keys())
188 return columns, data