| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import argparse |
| 16 |
|
| 17 |
from cliff import show |
| 18 |
from osc_lib.command import command |
| 19 |
|
| 20 |
from mistralclient.commands.v2 import base |
| 21 |
from mistralclient import utils |
| 22 |
|
| 23 |
|
| 24 |
class CodeSourceFormatter(base.MistralFormatter): |
| 25 |
COLUMNS = [ |
| 26 |
('id', 'ID'), |
| 27 |
('name', 'Name'), |
| 28 |
('namespace', 'Namespace'), |
| 29 |
('project_id', 'Project ID'), |
| 30 |
('scope', 'Scope'), |
| 31 |
('created_at', 'Created at'), |
| 32 |
('updated_at', 'Updated at') |
| 33 |
] |
| 34 |
|
| 35 |
@staticmethod |
| 36 |
def format(code_src=None, lister=False): |
| 37 |
if code_src: |
| 38 |
data = ( |
| 39 |
code_src.id, |
| 40 |
code_src.name, |
| 41 |
code_src.namespace, |
| 42 |
code_src.project_id, |
| 43 |
code_src.scope, |
| 44 |
code_src.created_at |
| 45 |
) |
| 46 |
|
| 47 |
if hasattr(code_src, 'updated_at'): |
| 48 |
data += (code_src.updated_at,) |
| 49 |
else: |
| 50 |
data += (None,) |
| 51 |
else: |
| 52 |
data = (('',) * len(CodeSourceFormatter.COLUMNS),) |
| 53 |
|
| 54 |
return CodeSourceFormatter.headings(), data |
| 55 |
|
| 56 |
|
| 57 |
class List(base.MistralLister): |
| 58 |
"""List all workflows.""" |
| 59 |
|
| 60 |
def _get_format_function(self): |
| 61 |
return CodeSourceFormatter.format_list |
| 62 |
|
| 63 |
def get_parser(self, prog_name): |
| 64 |
parser = super(List, self).get_parser(prog_name) |
| 65 |
|
| 66 |
return parser |
| 67 |
|
| 68 |
def _get_resources(self, parsed_args): |
| 69 |
mistral_client = self.app.client_manager.workflow_engine |
| 70 |
|
| 71 |
return mistral_client.code_sources.list( |
| 72 |
marker=parsed_args.marker, |
| 73 |
limit=parsed_args.limit, |
| 74 |
sort_keys=parsed_args.sort_keys, |
| 75 |
sort_dirs=parsed_args.sort_dirs, |
| 76 |
fields=CodeSourceFormatter.fields(), |
| 77 |
**base.get_filters(parsed_args) |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
class Get(show.ShowOne): |
| 82 |
"""Show specific code source.""" |
| 83 |
|
| 84 |
def get_parser(self, prog_name): |
| 85 |
parser = super(Get, self).get_parser(prog_name) |
| 86 |
|
| 87 |
parser.add_argument('identifier', help='Code source ID or name.') |
| 88 |
parser.add_argument( |
| 89 |
'--namespace', |
| 90 |
nargs='?', |
| 91 |
default='', |
| 92 |
help="Namespace to get the code source from.", |
| 93 |
) |
| 94 |
|
| 95 |
return parser |
| 96 |
|
| 97 |
def take_action(self, parsed_args): |
| 98 |
mistral_client = self.app.client_manager.workflow_engine |
| 99 |
|
| 100 |
wf = mistral_client.code_sources.get( |
| 101 |
parsed_args.identifier, |
| 102 |
parsed_args.namespace |
| 103 |
) |
| 104 |
|
| 105 |
return CodeSourceFormatter.format(wf) |
| 106 |
|
| 107 |
|
| 108 |
class Create(command.ShowOne): |
| 109 |
"""Create new code source.""" |
| 110 |
|
| 111 |
def get_parser(self, prog_name): |
| 112 |
parser = super(Create, self).get_parser(prog_name) |
| 113 |
|
| 114 |
parser.add_argument('name', help='Code source name.') |
| 115 |
parser.add_argument( |
| 116 |
'content', |
| 117 |
type=argparse.FileType('r'), |
| 118 |
help='Code source content file.' |
| 119 |
) |
| 120 |
parser.add_argument( |
| 121 |
'--namespace', |
| 122 |
nargs='?', |
| 123 |
default='', |
| 124 |
help="Namespace to create the code source within.", |
| 125 |
) |
| 126 |
parser.add_argument( |
| 127 |
'--public', |
| 128 |
action='store_true', |
| 129 |
help='With this flag the code source will be marked as "public".' |
| 130 |
) |
| 131 |
|
| 132 |
return parser |
| 133 |
|
| 134 |
def take_action(self, parsed_args): |
| 135 |
scope = 'public' if parsed_args.public else 'private' |
| 136 |
|
| 137 |
mistral_client = self.app.client_manager.workflow_engine |
| 138 |
|
| 139 |
code_source = mistral_client.code_sources.create( |
| 140 |
parsed_args.name, |
| 141 |
parsed_args.content.read(), |
| 142 |
namespace=parsed_args.namespace, |
| 143 |
scope=scope |
| 144 |
) |
| 145 |
|
| 146 |
return CodeSourceFormatter.format(code_source) |
| 147 |
|
| 148 |
|
| 149 |
class Delete(command.Command): |
| 150 |
"""Delete workflow.""" |
| 151 |
|
| 152 |
def get_parser(self, prog_name): |
| 153 |
parser = super(Delete, self).get_parser(prog_name) |
| 154 |
|
| 155 |
parser.add_argument( |
| 156 |
'identifier', |
| 157 |
nargs='+', |
| 158 |
help='Code source name or ID (can be repeated multiple times).' |
| 159 |
) |
| 160 |
|
| 161 |
parser.add_argument( |
| 162 |
'--namespace', |
| 163 |
nargs='?', |
| 164 |
default=None, |
| 165 |
help="Namespace to delete the code source(s) from.", |
| 166 |
) |
| 167 |
|
| 168 |
return parser |
| 169 |
|
| 170 |
def take_action(self, parsed_args): |
| 171 |
mistral_client = self.app.client_manager.workflow_engine |
| 172 |
|
| 173 |
utils.do_action_on_many( |
| 174 |
lambda s: |
| 175 |
mistral_client.code_sources.delete(s, parsed_args.namespace), |
| 176 |
parsed_args.identifier, |
| 177 |
"Request to delete code source '%s' has been accepted.", |
| 178 |
"Unable to delete the specified code source(s)." |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
class Update(command.ShowOne): |
| 183 |
"""Update workflow.""" |
| 184 |
|
| 185 |
def get_parser(self, prog_name): |
| 186 |
parser = super(Update, self).get_parser(prog_name) |
| 187 |
|
| 188 |
parser.add_argument( |
| 189 |
'identifier', |
| 190 |
help='Code source identifier (name or ID).' |
| 191 |
) |
| 192 |
parser.add_argument( |
| 193 |
'content', |
| 194 |
type=argparse.FileType('r'), |
| 195 |
help='Code source content' |
| 196 |
) |
| 197 |
parser.add_argument('--id', help='Workflow ID.') |
| 198 |
parser.add_argument( |
| 199 |
'--namespace', |
| 200 |
nargs='?', |
| 201 |
default='', |
| 202 |
help="Namespace of the workflow.", |
| 203 |
) |
| 204 |
parser.add_argument( |
| 205 |
'--public', |
| 206 |
action='store_true', |
| 207 |
help='With this flag workflow will be marked as "public".' |
| 208 |
) |
| 209 |
|
| 210 |
return parser |
| 211 |
|
| 212 |
def take_action(self, parsed_args): |
| 213 |
scope = 'public' if parsed_args.public else 'private' |
| 214 |
|
| 215 |
mistral_client = self.app.client_manager.workflow_engine |
| 216 |
|
| 217 |
code_src = mistral_client.code_sources.update( |
| 218 |
parsed_args.identifier, |
| 219 |
parsed_args.content.read(), |
| 220 |
namespace=parsed_args.namespace, |
| 221 |
scope=scope |
| 222 |
) |
| 223 |
|
| 224 |
return CodeSourceFormatter.format(code_src) |
| 225 |
|
| 226 |
|
| 227 |
class GetContent(command.Command): |
| 228 |
"""Show workflow definition.""" |
| 229 |
|
| 230 |
def get_parser(self, prog_name): |
| 231 |
parser = super(GetContent, self).get_parser(prog_name) |
| 232 |
|
| 233 |
parser.add_argument('identifier', help='Code source ID or name.') |
| 234 |
parser.add_argument( |
| 235 |
'--namespace', |
| 236 |
nargs='?', |
| 237 |
default='', |
| 238 |
help="Namespace to get the code source from.", |
| 239 |
) |
| 240 |
|
| 241 |
return parser |
| 242 |
|
| 243 |
def take_action(self, parsed_args): |
| 244 |
mistral_client = self.app.client_manager.workflow_engine |
| 245 |
|
| 246 |
code_src = mistral_client.code_sources.get( |
| 247 |
parsed_args.identifier, |
| 248 |
parsed_args.namespace |
| 249 |
) |
| 250 |
|
| 251 |
self.app.stdout.write(code_src.content or "\n") |