|
| 1 | +# Copyright 2019 SUSE LLC |
| 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 | + |
| 16 | +"""Identity v3 Access Rule action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from osc_lib.command import command |
| 21 | +from osc_lib import exceptions |
| 22 | +from osc_lib import utils |
| 23 | +import six |
| 24 | + |
| 25 | +from openstackclient.i18n import _ |
| 26 | +from openstackclient.identity import common |
| 27 | + |
| 28 | + |
| 29 | +LOG = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +class DeleteAccessRule(command.Command): |
| 33 | + _description = _("Delete access rule(s)") |
| 34 | + |
| 35 | + def get_parser(self, prog_name): |
| 36 | + parser = super(DeleteAccessRule, self).get_parser(prog_name) |
| 37 | + parser.add_argument( |
| 38 | + 'access_rule', |
| 39 | + metavar='<access-rule>', |
| 40 | + nargs="+", |
| 41 | + help=_('Application credentials(s) to delete (name or ID)'), |
| 42 | + ) |
| 43 | + return parser |
| 44 | + |
| 45 | + def take_action(self, parsed_args): |
| 46 | + identity_client = self.app.client_manager.identity |
| 47 | + |
| 48 | + errors = 0 |
| 49 | + for ac in parsed_args.access_rule: |
| 50 | + try: |
| 51 | + access_rule = utils.find_resource( |
| 52 | + identity_client.access_rules, ac) |
| 53 | + identity_client.access_rules.delete(access_rule.id) |
| 54 | + except Exception as e: |
| 55 | + errors += 1 |
| 56 | + LOG.error(_("Failed to delete access rule with " |
| 57 | + "ID '%(ac)s': %(e)s"), |
| 58 | + {'ac': ac, 'e': e}) |
| 59 | + |
| 60 | + if errors > 0: |
| 61 | + total = len(parsed_args.access_rule) |
| 62 | + msg = (_("%(errors)s of %(total)s access rules failed " |
| 63 | + "to delete.") % {'errors': errors, 'total': total}) |
| 64 | + raise exceptions.CommandError(msg) |
| 65 | + |
| 66 | + |
| 67 | +class ListAccessRule(command.Lister): |
| 68 | + _description = _("List access rules") |
| 69 | + |
| 70 | + def get_parser(self, prog_name): |
| 71 | + parser = super(ListAccessRule, self).get_parser(prog_name) |
| 72 | + parser.add_argument( |
| 73 | + '--user', |
| 74 | + metavar='<user>', |
| 75 | + help=_('User whose access rules to list (name or ID)'), |
| 76 | + ) |
| 77 | + common.add_user_domain_option_to_parser(parser) |
| 78 | + return parser |
| 79 | + |
| 80 | + def take_action(self, parsed_args): |
| 81 | + identity_client = self.app.client_manager.identity |
| 82 | + if parsed_args.user: |
| 83 | + user_id = common.find_user(identity_client, |
| 84 | + parsed_args.user, |
| 85 | + parsed_args.user_domain).id |
| 86 | + else: |
| 87 | + user_id = None |
| 88 | + |
| 89 | + columns = ('ID', 'Service', 'Method', 'Path') |
| 90 | + data = identity_client.access_rules.list( |
| 91 | + user=user_id) |
| 92 | + return (columns, |
| 93 | + (utils.get_item_properties( |
| 94 | + s, columns, |
| 95 | + formatters={}, |
| 96 | + ) for s in data)) |
| 97 | + |
| 98 | + |
| 99 | +class ShowAccessRule(command.ShowOne): |
| 100 | + _description = _("Display access rule details") |
| 101 | + |
| 102 | + def get_parser(self, prog_name): |
| 103 | + parser = super(ShowAccessRule, self).get_parser(prog_name) |
| 104 | + parser.add_argument( |
| 105 | + 'access_rule', |
| 106 | + metavar='<access-rule>', |
| 107 | + help=_('Application credential to display (name or ID)'), |
| 108 | + ) |
| 109 | + return parser |
| 110 | + |
| 111 | + def take_action(self, parsed_args): |
| 112 | + identity_client = self.app.client_manager.identity |
| 113 | + access_rule = utils.find_resource(identity_client.access_rules, |
| 114 | + parsed_args.access_rule) |
| 115 | + |
| 116 | + access_rule._info.pop('links', None) |
| 117 | + |
| 118 | + return zip(*sorted(six.iteritems(access_rule._info))) |
0 commit comments