| 1 |
# Copyright (c) 2016 Catalyst IT Ltd. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import json |
| 16 |
import logging |
| 17 |
from osc_lib.command import command |
| 18 |
from osc_lib.i18n import _ |
| 19 |
|
| 20 |
|
| 21 |
LOG = logging.getLogger(__name__) |
| 22 |
|
| 23 |
|
| 24 |
def _show_task(task_id, client, formatter): |
| 25 |
task = client.tasks.get(task_id) |
| 26 |
if formatter == 'table': |
| 27 |
task._info['actions'] = json.dumps( |
| 28 |
getattr(task, 'actions', ""), indent=2) |
| 29 |
task._info['keystone_user'] = json.dumps( |
| 30 |
getattr(task, 'keystone_user', ""), indent=2) |
| 31 |
task._info['action_notes'] = json.dumps( |
| 32 |
getattr(task, 'action_notes', ""), indent=2) |
| 33 |
task._info['approved_by'] = json.dumps( |
| 34 |
getattr(task, 'approved_by', ""), indent=2) |
| 35 |
return zip(*(task.to_dict()).items()) |
| 36 |
|
| 37 |
|
| 38 |
class TaskList(command.Lister): |
| 39 |
"""Lists adjutant tasks. """ |
| 40 |
|
| 41 |
def get_parser(self, prog_name): |
| 42 |
parser = super(TaskList, self).get_parser(prog_name) |
| 43 |
|
| 44 |
parser.add_argument( |
| 45 |
'--limit', metavar='<limit>', |
| 46 |
required=False, |
| 47 |
help=_('Limit the number of task responses.')) |
| 48 |
parser.add_argument( |
| 49 |
'--filters', metavar='<filters>', |
| 50 |
required=False, |
| 51 |
help=_('JSON containing filters for the tasks.'), |
| 52 |
default={}) |
| 53 |
|
| 54 |
return parser |
| 55 |
|
| 56 |
def take_action(self, parsed_args): |
| 57 |
client = self.app.client_manager.admin_logic |
| 58 |
|
| 59 |
kwargs = {'filters': parsed_args.filters} |
| 60 |
|
| 61 |
if parsed_args.limit: |
| 62 |
kwargs['tasks_per_page'] = parsed_args.limit |
| 63 |
|
| 64 |
tasks = client.tasks.list(**kwargs) |
| 65 |
|
| 66 |
headers = [ |
| 67 |
'UUID', 'Task Type', 'Created on', |
| 68 |
'Approved on', 'Completed on', 'Cancelled'] |
| 69 |
|
| 70 |
rows = [[task.uuid, task.task_type, task.created_on, |
| 71 |
task.approved_on, task.completed_on, task.cancelled] |
| 72 |
for task in tasks] |
| 73 |
|
| 74 |
return headers, rows |
| 75 |
|
| 76 |
|
| 77 |
class TaskShow(command.ShowOne): |
| 78 |
"""Show details of one task.""" |
| 79 |
|
| 80 |
def get_parser(self, prog_name): |
| 81 |
parser = super(TaskShow, self).get_parser(prog_name) |
| 82 |
|
| 83 |
parser.add_argument( |
| 84 |
'task_id', metavar='<taskid>', |
| 85 |
help=_("The task ID.")) |
| 86 |
return parser |
| 87 |
|
| 88 |
def take_action(self, parsed_args): |
| 89 |
client = self.app.client_manager.admin_logic |
| 90 |
return _show_task(parsed_args.task_id, client, parsed_args.formatter) |
| 91 |
|
| 92 |
|
| 93 |
class TaskApprove(command.ShowOne): |
| 94 |
"""Approve and show details of a task.""" |
| 95 |
def get_parser(self, prog_name): |
| 96 |
parser = super(TaskApprove, self).get_parser(prog_name) |
| 97 |
|
| 98 |
parser.add_argument( |
| 99 |
'task_id', metavar='<taskid>', |
| 100 |
help=_("The task ID.")) |
| 101 |
return parser |
| 102 |
|
| 103 |
def take_action(self, parsed_args): |
| 104 |
client = self.app.client_manager.admin_logic |
| 105 |
client.tasks.approve(parsed_args.task_id) |
| 106 |
return _show_task(parsed_args.task_id, client, parsed_args.formatter) |
| 107 |
|
| 108 |
|
| 109 |
class TaskCancel(command.ShowOne): |
| 110 |
"""Approve and show details of a task.""" |
| 111 |
def get_parser(self, prog_name): |
| 112 |
parser = super(TaskCancel, self).get_parser(prog_name) |
| 113 |
|
| 114 |
parser.add_argument( |
| 115 |
'task_id', metavar='<taskid>', |
| 116 |
help=_("The task ID.")) |
| 117 |
return parser |
| 118 |
|
| 119 |
def take_action(self, parsed_args): |
| 120 |
client = self.app.client_manager.admin_logic |
| 121 |
client.tasks.cancel(task_id=parsed_args.task_id) |
| 122 |
return _show_task(parsed_args.task_id, client, parsed_args.formatter) |
| 123 |
|
| 124 |
|
| 125 |
class TaskUpdate(command.ShowOne): |
| 126 |
"""Update data and re-run pre-approve for a task.""" |
| 127 |
def get_parser(self, prog_name): |
| 128 |
parser = super(TaskUpdate, self).get_parser(prog_name) |
| 129 |
|
| 130 |
parser.add_argument( |
| 131 |
'task_id', metavar='<taskid>', |
| 132 |
help=_("The task ID.")) |
| 133 |
parser.add_argument( |
| 134 |
'data', metavar='<data>', |
| 135 |
help=_("New JSON action data.")) |
| 136 |
return parser |
| 137 |
|
| 138 |
def take_action(self, parsed_args): |
| 139 |
client = self.app.client_manager.admin_logic |
| 140 |
client.tasks.update(task_id=parsed_args.task_id, |
| 141 |
data=json.loads(parsed_args.data)) |
| 142 |
return _show_task(parsed_args.task_id, client, parsed_args.formatter) |
| 143 |
|
| 144 |
|
| 145 |
class TaskTokenReissue(command.Command): |
| 146 |
"""Reissues a token for the provided pending task """ |
| 147 |
def get_parser(self, prog_name): |
| 148 |
parser = super(TaskTokenReissue, self).get_parser(prog_name) |
| 149 |
|
| 150 |
parser.add_argument('task_id', metavar='<task_id>', |
| 151 |
help=_('The task ID.')) |
| 152 |
return parser |
| 153 |
|
| 154 |
def take_action(self, parsed_args): |
| 155 |
client = self.app.client_manager.admin_logic |
| 156 |
resp = client.tokens.reissue(task_id=parsed_args.task_id) |
| 157 |
print('Success', ' '.join(resp.notes)) |