|
| 1 | +# Copyright 2012-2013 OpenStack Foundation |
| 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 Implied Role action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from osc_lib.command import command |
| 21 | +import six |
| 22 | + |
| 23 | +from openstackclient.i18n import _ |
| 24 | + |
| 25 | + |
| 26 | +LOG = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def _get_role_ids(identity_client, parsed_args): |
| 30 | + """Return prior and implied role id(s) |
| 31 | +
|
| 32 | + If prior and implied role id(s) are retrievable from identity |
| 33 | + client, return tuple containing them. |
| 34 | + """ |
| 35 | + role_id = None |
| 36 | + implied_role_id = None |
| 37 | + |
| 38 | + roles = identity_client.roles.list() |
| 39 | + |
| 40 | + for role in roles: |
| 41 | + role_id_or_name = (role.name, role.id) |
| 42 | + |
| 43 | + if parsed_args.role in role_id_or_name: |
| 44 | + role_id = role.id |
| 45 | + elif parsed_args.implied_role in role_id_or_name: |
| 46 | + implied_role_id = role.id |
| 47 | + |
| 48 | + return (role_id, implied_role_id) |
| 49 | + |
| 50 | + |
| 51 | +class CreateImpliedRole(command.ShowOne): |
| 52 | + |
| 53 | + _description = _("Creates an association between prior and implied roles") |
| 54 | + |
| 55 | + def get_parser(self, prog_name): |
| 56 | + parser = super(CreateImpliedRole, self).get_parser(prog_name) |
| 57 | + parser.add_argument( |
| 58 | + 'role', |
| 59 | + metavar='<role>', |
| 60 | + help=_('Role (name or ID) that implies another role'), |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + '--implied-role', |
| 64 | + metavar='<role>', |
| 65 | + help='<role> (name or ID) implied by another role', |
| 66 | + required=True, |
| 67 | + ) |
| 68 | + return parser |
| 69 | + |
| 70 | + def take_action(self, parsed_args): |
| 71 | + identity_client = self.app.client_manager.identity |
| 72 | + (prior_role_id, implied_role_id) = _get_role_ids( |
| 73 | + identity_client, parsed_args) |
| 74 | + response = identity_client.roles.create_implied( |
| 75 | + prior_role_id, implied_role_id) |
| 76 | + response._info.pop('links', None) |
| 77 | + return zip(*sorted([(k, v['id']) |
| 78 | + for k, v in six.iteritems(response._info)])) |
| 79 | + |
| 80 | + |
| 81 | +class DeleteImpliedRole(command.Command): |
| 82 | + |
| 83 | + _description = _("Deletes an association between prior and implied roles") |
| 84 | + |
| 85 | + def get_parser(self, prog_name): |
| 86 | + parser = super(DeleteImpliedRole, self).get_parser(prog_name) |
| 87 | + parser.add_argument( |
| 88 | + 'role', |
| 89 | + metavar='<role>', |
| 90 | + help=_('Role (name or ID) that implies another role'), |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + '--implied-role', |
| 94 | + metavar='<role>', |
| 95 | + help='<role> (name or ID) implied by another role', |
| 96 | + required=True, |
| 97 | + ) |
| 98 | + return parser |
| 99 | + |
| 100 | + def take_action(self, parsed_args): |
| 101 | + identity_client = self.app.client_manager.identity |
| 102 | + (prior_role_id, implied_role_id) = _get_role_ids( |
| 103 | + identity_client, parsed_args) |
| 104 | + identity_client.roles.delete_implied( |
| 105 | + prior_role_id, implied_role_id) |
| 106 | + |
| 107 | + |
| 108 | +class ListImpliedRole(command.Lister): |
| 109 | + |
| 110 | + _description = _("List implied roles") |
| 111 | + _COLUMNS = ['Prior Role ID', 'Prior Role Name', |
| 112 | + 'Implied Role ID', 'Implied Role Name'] |
| 113 | + |
| 114 | + def get_parser(self, prog_name): |
| 115 | + parser = super(ListImpliedRole, self).get_parser(prog_name) |
| 116 | + return parser |
| 117 | + |
| 118 | + def take_action(self, parsed_args): |
| 119 | + def _list_implied(response): |
| 120 | + for rule in response: |
| 121 | + for implies in rule.implies: |
| 122 | + yield (rule.prior_role['id'], |
| 123 | + rule.prior_role['name'], |
| 124 | + implies['id'], |
| 125 | + implies['name']) |
| 126 | + |
| 127 | + identity_client = self.app.client_manager.identity |
| 128 | + response = identity_client.roles.list_inference_roles() |
| 129 | + return (self._COLUMNS, _list_implied(response)) |
0 commit comments