|
| 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 Endpoint 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 CreateEndpoint(show.ShowOne): |
| 29 | + """Create endpoint command""" |
| 30 | + |
| 31 | + api = 'identity' |
| 32 | + log = logging.getLogger(__name__ + '.CreateEndpoint') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(CreateEndpoint, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + 'service', |
| 38 | + metavar='<service>', |
| 39 | + help='Name or ID of new endpoint service') |
| 40 | + parser.add_argument( |
| 41 | + 'interface', |
| 42 | + metavar='<interface>', |
| 43 | + choices=['admin', 'public', 'internal'], |
| 44 | + help='New endpoint interface, must be admin, public or internal') |
| 45 | + parser.add_argument( |
| 46 | + 'url', |
| 47 | + metavar='<url>', |
| 48 | + help='New endpoint URL') |
| 49 | + parser.add_argument( |
| 50 | + '--region', |
| 51 | + metavar='<region>', |
| 52 | + help='New endpoint region') |
| 53 | + enable_group = parser.add_mutually_exclusive_group() |
| 54 | + enable_group.add_argument( |
| 55 | + '--enable', |
| 56 | + dest='enabled', |
| 57 | + action='store_true', |
| 58 | + default=True, |
| 59 | + help='Enable user', |
| 60 | + ) |
| 61 | + enable_group.add_argument( |
| 62 | + '--disable', |
| 63 | + dest='enabled', |
| 64 | + action='store_false', |
| 65 | + help='Disable user', |
| 66 | + ) |
| 67 | + return parser |
| 68 | + |
| 69 | + def take_action(self, parsed_args): |
| 70 | + self.log.debug('take_action(%s)' % parsed_args) |
| 71 | + identity_client = self.app.client_manager.identity |
| 72 | + service = utils.find_resource(identity_client.services, |
| 73 | + parsed_args.service) |
| 74 | + |
| 75 | + endpoint = identity_client.endpoints.create( |
| 76 | + service.id, |
| 77 | + parsed_args.url, |
| 78 | + parsed_args.interface, |
| 79 | + parsed_args.region, |
| 80 | + parsed_args.enabled |
| 81 | + ) |
| 82 | + |
| 83 | + info = {} |
| 84 | + info.update(endpoint._info) |
| 85 | + info['service_name'] = service.name |
| 86 | + info['service_type'] = service.type |
| 87 | + return zip(*sorted(info.iteritems())) |
| 88 | + |
| 89 | + |
| 90 | +class DeleteEndpoint(command.Command): |
| 91 | + """Delete endpoint command""" |
| 92 | + |
| 93 | + api = 'identity' |
| 94 | + log = logging.getLogger(__name__ + '.DeleteEndpoint') |
| 95 | + |
| 96 | + def get_parser(self, prog_name): |
| 97 | + parser = super(DeleteEndpoint, self).get_parser(prog_name) |
| 98 | + parser.add_argument( |
| 99 | + 'endpoint', |
| 100 | + metavar='<endpoint>', |
| 101 | + help='ID of endpoint to delete') |
| 102 | + return parser |
| 103 | + |
| 104 | + def take_action(self, parsed_args): |
| 105 | + self.log.debug('take_action(%s)' % parsed_args) |
| 106 | + identity_client = self.app.client_manager.identity |
| 107 | + endpoint_id = utils.find_resource(identity_client.endpoints, |
| 108 | + parsed_args.endpoint).id |
| 109 | + identity_client.endpoints.delete(endpoint_id) |
| 110 | + return |
| 111 | + |
| 112 | + |
| 113 | +class ListEndpoint(lister.Lister): |
| 114 | + """List endpoint command""" |
| 115 | + |
| 116 | + api = 'identity' |
| 117 | + log = logging.getLogger(__name__ + '.ListEndpoint') |
| 118 | + |
| 119 | + def get_parser(self, prog_name): |
| 120 | + parser = super(ListEndpoint, self).get_parser(prog_name) |
| 121 | + parser.add_argument( |
| 122 | + '--long', |
| 123 | + action='store_true', |
| 124 | + default=False, |
| 125 | + help='Additional fields are listed in output') |
| 126 | + return parser |
| 127 | + |
| 128 | + def take_action(self, parsed_args): |
| 129 | + self.log.debug('take_action(%s)' % parsed_args) |
| 130 | + identity_client = self.app.client_manager.identity |
| 131 | + if parsed_args.long: |
| 132 | + columns = ('ID', 'Region', 'Service Name', 'Service Type', |
| 133 | + 'Enabled', 'Interface', 'URL') |
| 134 | + else: |
| 135 | + columns = ('ID', 'Region', 'Service Name', 'Enabled') |
| 136 | + data = identity_client.endpoints.list() |
| 137 | + |
| 138 | + for ep in data: |
| 139 | + service = utils.find_resource( |
| 140 | + identity_client.services, ep.service_id) |
| 141 | + ep.service_name = service.name |
| 142 | + ep.service_type = service.type |
| 143 | + return (columns, |
| 144 | + (utils.get_item_properties( |
| 145 | + s, columns, |
| 146 | + formatters={}, |
| 147 | + ) for s in data)) |
| 148 | + |
| 149 | + |
| 150 | +class SetEndpoint(command.Command): |
| 151 | + """Set endpoint command""" |
| 152 | + |
| 153 | + api = 'identity' |
| 154 | + log = logging.getLogger(__name__ + '.SetEndpoint') |
| 155 | + |
| 156 | + def get_parser(self, prog_name): |
| 157 | + parser = super(SetEndpoint, self).get_parser(prog_name) |
| 158 | + parser.add_argument( |
| 159 | + 'endpoint', |
| 160 | + metavar='<endpoint>', |
| 161 | + help='ID of endpoint to update') |
| 162 | + parser.add_argument( |
| 163 | + '--interface', |
| 164 | + metavar='<interface>', |
| 165 | + choices=['admin', 'public', 'internal'], |
| 166 | + help='New endpoint interface, must be admin|public|internal') |
| 167 | + parser.add_argument( |
| 168 | + '--url', |
| 169 | + metavar='<url>', |
| 170 | + help='New endpoint URL') |
| 171 | + parser.add_argument( |
| 172 | + '--service', |
| 173 | + metavar='<service>', |
| 174 | + help='Name or ID of new endpoint service') |
| 175 | + parser.add_argument( |
| 176 | + '--region', |
| 177 | + metavar='<region>', |
| 178 | + help='New endpoint region') |
| 179 | + enable_group = parser.add_mutually_exclusive_group() |
| 180 | + enable_group.add_argument( |
| 181 | + '--enable', |
| 182 | + dest='enabled', |
| 183 | + action='store_true', |
| 184 | + default=True, |
| 185 | + help='Enable user', |
| 186 | + ) |
| 187 | + enable_group.add_argument( |
| 188 | + '--disable', |
| 189 | + dest='enabled', |
| 190 | + action='store_false', |
| 191 | + help='Disable user', |
| 192 | + ) |
| 193 | + return parser |
| 194 | + |
| 195 | + def take_action(self, parsed_args): |
| 196 | + self.log.debug('take_action(%s)' % parsed_args) |
| 197 | + identity_client = self.app.client_manager.identity |
| 198 | + endpoint = utils.find_resource(identity_client.endpoints, |
| 199 | + parsed_args.endpoint) |
| 200 | + service = utils.find_resource(identity_client.services, |
| 201 | + parsed_args.service) |
| 202 | + |
| 203 | + if (not parsed_args.interface and not parsed_args.url |
| 204 | + and not parsed_args.service and not parsed_args.region): |
| 205 | + sys.stdout.write("Endpoint not updated, no arguments present") |
| 206 | + return |
| 207 | + |
| 208 | + identity_client.endpoints.update( |
| 209 | + endpoint.id, |
| 210 | + service.id, |
| 211 | + parsed_args.url, |
| 212 | + parsed_args.interface, |
| 213 | + parsed_args.region, |
| 214 | + parsed_args.enabled |
| 215 | + ) |
| 216 | + |
| 217 | + return |
| 218 | + |
| 219 | + |
| 220 | +class ShowEndpoint(show.ShowOne): |
| 221 | + """Show endpoint command""" |
| 222 | + |
| 223 | + api = 'identity' |
| 224 | + log = logging.getLogger(__name__ + '.ShowEndpoint') |
| 225 | + |
| 226 | + def get_parser(self, prog_name): |
| 227 | + parser = super(ShowEndpoint, self).get_parser(prog_name) |
| 228 | + parser.add_argument( |
| 229 | + 'endpoint', |
| 230 | + metavar='<endpoint>', |
| 231 | + help='ID of endpoint to display') |
| 232 | + return parser |
| 233 | + |
| 234 | + def take_action(self, parsed_args): |
| 235 | + self.log.debug('take_action(%s)' % parsed_args) |
| 236 | + identity_client = self.app.client_manager.identity |
| 237 | + endpoint = utils.find_resource(identity_client.endpoints, |
| 238 | + parsed_args.endpoint) |
| 239 | + |
| 240 | + service = utils.find_resource(identity_client.services, |
| 241 | + endpoint.service_id) |
| 242 | + |
| 243 | + info = {} |
| 244 | + info.update(endpoint._info) |
| 245 | + info['service_name'] = service.name |
| 246 | + info['service_type'] = service.type |
| 247 | + return zip(*sorted(info.iteritems())) |
0 commit comments