Debian Codegraph @

mistralclient/commands/v2/environments.py

raw symbols SCIP
1 # Copyright 2015 - StackStorm, Inc.
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 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import argparse
16
17 from oslo_serialization import jsonutils
18
19 from osc_lib.command import command
20
21 from mistralclient.commands.v2 import base
22 from mistralclient import utils
23
24
25 class EnvironmentFormatter(base.MistralFormatter):
26 COLUMNS = [
27 ('name', 'Name'),
28 ('description', 'Description'),
29 ('variables', 'Variables'),
30 ('scope', 'Scope'),
31 ('created_at', 'Created at'),
32 ('updated_at', 'Updated at'),
33 ]
34 LIST_COLUMN_FIELD_NAMES = [c[0] for c in COLUMNS if c[0] != 'variables']
35 LIST_COLUMN_HEADING_NAMES = [c[1] for c in COLUMNS if c[0] != 'variables']
36
37 @staticmethod
38 def format(environment=None, lister=False):
39 if lister:
40 columns = EnvironmentFormatter.LIST_COLUMN_HEADING_NAMES
41 else:
42 columns = EnvironmentFormatter.headings()
43
44 if environment:
45 data = (
46 environment.name,)
47 if hasattr(environment, 'description'):
48 data += (environment.description or '<none>',)
49 else:
50 data += (None,)
51 if not lister:
52 data += (jsonutils.dumps(environment.variables, indent=4),)
53 data += (
54 environment.scope,
55 environment.created_at,)
56 if hasattr(environment, 'updated_at'):
57 data += (environment.updated_at or '<none>',)
58 else:
59 data += (None,)
60 else:
61 data = (tuple('' for _ in range(len(columns))),)
62
63 return columns, data
64
65
66 class List(base.MistralLister):
67 """List all environments."""
68
69 def _get_format_function(self):
70 return EnvironmentFormatter.format_list
71
72 def _get_resources(self, parsed_args):
73 mistral_client = self.app.client_manager.workflow_engine
74 return mistral_client.environments.list(
75 marker=parsed_args.marker,
76 limit=parsed_args.limit,
77 sort_keys=parsed_args.sort_keys,
78 sort_dirs=parsed_args.sort_dirs,
79 fields=EnvironmentFormatter.fields(),
80 **base.get_filters(parsed_args)
81 )
82
83
84 class Get(command.ShowOne):
85 """Show specific environment."""
86
87 def get_parser(self, prog_name):
88 parser = super(Get, self).get_parser(prog_name)
89
90 parser.add_argument('environment', help='Environment name')
91
92 parser.add_argument(
93 '--export',
94 default=False,
95 action='store_true',
96 help='Export the environment suitable for import'
97 )
98
99 return parser
100
101 def take_action(self, parsed_args):
102 mistral_client = self.app.client_manager.workflow_engine
103 environment = mistral_client.environments.get(parsed_args.environment)
104
105 if parsed_args.export:
106 columns = ('name',
107 'description',
108 'scope',
109 'variables')
110
111 data = (environment.name,
112 environment.description,
113 environment.scope,
114 jsonutils.dumps(environment.variables))
115
116 return columns, data
117
118 return EnvironmentFormatter.format(environment)
119
120
121 class Create(command.ShowOne):
122 """Create new environment."""
123
124 def get_parser(self, prog_name):
125 parser = super(Create, self).get_parser(prog_name)
126
127 parser.add_argument(
128 'file',
129 type=argparse.FileType('r'),
130 help='Environment configuration file in JSON or YAML'
131 )
132
133 return parser
134
135 def take_action(self, parsed_args):
136 data = utils.load_content(parsed_args.file.read())
137
138 mistral_client = self.app.client_manager.workflow_engine
139 environment = mistral_client.environments.create(**data)
140
141 return EnvironmentFormatter.format(environment)
142
143
144 class Delete(command.Command):
145 """Delete environment."""
146
147 def get_parser(self, prog_name):
148 parser = super(Delete, self).get_parser(prog_name)
149
150 parser.add_argument(
151 'environment',
152 nargs='+',
153 help='Name of environment(s).'
154 )
155
156 return parser
157
158 def take_action(self, parsed_args):
159 mistral_client = self.app.client_manager.workflow_engine
160
161 utils.do_action_on_many(
162 lambda s: mistral_client.environments.delete(s),
163 parsed_args.environment,
164 "Request to delete environment %s has been accepted.",
165 "Unable to delete the specified environment(s)."
166 )
167
168
169 class Update(command.ShowOne):
170 """Update environment."""
171
172 def get_parser(self, prog_name):
173 parser = super(Update, self).get_parser(prog_name)
174
175 parser.add_argument(
176 'file',
177 type=argparse.FileType('r'),
178 help='Environment configuration file in JSON or YAML'
179 )
180
181 return parser
182
183 def take_action(self, parsed_args):
184 data = utils.load_content(parsed_args.file.read())
185
186 mistral_client = self.app.client_manager.workflow_engine
187 environment = mistral_client.environments.update(**data)
188
189 return EnvironmentFormatter.format(environment)