|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +"""Identity v3 EC2 Credentials action implementations""" |
| 14 | + |
| 15 | +import logging |
| 16 | +import six |
| 17 | + |
| 18 | +from cliff import command |
| 19 | +from cliff import lister |
| 20 | +from cliff import show |
| 21 | + |
| 22 | +from openstackclient.common import utils |
| 23 | +from openstackclient.i18n import _ # noqa |
| 24 | + |
| 25 | + |
| 26 | +class CreateEC2Creds(show.ShowOne): |
| 27 | + """Create EC2 credentials""" |
| 28 | + |
| 29 | + log = logging.getLogger(__name__ + ".CreateEC2Creds") |
| 30 | + |
| 31 | + def get_parser(self, prog_name): |
| 32 | + parser = super(CreateEC2Creds, self).get_parser(prog_name) |
| 33 | + parser.add_argument( |
| 34 | + '--project', |
| 35 | + metavar='<project>', |
| 36 | + help=_('Specify an alternate project' |
| 37 | + ' (default: current authenticated project)'), |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + '--user', |
| 41 | + metavar='<user>', |
| 42 | + help=_('Specify an alternate user' |
| 43 | + ' (default: current authenticated user)'), |
| 44 | + ) |
| 45 | + return parser |
| 46 | + |
| 47 | + def take_action(self, parsed_args): |
| 48 | + self.log.debug('take_action(%s)', parsed_args) |
| 49 | + identity_client = self.app.client_manager.identity |
| 50 | + |
| 51 | + if parsed_args.project: |
| 52 | + project = utils.find_resource( |
| 53 | + identity_client.projects, |
| 54 | + parsed_args.project, |
| 55 | + ).id |
| 56 | + else: |
| 57 | + # Get the project from the current auth |
| 58 | + project = self.app.client_manager.auth_ref.project_id |
| 59 | + if parsed_args.user: |
| 60 | + user = utils.find_resource( |
| 61 | + identity_client.users, |
| 62 | + parsed_args.user, |
| 63 | + ).id |
| 64 | + else: |
| 65 | + # Get the user from the current auth |
| 66 | + user = self.app.client_manager.auth_ref.user_id |
| 67 | + |
| 68 | + creds = identity_client.ec2.create(user, project) |
| 69 | + |
| 70 | + info = {} |
| 71 | + info.update(creds._info) |
| 72 | + |
| 73 | + if 'tenant_id' in info: |
| 74 | + info.update( |
| 75 | + {'project_id': info.pop('tenant_id')} |
| 76 | + ) |
| 77 | + |
| 78 | + return zip(*sorted(six.iteritems(info))) |
| 79 | + |
| 80 | + |
| 81 | +class DeleteEC2Creds(command.Command): |
| 82 | + """Delete EC2 credentials""" |
| 83 | + |
| 84 | + log = logging.getLogger(__name__ + '.DeleteEC2Creds') |
| 85 | + |
| 86 | + def get_parser(self, prog_name): |
| 87 | + parser = super(DeleteEC2Creds, self).get_parser(prog_name) |
| 88 | + parser.add_argument( |
| 89 | + 'access_key', |
| 90 | + metavar='<access-key>', |
| 91 | + help=_('Credentials access key'), |
| 92 | + ) |
| 93 | + parser.add_argument( |
| 94 | + '--user', |
| 95 | + metavar='<user>', |
| 96 | + help=_('Specify a user'), |
| 97 | + ) |
| 98 | + return parser |
| 99 | + |
| 100 | + def take_action(self, parsed_args): |
| 101 | + self.log.debug('take_action(%s)', parsed_args) |
| 102 | + identity_client = self.app.client_manager.identity |
| 103 | + |
| 104 | + if parsed_args.user: |
| 105 | + user = utils.find_resource( |
| 106 | + identity_client.users, |
| 107 | + parsed_args.user, |
| 108 | + ).id |
| 109 | + else: |
| 110 | + # Get the user from the current auth |
| 111 | + user = self.app.client_manager.auth_ref.user_id |
| 112 | + |
| 113 | + identity_client.ec2.delete(user, parsed_args.access_key) |
| 114 | + |
| 115 | + |
| 116 | +class ListEC2Creds(lister.Lister): |
| 117 | + """List EC2 credentials""" |
| 118 | + |
| 119 | + log = logging.getLogger(__name__ + '.ListEC2Creds') |
| 120 | + |
| 121 | + def get_parser(self, prog_name): |
| 122 | + parser = super(ListEC2Creds, self).get_parser(prog_name) |
| 123 | + parser.add_argument( |
| 124 | + '--user', |
| 125 | + metavar='<user>', |
| 126 | + help=_('Specify a user'), |
| 127 | + ) |
| 128 | + return parser |
| 129 | + |
| 130 | + def take_action(self, parsed_args): |
| 131 | + self.log.debug('take_action(%s)', parsed_args) |
| 132 | + identity_client = self.app.client_manager.identity |
| 133 | + |
| 134 | + if parsed_args.user: |
| 135 | + user = utils.find_resource( |
| 136 | + identity_client.users, |
| 137 | + parsed_args.user, |
| 138 | + ).id |
| 139 | + else: |
| 140 | + # Get the user from the current auth |
| 141 | + user = self.app.client_manager.auth_ref.user_id |
| 142 | + |
| 143 | + columns = ('access', 'secret', 'tenant_id', 'user_id') |
| 144 | + column_headers = ('Access', 'Secret', 'Project ID', 'User ID') |
| 145 | + data = identity_client.ec2.list(user) |
| 146 | + |
| 147 | + return (column_headers, |
| 148 | + (utils.get_item_properties( |
| 149 | + s, columns, |
| 150 | + formatters={}, |
| 151 | + ) for s in data)) |
| 152 | + |
| 153 | + |
| 154 | +class ShowEC2Creds(show.ShowOne): |
| 155 | + """Display EC2 credentials details""" |
| 156 | + |
| 157 | + log = logging.getLogger(__name__ + '.ShowEC2Creds') |
| 158 | + |
| 159 | + def get_parser(self, prog_name): |
| 160 | + parser = super(ShowEC2Creds, self).get_parser(prog_name) |
| 161 | + parser.add_argument( |
| 162 | + 'access_key', |
| 163 | + metavar='<access-key>', |
| 164 | + help=_('Credentials access key'), |
| 165 | + ) |
| 166 | + parser.add_argument( |
| 167 | + '--user', |
| 168 | + metavar='<user>', |
| 169 | + help=_('Specify a user'), |
| 170 | + ) |
| 171 | + return parser |
| 172 | + |
| 173 | + def take_action(self, parsed_args): |
| 174 | + self.log.debug('take_action(%s)', parsed_args) |
| 175 | + identity_client = self.app.client_manager.identity |
| 176 | + |
| 177 | + if parsed_args.user: |
| 178 | + user = utils.find_resource( |
| 179 | + identity_client.users, |
| 180 | + parsed_args.user, |
| 181 | + ).id |
| 182 | + else: |
| 183 | + # Get the user from the current auth |
| 184 | + user = self.app.client_manager.auth_ref.user_id |
| 185 | + |
| 186 | + creds = identity_client.ec2.get(user, parsed_args.access_key) |
| 187 | + |
| 188 | + info = {} |
| 189 | + info.update(creds._info) |
| 190 | + |
| 191 | + if 'tenant_id' in info: |
| 192 | + info.update( |
| 193 | + {'project_id': info.pop('tenant_id')} |
| 194 | + ) |
| 195 | + |
| 196 | + return zip(*sorted(six.iteritems(info))) |
0 commit comments