|
| 1 | +# Copyright 2012 OpenStack LLC. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | +# |
| 16 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 17 | + |
| 18 | +""" |
| 19 | +Role action implementations |
| 20 | +""" |
| 21 | + |
| 22 | +import logging |
| 23 | + |
| 24 | +from cliff import lister |
| 25 | +from cliff import show |
| 26 | + |
| 27 | +from openstackclient.common import command |
| 28 | +from openstackclient.common import utils |
| 29 | + |
| 30 | + |
| 31 | +class AddRole(command.OpenStackCommand, show.ShowOne): |
| 32 | + """Add role to tenant:user""" |
| 33 | + |
| 34 | + api = 'identity' |
| 35 | + log = logging.getLogger(__name__ + '.AddRole') |
| 36 | + |
| 37 | + def get_parser(self, prog_name): |
| 38 | + parser = super(AddRole, self).get_parser(prog_name) |
| 39 | + parser.add_argument( |
| 40 | + 'role', |
| 41 | + metavar='<role>', |
| 42 | + help='Role name or ID to add to user', |
| 43 | + ) |
| 44 | + parser.add_argument( |
| 45 | + '--tenant', |
| 46 | + metavar='<tenant>', |
| 47 | + required=True, |
| 48 | + help='Name or ID of tenant to include', |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + '--user', |
| 52 | + metavar='<user>', |
| 53 | + required=True, |
| 54 | + help='Name or ID of user to include', |
| 55 | + ) |
| 56 | + return parser |
| 57 | + |
| 58 | + def get_data(self, parsed_args): |
| 59 | + self.log.debug('get_data(%s)' % parsed_args) |
| 60 | + identity_client = self.app.client_manager.identity |
| 61 | + role = utils.find_resource( |
| 62 | + identity_client.roles, parsed_args.role) |
| 63 | + tenant = utils.find_resource( |
| 64 | + identity_client.tenants, parsed_args.tenant) |
| 65 | + user = utils.find_resource( |
| 66 | + identity_client.users, parsed_args.user) |
| 67 | + role = identity_client.roles.add_user_role( |
| 68 | + user, |
| 69 | + role, |
| 70 | + tenant, |
| 71 | + ) |
| 72 | + |
| 73 | + info = {} |
| 74 | + info.update(role._info) |
| 75 | + return zip(*sorted(info.iteritems())) |
| 76 | + |
| 77 | + |
| 78 | +class CreateRole(command.OpenStackCommand, show.ShowOne): |
| 79 | + """Create new role""" |
| 80 | + |
| 81 | + api = 'identity' |
| 82 | + log = logging.getLogger(__name__ + '.CreateRole') |
| 83 | + |
| 84 | + def get_parser(self, prog_name): |
| 85 | + parser = super(CreateRole, self).get_parser(prog_name) |
| 86 | + parser.add_argument( |
| 87 | + 'role_name', |
| 88 | + metavar='<role-name>', |
| 89 | + help='New role name', |
| 90 | + ) |
| 91 | + return parser |
| 92 | + |
| 93 | + def get_data(self, parsed_args): |
| 94 | + self.log.debug('get_data(%s)' % parsed_args) |
| 95 | + identity_client = self.app.client_manager.identity |
| 96 | + role = identity_client.roles.create( |
| 97 | + parsed_args.role_name, |
| 98 | + ) |
| 99 | + |
| 100 | + info = {} |
| 101 | + info.update(role._info) |
| 102 | + return zip(*sorted(info.iteritems())) |
| 103 | + |
| 104 | + |
| 105 | +class DeleteRole(command.OpenStackCommand): |
| 106 | + """Delete existing role""" |
| 107 | + |
| 108 | + api = 'identity' |
| 109 | + log = logging.getLogger(__name__ + '.DeleteRole') |
| 110 | + |
| 111 | + def get_parser(self, prog_name): |
| 112 | + parser = super(DeleteRole, self).get_parser(prog_name) |
| 113 | + parser.add_argument( |
| 114 | + 'role', |
| 115 | + metavar='<role>', |
| 116 | + help='Name or ID of role to delete', |
| 117 | + ) |
| 118 | + return parser |
| 119 | + |
| 120 | + def run(self, parsed_args): |
| 121 | + self.log.debug('run(%s)' % parsed_args) |
| 122 | + identity_client = self.app.client_manager.identity |
| 123 | + role = utils.find_resource( |
| 124 | + identity_client.roles, parsed_args.role) |
| 125 | + identity_client.roles.delete(role.id) |
| 126 | + return |
| 127 | + |
| 128 | + |
| 129 | +class ListRole(command.OpenStackCommand, lister.Lister): |
| 130 | + """List roles""" |
| 131 | + |
| 132 | + api = 'identity' |
| 133 | + log = logging.getLogger(__name__ + '.ListRole') |
| 134 | + |
| 135 | + def get_data(self, parsed_args): |
| 136 | + self.log.debug('get_data(%s)' % parsed_args) |
| 137 | + columns = ('ID', 'Name') |
| 138 | + data = self.app.client_manager.identity.roles.list() |
| 139 | + return (columns, |
| 140 | + (utils.get_item_properties( |
| 141 | + s, columns, |
| 142 | + formatters={}, |
| 143 | + ) for s in data), |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | +class ListUserRole(command.OpenStackCommand, lister.Lister): |
| 148 | + """List user-role assignments""" |
| 149 | + |
| 150 | + api = 'identity' |
| 151 | + log = logging.getLogger(__name__ + '.ListUserRole') |
| 152 | + |
| 153 | + def get_parser(self, prog_name): |
| 154 | + parser = super(ListUserRole, self).get_parser(prog_name) |
| 155 | + parser.add_argument( |
| 156 | + 'user', |
| 157 | + metavar='<user>', |
| 158 | + nargs='?', |
| 159 | + help='Name or ID of user to include', |
| 160 | + ) |
| 161 | + parser.add_argument( |
| 162 | + '--tenant', |
| 163 | + metavar='<tenant>', |
| 164 | + help='Name or ID of tenant to include', |
| 165 | + ) |
| 166 | + return parser |
| 167 | + |
| 168 | + def get_data(self, parsed_args): |
| 169 | + self.log.debug('get_data(%s)' % parsed_args) |
| 170 | + columns = ('ID', 'Name', 'Tenant ID', 'User ID') |
| 171 | + identity_client = self.app.client_manager.identity |
| 172 | + |
| 173 | + # user-only roles are not supported in KSL so we are |
| 174 | + # required to have a user and tenant; default to the |
| 175 | + # values used for authentication if not specified |
| 176 | + if not parsed_args.tenant: |
| 177 | + parsed_args.tenant = identity_client.auth_tenant_id |
| 178 | + if not parsed_args.user: |
| 179 | + parsed_args.user = identity_client.auth_user_id |
| 180 | + |
| 181 | + tenant = utils.find_resource( |
| 182 | + identity_client.tenants, parsed_args.tenant) |
| 183 | + user = utils.find_resource( |
| 184 | + identity_client.users, parsed_args.user) |
| 185 | + |
| 186 | + data = identity_client.roles.roles_for_user(user.id, tenant.id) |
| 187 | + |
| 188 | + # Add the names to the output even though they will be constant |
| 189 | + for role in data: |
| 190 | + role.user_id = user.name |
| 191 | + role.tenant_id = tenant.name |
| 192 | + |
| 193 | + return (columns, |
| 194 | + (utils.get_item_properties( |
| 195 | + s, columns, |
| 196 | + formatters={}, |
| 197 | + ) for s in data), |
| 198 | + ) |
| 199 | + |
| 200 | + |
| 201 | +class RemoveRole(command.OpenStackCommand): |
| 202 | + """Remove role from tenant:user""" |
| 203 | + |
| 204 | + api = 'identity' |
| 205 | + log = logging.getLogger(__name__ + '.RemoveRole') |
| 206 | + |
| 207 | + def get_parser(self, prog_name): |
| 208 | + parser = super(RemoveRole, self).get_parser(prog_name) |
| 209 | + parser.add_argument( |
| 210 | + 'role', |
| 211 | + metavar='<role>', |
| 212 | + help='Role name or ID to remove from user', |
| 213 | + ) |
| 214 | + parser.add_argument( |
| 215 | + '--tenant', |
| 216 | + metavar='<tenant>', |
| 217 | + required=True, |
| 218 | + help='Name or ID of tenant', |
| 219 | + ) |
| 220 | + parser.add_argument( |
| 221 | + '--user', |
| 222 | + metavar='<user>', |
| 223 | + required=True, |
| 224 | + help='Name or ID of user', |
| 225 | + ) |
| 226 | + return parser |
| 227 | + |
| 228 | + def get_data(self, parsed_args): |
| 229 | + self.log.debug('get_data(%s)' % parsed_args) |
| 230 | + identity_client = self.app.client_manager.identity |
| 231 | + role = utils.find_resource( |
| 232 | + identity_client.roles, parsed_args.role) |
| 233 | + tenant = utils.find_resource( |
| 234 | + identity_client.tenants, parsed_args.tenant) |
| 235 | + user = utils.find_resource( |
| 236 | + identity_client.users, parsed_args.user) |
| 237 | + print "role: %s" % role |
| 238 | + identity_client.roles.remove_user_role( |
| 239 | + user.id, |
| 240 | + role.id, |
| 241 | + tenant.id, |
| 242 | + ) |
| 243 | + |
| 244 | + |
| 245 | +class ShowRole(command.OpenStackCommand, show.ShowOne): |
| 246 | + """Show single role""" |
| 247 | + |
| 248 | + api = 'identity' |
| 249 | + log = logging.getLogger(__name__ + '.ShowRole') |
| 250 | + |
| 251 | + def get_parser(self, prog_name): |
| 252 | + parser = super(ShowRole, self).get_parser(prog_name) |
| 253 | + parser.add_argument( |
| 254 | + 'role', |
| 255 | + metavar='<role>', |
| 256 | + help='Name or ID of role to display', |
| 257 | + ) |
| 258 | + return parser |
| 259 | + |
| 260 | + def get_data(self, parsed_args): |
| 261 | + self.log.debug('get_data(%s)' % parsed_args) |
| 262 | + identity_client = self.app.client_manager.identity |
| 263 | + role = utils.find_resource( |
| 264 | + identity_client.roles, parsed_args.role) |
| 265 | + |
| 266 | + info = {} |
| 267 | + info.update(role._info) |
| 268 | + return zip(*sorted(info.iteritems())) |
0 commit comments