Debian Codegraph @

mistralclient/commands/v2/workbooks.py

raw symbols SCIP
1 # Copyright 2014 - Mirantis, Inc.
2 # Copyright 2015 - StackStorm, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # 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 import argparse
17
18 from osc_lib.command import command
19
20 from mistralclient.commands.v2 import base
21 from mistralclient import utils
22
23
24 class WorkbookFormatter(base.MistralFormatter):
25 COLUMNS = [
26 ('name', 'Name'),
27 ('namespace', 'Namespace'),
28 ('tags', 'Tags'),
29 ('scope', 'Scope'),
30 ('created_at', 'Created at'),
31 ('updated_at', 'Updated at')
32 ]
33
34 @staticmethod
35 def format(workbook=None, lister=False):
36 if workbook:
37 data = (
38 workbook.name,
39 workbook.namespace,
40 base.wrap(', '.join(workbook.tags or '')) or '<none>',
41 workbook.scope,
42 workbook.created_at,
43 )
44
45 if hasattr(workbook, 'updated_at'):
46 data += (workbook.updated_at,)
47 else:
48 data += (None,)
49
50 else:
51 data = (tuple('' for _ in range(len(WorkbookFormatter.COLUMNS))),)
52
53 return WorkbookFormatter.headings(), data
54
55
56 class List(base.MistralLister):
57 """List all workbooks."""
58
59 def _get_format_function(self):
60 return WorkbookFormatter.format
61
62 def _get_resources(self, parsed_args):
63 mistral_client = self.app.client_manager.workflow_engine
64
65 return mistral_client.workbooks.list(
66 marker=parsed_args.marker,
67 limit=parsed_args.limit,
68 sort_keys=parsed_args.sort_keys,
69 sort_dirs=parsed_args.sort_dirs,
70 fields=WorkbookFormatter.fields(),
71 **base.get_filters(parsed_args)
72 )
73
74
75 class Get(command.ShowOne):
76 """Show specific workbook."""
77
78 def get_parser(self, prog_name):
79 parser = super(Get, self).get_parser(prog_name)
80
81 parser.add_argument('workbook', help='Workbook name')
82
83 parser.add_argument(
84 '--namespace',
85 nargs='?',
86 default='',
87 help="Namespace to get the workbook from."
88 )
89
90 return parser
91
92 def take_action(self, parsed_args):
93 mistral_client = self.app.client_manager.workflow_engine
94
95 workbook = mistral_client.workbooks.get(
96 parsed_args.workbook,
97 parsed_args.namespace
98 )
99
100 return WorkbookFormatter.format(workbook)
101
102
103 class Create(command.ShowOne):
104 """Create new workbook."""
105
106 def get_parser(self, prog_name):
107 parser = super(Create, self).get_parser(prog_name)
108
109 parser.add_argument(
110 'definition',
111 type=argparse.FileType('r'),
112 help='Workbook definition file'
113 )
114 parser.add_argument(
115 '--public',
116 action='store_true',
117 help='With this flag workbook will be marked as "public".'
118 )
119
120 parser.add_argument(
121 '--namespace',
122 nargs='?',
123 default='',
124 help="Namespace to create the workbook within."
125 )
126
127 return parser
128
129 def take_action(self, parsed_args):
130 scope = 'public' if parsed_args.public else 'private'
131
132 mistral_client = self.app.client_manager.workflow_engine
133
134 workbook = mistral_client.workbooks.create(
135 parsed_args.definition.read(),
136 namespace=parsed_args.namespace,
137 scope=scope
138 )
139
140 return WorkbookFormatter.format(workbook)
141
142
143 class Delete(command.Command):
144 """Delete workbook."""
145
146 def get_parser(self, prog_name):
147 parser = super(Delete, self).get_parser(prog_name)
148
149 parser.add_argument('workbook', nargs='+', help='Name of workbook(s).')
150 parser.add_argument(
151 '--namespace',
152 nargs='?',
153 default=None,
154 help="Namespace to delete the workbook(s) from."
155 )
156
157 return parser
158
159 def take_action(self, parsed_args):
160 mistral_client = self.app.client_manager.workflow_engine
161
162 utils.do_action_on_many(
163 lambda s:
164 mistral_client.workbooks.delete(s, parsed_args.namespace),
165 parsed_args.workbook,
166 "Request to delete workbook %s has been accepted.",
167 "Unable to delete the specified workbook(s)."
168 )
169
170
171 class Update(command.ShowOne):
172 """Update workbook."""
173
174 def get_parser(self, prog_name):
175 parser = super(Update, self).get_parser(prog_name)
176
177 parser.add_argument(
178 'definition',
179 type=argparse.FileType('r'),
180 help='Workbook definition file'
181 )
182 parser.add_argument(
183 '--namespace',
184 nargs='?',
185 default=None,
186 help="Namespace to update the workbook in."
187 )
188 parser.add_argument(
189 '--public',
190 action='store_true',
191 help='With this flag workbook will be marked as "public".'
192 )
193
194 return parser
195
196 def take_action(self, parsed_args):
197 scope = 'public' if parsed_args.public else 'private'
198
199 mistral_client = self.app.client_manager.workflow_engine
200
201 workbook = mistral_client.workbooks.update(
202 parsed_args.definition.read(),
203 namespace=parsed_args.namespace,
204 scope=scope
205 )
206
207 return WorkbookFormatter.format(workbook)
208
209
210 class GetDefinition(command.Command):
211 """Show workbook definition."""
212
213 def get_parser(self, prog_name):
214 parser = super(GetDefinition, self).get_parser(prog_name)
215
216 parser.add_argument('name', help='Workbook name')
217
218 return parser
219
220 def take_action(self, parsed_args):
221 mistral_client = self.app.client_manager.workflow_engine
222 definition = mistral_client.workbooks.get(parsed_args.name).definition
223
224 self.app.stdout.write(definition or "\n")
225
226
227 class Validate(command.ShowOne):
228 """Validate workbook."""
229 @staticmethod
230 def _format(result=None):
231 columns = ('Valid', 'Error')
232
233 if result:
234 data = (result.get('valid'), result.get('error'),)
235 else:
236 data = (tuple('<none>' for _ in range(len(columns))),)
237
238 return columns, data
239
240 def get_parser(self, prog_name):
241 parser = super(Validate, self).get_parser(prog_name)
242
243 parser.add_argument(
244 'definition',
245 type=argparse.FileType('r'),
246 help='Workbook definition file'
247 )
248
249 return parser
250
251 def take_action(self, parsed_args):
252 mistral_client = self.app.client_manager.workflow_engine
253
254 result = mistral_client.workbooks.validate(
255 parsed_args.definition.read()
256 )
257
258 return self._format(result)