| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import argparse |
| 19 |
|
| 20 |
from osc_lib.command import command |
| 21 |
|
| 22 |
from mistralclient.commands.v2 import base |
| 23 |
from mistralclient import utils |
| 24 |
|
| 25 |
|
| 26 |
class ActionFormatter(base.MistralFormatter): |
| 27 |
COLUMNS = [ |
| 28 |
('id', 'ID'), |
| 29 |
('name', 'Name'), |
| 30 |
('is_system', 'Is system'), |
| 31 |
('input', 'Input'), |
| 32 |
('description', 'Description'), |
| 33 |
('tags', 'Tags'), |
| 34 |
('created_at', 'Created at'), |
| 35 |
('updated_at', 'Updated at'), |
| 36 |
] |
| 37 |
|
| 38 |
@staticmethod |
| 39 |
def format(action=None, lister=False): |
| 40 |
if action: |
| 41 |
tags = getattr(action, 'tags', None) or [] |
| 42 |
input_ = action.input if not lister else base.cut(action.input) |
| 43 |
desc = (action.description if not lister |
| 44 |
else base.cut(action.description)) |
| 45 |
|
| 46 |
data = ( |
| 47 |
action.id, |
| 48 |
action.name, |
| 49 |
action.is_system, |
| 50 |
input_, |
| 51 |
desc, |
| 52 |
base.wrap(', '.join(tags)) or '<none>', |
| 53 |
action.created_at, |
| 54 |
) |
| 55 |
if hasattr(action, 'updated_at'): |
| 56 |
data += (action.updated_at,) |
| 57 |
else: |
| 58 |
data += (None,) |
| 59 |
|
| 60 |
else: |
| 61 |
data = (tuple('' for _ in range(len(ActionFormatter.COLUMNS))),) |
| 62 |
|
| 63 |
return ActionFormatter.headings(), data |
| 64 |
|
| 65 |
|
| 66 |
class List(base.MistralLister): |
| 67 |
"""List all actions.""" |
| 68 |
|
| 69 |
def _get_format_function(self): |
| 70 |
return ActionFormatter.format_list |
| 71 |
|
| 72 |
def get_parser(self, prog_name): |
| 73 |
parser = super(List, self).get_parser(prog_name) |
| 74 |
|
| 75 |
parser.add_argument( |
| 76 |
'--namespace', |
| 77 |
nargs='?', |
| 78 |
default='', |
| 79 |
help="Namespace of the actions.", |
| 80 |
) |
| 81 |
|
| 82 |
return parser |
| 83 |
|
| 84 |
def _get_resources(self, parsed_args): |
| 85 |
mistral_client = self.app.client_manager.workflow_engine |
| 86 |
|
| 87 |
return mistral_client.actions.list( |
| 88 |
marker=parsed_args.marker, |
| 89 |
limit=parsed_args.limit, |
| 90 |
sort_keys=parsed_args.sort_keys, |
| 91 |
sort_dirs=parsed_args.sort_dirs, |
| 92 |
fields=ActionFormatter.fields(), |
| 93 |
namespace=parsed_args.namespace, |
| 94 |
**base.get_filters(parsed_args) |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
class Get(command.ShowOne): |
| 99 |
"""Show specific action.""" |
| 100 |
|
| 101 |
def get_parser(self, prog_name): |
| 102 |
parser = super(Get, self).get_parser(prog_name) |
| 103 |
|
| 104 |
parser.add_argument('action', help='Action (name or ID)') |
| 105 |
parser.add_argument( |
| 106 |
'--namespace', |
| 107 |
nargs='?', |
| 108 |
default='', |
| 109 |
help="Namespace to create the action within.", |
| 110 |
) |
| 111 |
|
| 112 |
return parser |
| 113 |
|
| 114 |
def take_action(self, parsed_args): |
| 115 |
mistral_client = self.app.client_manager.workflow_engine |
| 116 |
|
| 117 |
action = mistral_client.actions.get( |
| 118 |
parsed_args.action, |
| 119 |
parsed_args.namespace |
| 120 |
) |
| 121 |
|
| 122 |
return ActionFormatter.format(action) |
| 123 |
|
| 124 |
|
| 125 |
class Create(base.MistralLister): |
| 126 |
"""Create new action.""" |
| 127 |
|
| 128 |
def get_parser(self, prog_name): |
| 129 |
parser = super(Create, self).get_parser(prog_name) |
| 130 |
|
| 131 |
parser.add_argument( |
| 132 |
'definition', |
| 133 |
type=argparse.FileType('r'), |
| 134 |
help='Action definition file' |
| 135 |
) |
| 136 |
parser.add_argument( |
| 137 |
'--public', |
| 138 |
action='store_true', |
| 139 |
help='With this flag action will be marked as "public".' |
| 140 |
) |
| 141 |
parser.add_argument( |
| 142 |
'--namespace', |
| 143 |
nargs='?', |
| 144 |
default='', |
| 145 |
help="Namespace to create the action within.", |
| 146 |
) |
| 147 |
|
| 148 |
return parser |
| 149 |
|
| 150 |
def _validate_parsed_args(self, parsed_args): |
| 151 |
if not parsed_args.definition: |
| 152 |
raise RuntimeError("Provide action definition file.") |
| 153 |
|
| 154 |
def _get_format_function(self): |
| 155 |
return ActionFormatter.format_list |
| 156 |
|
| 157 |
def _get_resources(self, parsed_args): |
| 158 |
scope = 'public' if parsed_args.public else 'private' |
| 159 |
|
| 160 |
mistral_client = self.app.client_manager.workflow_engine |
| 161 |
|
| 162 |
return mistral_client.actions.create( |
| 163 |
parsed_args.definition.read(), |
| 164 |
namespace=parsed_args.namespace, |
| 165 |
scope=scope |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
class Delete(command.Command): |
| 170 |
"""Delete action.""" |
| 171 |
|
| 172 |
def get_parser(self, prog_name): |
| 173 |
parser = super(Delete, self).get_parser(prog_name) |
| 174 |
|
| 175 |
parser.add_argument( |
| 176 |
'action', |
| 177 |
nargs='+', |
| 178 |
help='Name or ID of action(s).' |
| 179 |
) |
| 180 |
parser.add_argument( |
| 181 |
'--namespace', |
| 182 |
nargs='?', |
| 183 |
default='', |
| 184 |
help="Namespace of the action(s).", |
| 185 |
) |
| 186 |
|
| 187 |
return parser |
| 188 |
|
| 189 |
def take_action(self, parsed_args): |
| 190 |
mistral_client = self.app.client_manager.workflow_engine |
| 191 |
|
| 192 |
utils.do_action_on_many( |
| 193 |
lambda s: mistral_client.actions.delete( |
| 194 |
s, |
| 195 |
namespace=parsed_args.namespace), |
| 196 |
parsed_args.action, |
| 197 |
"Request to delete action %s has been accepted.", |
| 198 |
"Unable to delete the specified action(s)." |
| 199 |
) |
| 200 |
|
| 201 |
|
| 202 |
class Update(base.MistralLister): |
| 203 |
"""Update action.""" |
| 204 |
|
| 205 |
def get_parser(self, prog_name): |
| 206 |
parser = super(Update, self).get_parser(prog_name) |
| 207 |
|
| 208 |
parser.add_argument( |
| 209 |
'definition', |
| 210 |
type=argparse.FileType('r'), |
| 211 |
help='Action definition file' |
| 212 |
) |
| 213 |
parser.add_argument('--id', help='Action ID.') |
| 214 |
parser.add_argument( |
| 215 |
'--public', |
| 216 |
action='store_true', |
| 217 |
help='With this flag action will be marked as "public".' |
| 218 |
) |
| 219 |
parser.add_argument( |
| 220 |
'--namespace', |
| 221 |
nargs='?', |
| 222 |
default='', |
| 223 |
help="Namespace of the action.", |
| 224 |
) |
| 225 |
|
| 226 |
return parser |
| 227 |
|
| 228 |
def _get_format_function(self): |
| 229 |
return ActionFormatter.format_list |
| 230 |
|
| 231 |
def _get_resources(self, parsed_args): |
| 232 |
scope = 'public' if parsed_args.public else 'private' |
| 233 |
|
| 234 |
mistral_client = self.app.client_manager.workflow_engine |
| 235 |
|
| 236 |
return mistral_client.actions.update( |
| 237 |
parsed_args.definition.read(), |
| 238 |
scope=scope, |
| 239 |
id=parsed_args.id |
| 240 |
) |
| 241 |
|
| 242 |
|
| 243 |
class GetDefinition(command.Command): |
| 244 |
"""Show action definition.""" |
| 245 |
|
| 246 |
def get_parser(self, prog_name): |
| 247 |
parser = super(GetDefinition, self).get_parser(prog_name) |
| 248 |
|
| 249 |
parser.add_argument('name', help='Action name') |
| 250 |
parser.add_argument( |
| 251 |
'--namespace', |
| 252 |
nargs='?', |
| 253 |
default='', |
| 254 |
help="Namespace of the action.", |
| 255 |
) |
| 256 |
|
| 257 |
return parser |
| 258 |
|
| 259 |
def take_action(self, parsed_args): |
| 260 |
mistral_client = self.app.client_manager.workflow_engine |
| 261 |
definition = mistral_client.actions.get( |
| 262 |
parsed_args.name, |
| 263 |
namespace=parsed_args.namespace).definition |
| 264 |
|
| 265 |
self.app.stdout.write(definition or "\n") |
| 266 |
|
| 267 |
|
| 268 |
class Validate(command.ShowOne): |
| 269 |
"""Validate action.""" |
| 270 |
|
| 271 |
@staticmethod |
| 272 |
def _format(result=None): |
| 273 |
columns = ('Valid', 'Error') |
| 274 |
|
| 275 |
if result: |
| 276 |
data = (result.get('valid'), result.get('error')) |
| 277 |
else: |
| 278 |
data = (tuple('<none>' for _ in range(len(columns))),) |
| 279 |
|
| 280 |
return columns, data |
| 281 |
|
| 282 |
def get_parser(self, prog_name): |
| 283 |
parser = super(Validate, self).get_parser(prog_name) |
| 284 |
|
| 285 |
parser.add_argument( |
| 286 |
'definition', |
| 287 |
type=argparse.FileType('r'), |
| 288 |
help='action definition file' |
| 289 |
) |
| 290 |
|
| 291 |
return parser |
| 292 |
|
| 293 |
def take_action(self, parsed_args): |
| 294 |
mistral_client = self.app.client_manager.workflow_engine |
| 295 |
|
| 296 |
result = mistral_client.actions.validate( |
| 297 |
parsed_args.definition.read() |
| 298 |
) |
| 299 |
|
| 300 |
return self._format(result) |