|
| 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 | +""" |
| 17 | +Group action implementations |
| 18 | +""" |
| 19 | + |
| 20 | +import logging |
| 21 | + |
| 22 | +from cliff import command |
| 23 | +from cliff import lister |
| 24 | +from cliff import show |
| 25 | + |
| 26 | +from openstackclient.common import utils |
| 27 | + |
| 28 | + |
| 29 | +class CreateGroup(show.ShowOne): |
| 30 | + """Create group command""" |
| 31 | + |
| 32 | + api = 'identity' |
| 33 | + log = logging.getLogger(__name__ + '.CreateGroup') |
| 34 | + |
| 35 | + def get_parser(self, prog_name): |
| 36 | + parser = super(CreateGroup, self).get_parser(prog_name) |
| 37 | + parser.add_argument( |
| 38 | + 'name', |
| 39 | + metavar='<group-name>', |
| 40 | + help='New group name', |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + '--description', |
| 44 | + metavar='<group-description>', |
| 45 | + help='New group description', |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + '--domain', |
| 49 | + metavar='<group-domain>', |
| 50 | + help='References the domain ID or name which owns the group', |
| 51 | + ) |
| 52 | + |
| 53 | + return parser |
| 54 | + |
| 55 | + def take_action(self, parsed_args): |
| 56 | + self.log.debug('take_action(%s)' % parsed_args) |
| 57 | + identity_client = self.app.client_manager.identity |
| 58 | + if parsed_args.domain: |
| 59 | + domain = utils.find_resource( |
| 60 | + identity_client.domains, parsed_args.domain).id |
| 61 | + else: |
| 62 | + domain = None |
| 63 | + group = identity_client.groups.create( |
| 64 | + parsed_args.name, |
| 65 | + domain=domain, |
| 66 | + description=parsed_args.description, |
| 67 | + ) |
| 68 | + |
| 69 | + info = {} |
| 70 | + info.update(group._info) |
| 71 | + return zip(*sorted(info.iteritems())) |
| 72 | + |
| 73 | + |
| 74 | +class DeleteGroup(command.Command): |
| 75 | + """Delete group command""" |
| 76 | + |
| 77 | + api = 'identity' |
| 78 | + log = logging.getLogger(__name__ + '.DeleteGroup') |
| 79 | + |
| 80 | + def get_parser(self, prog_name): |
| 81 | + parser = super(DeleteGroup, self).get_parser(prog_name) |
| 82 | + parser.add_argument( |
| 83 | + 'group', |
| 84 | + metavar='<group>', |
| 85 | + help='Name or ID of group to delete', |
| 86 | + ) |
| 87 | + return parser |
| 88 | + |
| 89 | + def take_action(self, parsed_args): |
| 90 | + self.log.debug('take_action(%s)' % parsed_args) |
| 91 | + identity_client = self.app.client_manager.identity |
| 92 | + group = utils.find_resource( |
| 93 | + identity_client.groups, parsed_args.group) |
| 94 | + identity_client.groups.delete(group.id) |
| 95 | + return |
| 96 | + |
| 97 | + |
| 98 | +class ListGroup(lister.Lister): |
| 99 | + """List group command""" |
| 100 | + |
| 101 | + api = 'identity' |
| 102 | + log = logging.getLogger(__name__ + '.ListGroup') |
| 103 | + |
| 104 | + def get_parser(self, prog_name): |
| 105 | + parser = super(ListGroup, self).get_parser(prog_name) |
| 106 | + parser.add_argument( |
| 107 | + '--long', |
| 108 | + action='store_true', |
| 109 | + default=False, |
| 110 | + help='Additional fields are listed in output', |
| 111 | + ) |
| 112 | + return parser |
| 113 | + |
| 114 | + def take_action(self, parsed_args): |
| 115 | + self.log.debug('take_action(%s)' % parsed_args) |
| 116 | + if parsed_args.long: |
| 117 | + columns = ('ID', 'Name', 'Domain ID', 'Description') |
| 118 | + else: |
| 119 | + columns = ('ID', 'Name') |
| 120 | + data = self.app.client_manager.identity.groups.list() |
| 121 | + return (columns, |
| 122 | + (utils.get_item_properties( |
| 123 | + s, columns, |
| 124 | + formatters={}, |
| 125 | + ) for s in data), |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +class SetGroup(command.Command): |
| 130 | + """Set group command""" |
| 131 | + |
| 132 | + api = 'identity' |
| 133 | + log = logging.getLogger(__name__ + '.SetGroup') |
| 134 | + |
| 135 | + def get_parser(self, prog_name): |
| 136 | + parser = super(SetGroup, self).get_parser(prog_name) |
| 137 | + parser.add_argument( |
| 138 | + 'group', |
| 139 | + metavar='<group>', |
| 140 | + help='Name or ID of group to change', |
| 141 | + ) |
| 142 | + parser.add_argument( |
| 143 | + '--name', |
| 144 | + metavar='<new-group-name>', |
| 145 | + help='New group name', |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + '--domain', |
| 149 | + metavar='<group-domain>', |
| 150 | + help='New domain name or ID that will now own the group', |
| 151 | + ) |
| 152 | + parser.add_argument( |
| 153 | + '--description', |
| 154 | + metavar='<group-description>', |
| 155 | + help='New group description', |
| 156 | + ) |
| 157 | + return parser |
| 158 | + |
| 159 | + def take_action(self, parsed_args): |
| 160 | + self.log.debug('take_action(%s)' % parsed_args) |
| 161 | + identity_client = self.app.client_manager.identity |
| 162 | + group = utils.find_resource( |
| 163 | + identity_client.groups, parsed_args.group) |
| 164 | + kwargs = {} |
| 165 | + if parsed_args.name: |
| 166 | + kwargs['name'] = parsed_args.name |
| 167 | + if parsed_args.description: |
| 168 | + kwargs['description'] = parsed_args.description |
| 169 | + if parsed_args.domain: |
| 170 | + domain = utils.find_resource( |
| 171 | + identity_client.domains, parsed_args.domain).id |
| 172 | + kwargs['domain'] = domain |
| 173 | + |
| 174 | + if not len(kwargs): |
| 175 | + stdout.write("Group not updated, no arguments present") |
| 176 | + return |
| 177 | + identity_client.groups.update(group.id, **kwargs) |
| 178 | + return |
| 179 | + |
| 180 | + |
| 181 | +class ShowGroup(show.ShowOne): |
| 182 | + """Show group command""" |
| 183 | + |
| 184 | + api = 'identity' |
| 185 | + log = logging.getLogger(__name__ + '.ShowGroup') |
| 186 | + |
| 187 | + def get_parser(self, prog_name): |
| 188 | + parser = super(ShowGroup, self).get_parser(prog_name) |
| 189 | + parser.add_argument( |
| 190 | + 'group', |
| 191 | + metavar='<group>', |
| 192 | + help='Name or ID of group to display', |
| 193 | + ) |
| 194 | + return parser |
| 195 | + |
| 196 | + def take_action(self, parsed_args): |
| 197 | + self.log.debug('take_action(%s)' % parsed_args) |
| 198 | + identity_client = self.app.client_manager.identity |
| 199 | + group = utils.find_resource( |
| 200 | + identity_client.groups, parsed_args.group) |
| 201 | + |
| 202 | + info = {} |
| 203 | + info.update(group._info) |
| 204 | + return zip(*sorted(info.iteritems())) |
0 commit comments