|
| 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 Credential 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 CreateCredential(show.ShowOne): |
| 29 | + """Create credential command""" |
| 30 | + |
| 31 | + api = 'identity' |
| 32 | + log = logging.getLogger(__name__ + '.CreateCredential') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(CreateCredential, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + 'user', |
| 38 | + metavar='<user>', |
| 39 | + help='Name or ID of user that owns the credential', |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '--type', |
| 43 | + default="cert", |
| 44 | + metavar='<type>', |
| 45 | + choices=['ec2', 'cert'], |
| 46 | + help='New credential type', |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + 'data', |
| 50 | + metavar='<data>', |
| 51 | + help='New credential data', |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + '--project', |
| 55 | + metavar='<project>', |
| 56 | + help='Project name or ID which limits the scope of the credential', |
| 57 | + ) |
| 58 | + return parser |
| 59 | + |
| 60 | + def take_action(self, parsed_args): |
| 61 | + self.log.debug('take_action(%s)' % parsed_args) |
| 62 | + identity_client = self.app.client_manager.identity |
| 63 | + user_id = utils.find_resource(identity_client.users, |
| 64 | + parsed_args.user).id |
| 65 | + if parsed_args.project: |
| 66 | + project = utils.find_resource(identity_client.projects, |
| 67 | + parsed_args.project).id |
| 68 | + else: |
| 69 | + project = None |
| 70 | + credential = identity_client.credentials.create( |
| 71 | + user_id, |
| 72 | + parsed_args.type, |
| 73 | + parsed_args.data, |
| 74 | + project=project) |
| 75 | + |
| 76 | + return zip(*sorted(credential._info.iteritems())) |
| 77 | + |
| 78 | + |
| 79 | +class DeleteCredential(command.Command): |
| 80 | + """Delete credential command""" |
| 81 | + |
| 82 | + api = 'identity' |
| 83 | + log = logging.getLogger(__name__ + '.DeleteCredential') |
| 84 | + |
| 85 | + def get_parser(self, prog_name): |
| 86 | + parser = super(DeleteCredential, self).get_parser(prog_name) |
| 87 | + parser.add_argument( |
| 88 | + 'credential', |
| 89 | + metavar='<credential-id>', |
| 90 | + help='ID of credential to delete', |
| 91 | + ) |
| 92 | + return parser |
| 93 | + |
| 94 | + def take_action(self, parsed_args): |
| 95 | + self.log.debug('take_action(%s)' % parsed_args) |
| 96 | + identity_client = self.app.client_manager.identity |
| 97 | + identity_client.credentials.delete(parsed_args.credential) |
| 98 | + return |
| 99 | + |
| 100 | + |
| 101 | +class ListCredential(lister.Lister): |
| 102 | + """List credential command""" |
| 103 | + |
| 104 | + api = 'identity' |
| 105 | + log = logging.getLogger(__name__ + '.ListCredential') |
| 106 | + |
| 107 | + def take_action(self, parsed_args): |
| 108 | + self.log.debug('take_action(%s)' % parsed_args) |
| 109 | + columns = ('ID', 'Type', 'User ID', 'Data', 'Project ID') |
| 110 | + data = self.app.client_manager.identity.credentials.list() |
| 111 | + return (columns, |
| 112 | + (utils.get_item_properties( |
| 113 | + s, columns, |
| 114 | + formatters={}, |
| 115 | + ) for s in data)) |
| 116 | + |
| 117 | + |
| 118 | +class SetCredential(command.Command): |
| 119 | + """Set credential command""" |
| 120 | + |
| 121 | + api = 'identity' |
| 122 | + log = logging.getLogger(__name__ + '.SetCredential') |
| 123 | + |
| 124 | + def get_parser(self, prog_name): |
| 125 | + parser = super(SetCredential, self).get_parser(prog_name) |
| 126 | + parser.add_argument( |
| 127 | + 'credential', |
| 128 | + metavar='<credential-id>', |
| 129 | + help='ID of credential to change', |
| 130 | + ) |
| 131 | + parser.add_argument( |
| 132 | + '--user', |
| 133 | + metavar='<user>', |
| 134 | + help='Name or ID of user that owns the credential', |
| 135 | + ) |
| 136 | + parser.add_argument( |
| 137 | + '--type', |
| 138 | + metavar='<type>', |
| 139 | + choices=['ec2', 'cert'], |
| 140 | + help='New credential type', |
| 141 | + ) |
| 142 | + parser.add_argument( |
| 143 | + '--data', |
| 144 | + metavar='<data>', |
| 145 | + help='New credential data', |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + '--project', |
| 149 | + metavar='<project>', |
| 150 | + help='Project name or ID which limits the scope of the credential', |
| 151 | + ) |
| 152 | + return parser |
| 153 | + |
| 154 | + def take_action(self, parsed_args): |
| 155 | + self.log.debug('take_action(%s)' % parsed_args) |
| 156 | + identity_client = self.app.client_manager.identity |
| 157 | + user_id = utils.find_resource(identity_client.users, |
| 158 | + parsed_args.user).id |
| 159 | + kwargs = {} |
| 160 | + if user_id: |
| 161 | + kwargs['user'] = user_id |
| 162 | + if parsed_args.type: |
| 163 | + kwargs['type'] = parsed_args.type |
| 164 | + if parsed_args.data: |
| 165 | + kwargs['data'] = parsed_args.data |
| 166 | + if parsed_args.project: |
| 167 | + project = utils.find_resource(identity_client.projects, |
| 168 | + parsed_args.project).id |
| 169 | + kwargs['project'] = project |
| 170 | + |
| 171 | + if not kwargs: |
| 172 | + sys.stdout.write("Credential not updated, no arguments present") |
| 173 | + return |
| 174 | + identity_client.credentials.update(parsed_args.credential, **kwargs) |
| 175 | + return |
| 176 | + |
| 177 | + |
| 178 | +class ShowCredential(show.ShowOne): |
| 179 | + """Show credential command""" |
| 180 | + |
| 181 | + api = 'identity' |
| 182 | + log = logging.getLogger(__name__ + '.ShowCredential') |
| 183 | + |
| 184 | + def get_parser(self, prog_name): |
| 185 | + parser = super(ShowCredential, self).get_parser(prog_name) |
| 186 | + parser.add_argument( |
| 187 | + 'credential', |
| 188 | + metavar='<credential-id>', |
| 189 | + help='ID of credential to display', |
| 190 | + ) |
| 191 | + return parser |
| 192 | + |
| 193 | + def take_action(self, parsed_args): |
| 194 | + self.log.debug('take_action(%s)' % parsed_args) |
| 195 | + identity_client = self.app.client_manager.identity |
| 196 | + credential = utils.find_resource(identity_client.credentials, |
| 197 | + parsed_args.credential) |
| 198 | + |
| 199 | + return zip(*sorted(credential._info.iteritems())) |
0 commit comments