|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Identity v2 Assignment action implementations """ |
| 15 | + |
| 16 | +from openstackclient.common import command |
| 17 | +from openstackclient.common import exceptions |
| 18 | +from openstackclient.common import utils |
| 19 | +from openstackclient.i18n import _ # noqa |
| 20 | + |
| 21 | + |
| 22 | +class ListRoleAssignment(command.Lister): |
| 23 | + """List role assignments""" |
| 24 | + |
| 25 | + def get_parser(self, prog_name): |
| 26 | + parser = super(ListRoleAssignment, self).get_parser(prog_name) |
| 27 | + parser.add_argument( |
| 28 | + '--user', |
| 29 | + metavar='<user>', |
| 30 | + help='User to filter (name or ID)', |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + '--project', |
| 34 | + metavar='<project>', |
| 35 | + help='Project to filter (name or ID)', |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + '--names', |
| 39 | + action="store_true", |
| 40 | + help='Display names instead of IDs', |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + '--auth-user', |
| 44 | + action="store_true", |
| 45 | + dest='authuser', |
| 46 | + help='Only list assignments for the authenticated user', |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + '--auth-project', |
| 50 | + action="store_true", |
| 51 | + dest='authproject', |
| 52 | + help='Only list assignments for the project to which the ' |
| 53 | + 'authenticated user\'s token is scoped', |
| 54 | + ) |
| 55 | + return parser |
| 56 | + |
| 57 | + def take_action(self, parsed_args): |
| 58 | + identity_client = self.app.client_manager.identity |
| 59 | + auth_ref = self.app.client_manager.auth_ref |
| 60 | + |
| 61 | + include_names = True if parsed_args.names else False |
| 62 | + |
| 63 | + user = None |
| 64 | + if parsed_args.user: |
| 65 | + user = utils.find_resource( |
| 66 | + identity_client.users, |
| 67 | + parsed_args.user, |
| 68 | + ) |
| 69 | + elif parsed_args.authuser: |
| 70 | + if auth_ref: |
| 71 | + user = utils.find_resource( |
| 72 | + identity_client.users, |
| 73 | + auth_ref.user_id |
| 74 | + ) |
| 75 | + |
| 76 | + project = None |
| 77 | + if parsed_args.project: |
| 78 | + project = utils.find_resource( |
| 79 | + identity_client.projects, |
| 80 | + parsed_args.project, |
| 81 | + ) |
| 82 | + elif parsed_args.authproject: |
| 83 | + if auth_ref: |
| 84 | + project = utils.find_resource( |
| 85 | + identity_client.projects, |
| 86 | + auth_ref.project_id |
| 87 | + ) |
| 88 | + |
| 89 | + # If user or project is not specified, we would ideally list all |
| 90 | + # relevant assignments in the system (to be compatible with v3). |
| 91 | + # However, there is no easy way of doing that in v2. |
| 92 | + if not user or not project: |
| 93 | + msg = _("Project and User must be specified") |
| 94 | + raise exceptions.CommandError(msg) |
| 95 | + else: |
| 96 | + data = identity_client.roles.roles_for_user(user.id, project.id) |
| 97 | + |
| 98 | + columns = ('Role', 'User', 'Project') |
| 99 | + for user_role in data: |
| 100 | + if include_names: |
| 101 | + setattr(user_role, 'role', user_role.name) |
| 102 | + user_role.user = user.name |
| 103 | + user_role.project = project.name |
| 104 | + else: |
| 105 | + setattr(user_role, 'role', user_role.id) |
| 106 | + user_role.user = user.id |
| 107 | + user_role.project = project.id |
| 108 | + |
| 109 | + return (columns, |
| 110 | + (utils.get_item_properties( |
| 111 | + s, columns, |
| 112 | + formatters={}, |
| 113 | + ) for s in data)) |
0 commit comments