|
| 1 | +# Copyright 2013 Nebula Inc. |
| 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 | +"""EC2 Credentials action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from cliff import command |
| 21 | +from cliff import lister |
| 22 | +from cliff import show |
| 23 | + |
| 24 | +from openstackclient.common import utils |
| 25 | + |
| 26 | + |
| 27 | +class CreateEC2Creds(show.ShowOne): |
| 28 | + """Create EC2 credentials""" |
| 29 | + |
| 30 | + api = "identity" |
| 31 | + log = logging.getLogger(__name__ + ".CreateEC2Creds") |
| 32 | + |
| 33 | + def get_parser(self, prog_name): |
| 34 | + parser = super(CreateEC2Creds, self).get_parser(prog_name) |
| 35 | + parser.add_argument( |
| 36 | + '--project', |
| 37 | + metavar='<project>', |
| 38 | + help='Specify a project [admin only]', |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + '--user', |
| 42 | + metavar='<user>', |
| 43 | + help='Specify a user [admin only]', |
| 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.tenants, |
| 54 | + parsed_args.project, |
| 55 | + ).id |
| 56 | + else: |
| 57 | + # Get the project from the current auth |
| 58 | + project = identity_client.auth_tenant_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 = identity_client.auth_user_id |
| 67 | + |
| 68 | + creds = identity_client.ec2.create(user, project) |
| 69 | + |
| 70 | + info = {} |
| 71 | + info.update(creds._info) |
| 72 | + return zip(*sorted(info.iteritems())) |
| 73 | + |
| 74 | + |
| 75 | +class DeleteEC2Creds(command.Command): |
| 76 | + """Delete EC2 credentials""" |
| 77 | + |
| 78 | + api = 'identity' |
| 79 | + log = logging.getLogger(__name__ + '.DeleteEC2Creds') |
| 80 | + |
| 81 | + def get_parser(self, prog_name): |
| 82 | + parser = super(DeleteEC2Creds, self).get_parser(prog_name) |
| 83 | + parser.add_argument( |
| 84 | + 'access_key', |
| 85 | + metavar='<access-key>', |
| 86 | + help='Credentials access key', |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + '--user', |
| 90 | + metavar='<user>', |
| 91 | + help='Specify a user [admin only]', |
| 92 | + ) |
| 93 | + return parser |
| 94 | + |
| 95 | + def take_action(self, parsed_args): |
| 96 | + self.log.debug('take_action(%s)' % parsed_args) |
| 97 | + identity_client = self.app.client_manager.identity |
| 98 | + |
| 99 | + if parsed_args.user: |
| 100 | + user = utils.find_resource( |
| 101 | + identity_client.users, |
| 102 | + parsed_args.user, |
| 103 | + ).id |
| 104 | + else: |
| 105 | + # Get the user from the current auth |
| 106 | + user = identity_client.auth_user_id |
| 107 | + |
| 108 | + identity_client.ec2.delete(user, parsed_args.access_key) |
| 109 | + |
| 110 | + |
| 111 | +class ListEC2Creds(lister.Lister): |
| 112 | + """List EC2 credentials""" |
| 113 | + |
| 114 | + api = 'identity' |
| 115 | + log = logging.getLogger(__name__ + '.ListEC2Creds') |
| 116 | + |
| 117 | + def get_parser(self, prog_name): |
| 118 | + parser = super(ListEC2Creds, self).get_parser(prog_name) |
| 119 | + parser.add_argument( |
| 120 | + '--user', |
| 121 | + metavar='<user>', |
| 122 | + help='Specify a user [admin only]', |
| 123 | + ) |
| 124 | + return parser |
| 125 | + |
| 126 | + def take_action(self, parsed_args): |
| 127 | + self.log.debug('take_action(%s)' % parsed_args) |
| 128 | + identity_client = self.app.client_manager.identity |
| 129 | + |
| 130 | + if parsed_args.user: |
| 131 | + user = utils.find_resource( |
| 132 | + identity_client.users, |
| 133 | + parsed_args.user, |
| 134 | + ).id |
| 135 | + else: |
| 136 | + # Get the user from the current auth |
| 137 | + user = identity_client.auth_user_id |
| 138 | + |
| 139 | + columns = ('Access', 'Secret', 'Project ID', 'User ID') |
| 140 | + data = identity_client.ec2.list(user) |
| 141 | + return (columns, |
| 142 | + (utils.get_item_properties( |
| 143 | + s, columns, |
| 144 | + formatters={}, |
| 145 | + ) for s in data)) |
| 146 | + |
| 147 | + |
| 148 | +class ShowEC2Creds(show.ShowOne): |
| 149 | + """Show EC2 credentials""" |
| 150 | + |
| 151 | + api = 'identity' |
| 152 | + log = logging.getLogger(__name__ + '.ShowEC2Creds') |
| 153 | + |
| 154 | + def get_parser(self, prog_name): |
| 155 | + parser = super(ShowEC2Creds, self).get_parser(prog_name) |
| 156 | + parser.add_argument( |
| 157 | + 'access_key', |
| 158 | + metavar='<access-key>', |
| 159 | + help='Credentials access key', |
| 160 | + ) |
| 161 | + parser.add_argument( |
| 162 | + '--user', |
| 163 | + metavar='<user>', |
| 164 | + help='Specify a user [admin only]', |
| 165 | + ) |
| 166 | + return parser |
| 167 | + |
| 168 | + def take_action(self, parsed_args): |
| 169 | + self.log.debug('take_action(%s)' % parsed_args) |
| 170 | + identity_client = self.app.client_manager.identity |
| 171 | + |
| 172 | + if parsed_args.user: |
| 173 | + user = utils.find_resource( |
| 174 | + identity_client.users, |
| 175 | + parsed_args.user, |
| 176 | + ).id |
| 177 | + else: |
| 178 | + # Get the user from the current auth |
| 179 | + user = identity_client.auth_user_id |
| 180 | + |
| 181 | + creds = identity_client.ec2.get(user, parsed_args.access_key) |
| 182 | + |
| 183 | + info = {} |
| 184 | + info.update(creds._info) |
| 185 | + return zip(*sorted(info.iteritems())) |
0 commit comments