Debian Codegraph @

adjutantclient/osc/v1/notifications.py

raw
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
18 from osc_lib.command import command
19 from osc_lib.i18n import _
20
21
22 LOG = logging.getLogger(__name__)
23
24
25 def _show_notification(notification_id, client, formatter):
26 notification = client.notifications.get(notification_id)
27 if formatter == 'table':
28 notification._info['notes'] = json.dumps(
29 notification.notes, indent=2)
30 return zip(*(notification.to_dict()).items())
31
32
33 class NotificationList(command.Lister):
34 """Lists adjutant notifications. """
35
36 def get_parser(self, prog_name):
37 parser = super(NotificationList, self).get_parser(prog_name)
38
39 parser.add_argument(
40 '--filters', metavar='<filters>',
41 required=False,
42 help=_('JSON containing filters for the notifications.'),
43 default={})
44
45 return parser
46
47 def take_action(self, parsed_args):
48 client = self.app.client_manager.admin_logic
49
50 kwargs = {}
51 if parsed_args.filters:
52 kwargs['filters'] = parsed_args.filters
53
54 notifications = client.notifications.list(filters=parsed_args.filters)
55
56 headers = ['uuid', 'task', 'acknowledged', 'created_on']
57
58 rows = [[notif.uuid, notif.task, notif.acknowledged,
59 notif.created_on] for notif in notifications]
60
61 return headers, rows
62
63
64 class NotificationShow(command.ShowOne):
65 """Show details of one notification."""
66
67 def get_parser(self, prog_name):
68 parser = super(NotificationShow, self).get_parser(prog_name)
69
70 parser.add_argument(
71 'notification_id', metavar='<notification_id>',
72 help=_("The notification ID."))
73 return parser
74
75 def take_action(self, parsed_args):
76 client = self.app.client_manager.admin_logic
77 return _show_notification(parsed_args.notification_id, client,
78 parsed_args.formatter)
79
80
81 class NotificationAcknowledge(command.Command):
82 """Approve and show details of a notification."""
83 def get_parser(self, prog_name):
84 parser = super(NotificationAcknowledge, self).get_parser(prog_name)
85
86 parser.add_argument('note_ids', metavar='<noteids>', nargs='+',
87 help=_('Notification IDs to acknowledge.'))
88 return parser
89
90 def take_action(self, parsed_args):
91 client = self.app.client_manager.admin_logic
92 resp = client.notifications.acknowledge(note_list=parsed_args.note_ids)
93 print('Success', ' '.join(resp.notes))