|
| 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 OAuth 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 AuthorizeRequestToken(show.ShowOne): |
| 29 | + """Authorize request token command""" |
| 30 | + |
| 31 | + api = 'identity' |
| 32 | + log = logging.getLogger(__name__ + '.AuthorizeRequestToken') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(AuthorizeRequestToken, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + '--request-key', |
| 38 | + metavar='<request-key>', |
| 39 | + help='Consumer key', |
| 40 | + required=True |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + '--user-token', |
| 44 | + metavar='<user-token>', |
| 45 | + help='Token of authorizing user', |
| 46 | + required=True |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + '--roles', |
| 50 | + metavar='<roles>', |
| 51 | + help='Role to authorize', |
| 52 | + required=True |
| 53 | + ) |
| 54 | + return parser |
| 55 | + |
| 56 | + def take_action(self, parsed_args): |
| 57 | + self.log.debug('take_action(%s)' % parsed_args) |
| 58 | + oauth_client = self.app.client_manager.identity.oauths |
| 59 | + |
| 60 | + verifier_pin = oauth_client.authorize_request_token( |
| 61 | + parsed_args.request_key, parsed_args.user_token, |
| 62 | + parsed_args.roles) |
| 63 | + info = {} |
| 64 | + info.update(verifier_pin._info) |
| 65 | + return zip(*sorted(info.iteritems())) |
| 66 | + |
| 67 | + |
| 68 | +class CreateAccessToken(show.ShowOne): |
| 69 | + """Create access token command""" |
| 70 | + |
| 71 | + api = 'identity' |
| 72 | + log = logging.getLogger(__name__ + '.CreateAccessToken') |
| 73 | + |
| 74 | + def get_parser(self, prog_name): |
| 75 | + parser = super(CreateAccessToken, self).get_parser(prog_name) |
| 76 | + parser.add_argument( |
| 77 | + '--consumer-key', |
| 78 | + metavar='<consumer-key>', |
| 79 | + help='Consumer key', |
| 80 | + required=True |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + '--request-key', |
| 84 | + metavar='<request-key>', |
| 85 | + help='Consumer key', |
| 86 | + required=True |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + '--verifier', |
| 90 | + metavar='<verifier>', |
| 91 | + help='Verifier Pin', |
| 92 | + required=True |
| 93 | + ) |
| 94 | + return parser |
| 95 | + |
| 96 | + def take_action(self, parsed_args): |
| 97 | + self.log.debug('take_action(%s)' % parsed_args) |
| 98 | + oauth_client = self.app.client_manager.identity.oauths |
| 99 | + access_token = oauth_client.create_access_token( |
| 100 | + parsed_args.consumer_key, parsed_args.request_key, |
| 101 | + parsed_args.verifier) |
| 102 | + info = {} |
| 103 | + info.update(access_token._info) |
| 104 | + return zip(*sorted(info.iteritems())) |
| 105 | + |
| 106 | + |
| 107 | +class CreateConsumer(show.ShowOne): |
| 108 | + """Create consumer command""" |
| 109 | + |
| 110 | + api = 'identity' |
| 111 | + log = logging.getLogger(__name__ + '.CreateConsumer') |
| 112 | + |
| 113 | + def get_parser(self, prog_name): |
| 114 | + parser = super(CreateConsumer, self).get_parser(prog_name) |
| 115 | + parser.add_argument( |
| 116 | + 'name', |
| 117 | + metavar='<consumer-name>', |
| 118 | + help='New consumer name', |
| 119 | + ) |
| 120 | + return parser |
| 121 | + |
| 122 | + def take_action(self, parsed_args): |
| 123 | + self.log.debug('take_action(%s)' % parsed_args) |
| 124 | + identity_client = self.app.client_manager.identity |
| 125 | + consumer = identity_client.oauths.create_consumer( |
| 126 | + parsed_args.name |
| 127 | + ) |
| 128 | + info = {} |
| 129 | + info.update(consumer._info) |
| 130 | + return zip(*sorted(info.iteritems())) |
| 131 | + |
| 132 | + |
| 133 | +class CreateRequestToken(show.ShowOne): |
| 134 | + """Create request token command""" |
| 135 | + |
| 136 | + api = 'identity' |
| 137 | + log = logging.getLogger(__name__ + '.CreateRequestToken') |
| 138 | + |
| 139 | + def get_parser(self, prog_name): |
| 140 | + parser = super(CreateRequestToken, self).get_parser(prog_name) |
| 141 | + parser.add_argument( |
| 142 | + '--consumer-key', |
| 143 | + metavar='<consumer-key>', |
| 144 | + help='Consumer key', |
| 145 | + required=True |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + '--roles', |
| 149 | + metavar='<roles>', |
| 150 | + help='Role requested', |
| 151 | + ) |
| 152 | + return parser |
| 153 | + |
| 154 | + def take_action(self, parsed_args): |
| 155 | + self.log.debug('take_action(%s)' % parsed_args) |
| 156 | + oauth_client = self.app.client_manager.identity.oauths |
| 157 | + request_token = oauth_client.create_request_token( |
| 158 | + parsed_args.consumer_key, parsed_args.roles) |
| 159 | + info = {} |
| 160 | + info.update(request_token._info) |
| 161 | + return zip(*sorted(info.iteritems())) |
| 162 | + |
| 163 | + |
| 164 | +class DeleteConsumer(command.Command): |
| 165 | + """Delete consumer command""" |
| 166 | + |
| 167 | + api = 'identity' |
| 168 | + log = logging.getLogger(__name__ + '.DeleteConsumer') |
| 169 | + |
| 170 | + def get_parser(self, prog_name): |
| 171 | + parser = super(DeleteConsumer, self).get_parser(prog_name) |
| 172 | + parser.add_argument( |
| 173 | + 'consumer', |
| 174 | + metavar='<consumer>', |
| 175 | + help='Name or ID of consumer to delete', |
| 176 | + ) |
| 177 | + return parser |
| 178 | + |
| 179 | + def take_action(self, parsed_args): |
| 180 | + self.log.debug('take_action(%s)' % parsed_args) |
| 181 | + identity_client = self.app.client_manager.identity |
| 182 | + consumer = utils.find_resource( |
| 183 | + identity_client.oauths, parsed_args.consumer) |
| 184 | + identity_client.oauths.delete_consumer(consumer.id) |
| 185 | + return |
| 186 | + |
| 187 | + |
| 188 | +class ListConsumer(lister.Lister): |
| 189 | + """List consumer command""" |
| 190 | + |
| 191 | + api = 'identity' |
| 192 | + log = logging.getLogger(__name__ + '.ListConsumer') |
| 193 | + |
| 194 | + def take_action(self, parsed_args): |
| 195 | + self.log.debug('take_action(%s)' % parsed_args) |
| 196 | + columns = ('ID', 'Name', 'Consumer Key', 'Consumer Secret') |
| 197 | + data = self.app.client_manager.identity.oauths.list_consumers() |
| 198 | + return (columns, |
| 199 | + (utils.get_item_properties( |
| 200 | + s, columns, |
| 201 | + formatters={}, |
| 202 | + ) for s in data)) |
| 203 | + |
| 204 | + |
| 205 | +class SetConsumer(command.Command): |
| 206 | + """Set consumer command""" |
| 207 | + |
| 208 | + api = 'identity' |
| 209 | + log = logging.getLogger(__name__ + '.SetConsumer') |
| 210 | + |
| 211 | + def get_parser(self, prog_name): |
| 212 | + parser = super(SetConsumer, self).get_parser(prog_name) |
| 213 | + parser.add_argument( |
| 214 | + 'consumer', |
| 215 | + metavar='<consumer>', |
| 216 | + help='Name or ID of consumer to change', |
| 217 | + ) |
| 218 | + parser.add_argument( |
| 219 | + '--name', |
| 220 | + metavar='<new-consumer-name>', |
| 221 | + help='New consumer name', |
| 222 | + ) |
| 223 | + return parser |
| 224 | + |
| 225 | + def take_action(self, parsed_args): |
| 226 | + self.log.debug('take_action(%s)' % parsed_args) |
| 227 | + identity_client = self.app.client_manager.identity |
| 228 | + consumer = utils.find_resource( |
| 229 | + identity_client.oauths, parsed_args.consumer) |
| 230 | + kwargs = {} |
| 231 | + if parsed_args.name: |
| 232 | + kwargs['name'] = parsed_args.name |
| 233 | + |
| 234 | + if not len(kwargs): |
| 235 | + sys.stdout.write("Consumer not updated, no arguments present") |
| 236 | + return |
| 237 | + identity_client.oauths.update_consumer(consumer.id, **kwargs) |
| 238 | + return |
| 239 | + |
| 240 | + |
| 241 | +class ShowConsumer(show.ShowOne): |
| 242 | + """Show consumer command""" |
| 243 | + |
| 244 | + api = 'identity' |
| 245 | + log = logging.getLogger(__name__ + '.ShowConsumer') |
| 246 | + |
| 247 | + def get_parser(self, prog_name): |
| 248 | + parser = super(ShowConsumer, self).get_parser(prog_name) |
| 249 | + parser.add_argument( |
| 250 | + 'consumer', |
| 251 | + metavar='<consumer>', |
| 252 | + help='Name or ID of consumer to display', |
| 253 | + ) |
| 254 | + return parser |
| 255 | + |
| 256 | + def take_action(self, parsed_args): |
| 257 | + self.log.debug('take_action(%s)' % parsed_args) |
| 258 | + identity_client = self.app.client_manager.identity |
| 259 | + consumer = utils.find_resource( |
| 260 | + identity_client.oauths, parsed_args.consumer) |
| 261 | + |
| 262 | + info = {} |
| 263 | + info.update(consumer._info) |
| 264 | + return zip(*sorted(info.iteritems())) |
0 commit comments