|
| 1 | +# Copyright 2012-2013 OpenStack, 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 Role action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | +import sys |
| 20 | + |
| 21 | +from cliff import command |
| 22 | +from cliff import lister |
| 23 | +from cliff import show |
| 24 | + |
| 25 | +from openstackclient.common import utils |
| 26 | + |
| 27 | + |
| 28 | +class CreateRole(show.ShowOne): |
| 29 | + """Create new role""" |
| 30 | + |
| 31 | + api = 'identity' |
| 32 | + log = logging.getLogger(__name__ + '.CreateRole') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(CreateRole, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + 'role-name', |
| 38 | + metavar='<role-name>', |
| 39 | + help='New role name') |
| 40 | + return parser |
| 41 | + |
| 42 | + def take_action(self, parsed_args): |
| 43 | + self.log.debug('take_action(%s)' % parsed_args) |
| 44 | + identity_client = self.app.client_manager.identity |
| 45 | + role = identity_client.roles.create(parsed_args.role_name) |
| 46 | + |
| 47 | + return zip(*sorted(role._info.iteritems())) |
| 48 | + |
| 49 | + |
| 50 | +class DeleteRole(command.Command): |
| 51 | + """Delete existing role""" |
| 52 | + |
| 53 | + api = 'identity' |
| 54 | + log = logging.getLogger(__name__ + '.DeleteRole') |
| 55 | + |
| 56 | + def get_parser(self, prog_name): |
| 57 | + parser = super(DeleteRole, self).get_parser(prog_name) |
| 58 | + parser.add_argument( |
| 59 | + 'role', |
| 60 | + metavar='<role>', |
| 61 | + help='Name or ID of role to delete') |
| 62 | + return parser |
| 63 | + |
| 64 | + def take_action(self, parsed_args): |
| 65 | + self.log.debug('take_action(%s)' % parsed_args) |
| 66 | + identity_client = self.app.client_manager.identity |
| 67 | + role_id = utils.find_resource(identity_client.roles, |
| 68 | + parsed_args.role) |
| 69 | + identity_client.roles.delete(role_id) |
| 70 | + return |
| 71 | + |
| 72 | + |
| 73 | +class ListRole(lister.Lister): |
| 74 | + """List roles""" |
| 75 | + |
| 76 | + api = 'identity' |
| 77 | + log = logging.getLogger(__name__ + '.ListRole') |
| 78 | + |
| 79 | + def take_action(self, parsed_args): |
| 80 | + self.log.debug('take_action(%s)' % parsed_args) |
| 81 | + columns = ('ID', 'Name') |
| 82 | + data = self.app.client_manager.identity.roles.list() |
| 83 | + return (columns, |
| 84 | + (utils.get_item_properties( |
| 85 | + s, columns, |
| 86 | + formatters={}, |
| 87 | + ) for s in data)) |
| 88 | + |
| 89 | + |
| 90 | +class SetRole(command.Command): |
| 91 | + """Set role command""" |
| 92 | + |
| 93 | + api = 'identity' |
| 94 | + log = logging.getLogger(__name__ + '.SetRole') |
| 95 | + |
| 96 | + def get_parser(self, prog_name): |
| 97 | + parser = super(SetRole, self).get_parser(prog_name) |
| 98 | + parser.add_argument( |
| 99 | + 'role', |
| 100 | + metavar='<role>', |
| 101 | + help='Name or ID of role to change', |
| 102 | + ) |
| 103 | + parser.add_argument( |
| 104 | + '--name', |
| 105 | + metavar='<new-role-name>', |
| 106 | + help='New role name', |
| 107 | + ) |
| 108 | + return parser |
| 109 | + |
| 110 | + def take_action(self, parsed_args): |
| 111 | + self.log.debug('take_action(%s)' % parsed_args) |
| 112 | + identity_client = self.app.client_manager.identity |
| 113 | + role_id = utils.find_resource(identity_client.roles, |
| 114 | + parsed_args.role) |
| 115 | + |
| 116 | + if not parsed_args.name: |
| 117 | + sys.stdout.write("Role not updated, no arguments present") |
| 118 | + return |
| 119 | + |
| 120 | + identity_client.roles.update(role_id, parsed_args.name) |
| 121 | + return |
| 122 | + |
| 123 | + |
| 124 | +class ShowRole(show.ShowOne): |
| 125 | + """Show single role""" |
| 126 | + |
| 127 | + api = 'identity' |
| 128 | + log = logging.getLogger(__name__ + '.ShowRole') |
| 129 | + |
| 130 | + def get_parser(self, prog_name): |
| 131 | + parser = super(ShowRole, self).get_parser(prog_name) |
| 132 | + parser.add_argument( |
| 133 | + 'role', |
| 134 | + metavar='<role>', |
| 135 | + help='Name or ID of role to display') |
| 136 | + return parser |
| 137 | + |
| 138 | + def take_action(self, parsed_args): |
| 139 | + self.log.debug('take_action(%s)' % parsed_args) |
| 140 | + identity_client = self.app.client_manager.identity |
| 141 | + role = utils.find_resource(identity_client.roles, |
| 142 | + parsed_args.role) |
| 143 | + |
| 144 | + return zip(*sorted(role._info.iteritems())) |
0 commit comments