|
| 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 | + |
| 14 | +"""Identity v3 Region action implementations""" |
| 15 | + |
| 16 | +import logging |
| 17 | +import six |
| 18 | + |
| 19 | +from cliff import command |
| 20 | +from cliff import lister |
| 21 | +from cliff import show |
| 22 | + |
| 23 | +from openstackclient.common import utils |
| 24 | +from openstackclient.i18n import _ # noqa |
| 25 | + |
| 26 | + |
| 27 | +class CreateRegion(show.ShowOne): |
| 28 | + """Create new region""" |
| 29 | + |
| 30 | + log = logging.getLogger(__name__ + '.CreateRegion') |
| 31 | + |
| 32 | + def get_parser(self, prog_name): |
| 33 | + parser = super(CreateRegion, self).get_parser(prog_name) |
| 34 | + # NOTE(stevemar): The API supports an optional region ID, but that |
| 35 | + # seems like poor UX, we will only support user-defined IDs. |
| 36 | + parser.add_argument( |
| 37 | + 'region', |
| 38 | + metavar='<region>', |
| 39 | + help=_('New region'), |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '--parent-region', |
| 43 | + metavar='<parent-region>', |
| 44 | + help=_('The parent region of new region'), |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + '--description', |
| 48 | + metavar='<region-description>', |
| 49 | + help=_('New region description'), |
| 50 | + ) |
| 51 | + parser.add_argument( |
| 52 | + '--url', |
| 53 | + metavar='<region-url>', |
| 54 | + help=_('New region url'), |
| 55 | + ) |
| 56 | + |
| 57 | + return parser |
| 58 | + |
| 59 | + def take_action(self, parsed_args): |
| 60 | + self.log.debug('take_action(%s)', parsed_args) |
| 61 | + identity_client = self.app.client_manager.identity |
| 62 | + |
| 63 | + region = identity_client.regions.create( |
| 64 | + id=parsed_args.region, |
| 65 | + url=parsed_args.url, |
| 66 | + parent_region=parsed_args.parent_region, |
| 67 | + description=parsed_args.description, |
| 68 | + ) |
| 69 | + |
| 70 | + region._info['region'] = region._info.pop('id') |
| 71 | + region._info['parent_region'] = region._info.pop('parent_region_id') |
| 72 | + region._info.pop('links', None) |
| 73 | + return zip(*sorted(six.iteritems(region._info))) |
| 74 | + |
| 75 | + |
| 76 | +class DeleteRegion(command.Command): |
| 77 | + """Delete region""" |
| 78 | + |
| 79 | + log = logging.getLogger(__name__ + '.DeleteRegion') |
| 80 | + |
| 81 | + def get_parser(self, prog_name): |
| 82 | + parser = super(DeleteRegion, self).get_parser(prog_name) |
| 83 | + parser.add_argument( |
| 84 | + 'region', |
| 85 | + metavar='<region>', |
| 86 | + help=_('Region to delete'), |
| 87 | + ) |
| 88 | + return parser |
| 89 | + |
| 90 | + def take_action(self, parsed_args): |
| 91 | + self.log.debug('take_action(%s)', parsed_args) |
| 92 | + identity_client = self.app.client_manager.identity |
| 93 | + |
| 94 | + identity_client.regions.delete(parsed_args.region) |
| 95 | + return |
| 96 | + |
| 97 | + |
| 98 | +class ListRegion(lister.Lister): |
| 99 | + """List regions""" |
| 100 | + |
| 101 | + log = logging.getLogger(__name__ + '.ListRegion') |
| 102 | + |
| 103 | + def get_parser(self, prog_name): |
| 104 | + parser = super(ListRegion, self).get_parser(prog_name) |
| 105 | + parser.add_argument( |
| 106 | + '--parent-region', |
| 107 | + metavar='<parent-region>', |
| 108 | + help=_('Filter by parent region'), |
| 109 | + ) |
| 110 | + return parser |
| 111 | + |
| 112 | + def take_action(self, parsed_args): |
| 113 | + self.log.debug('take_action(%s)', parsed_args) |
| 114 | + identity_client = self.app.client_manager.identity |
| 115 | + |
| 116 | + kwargs = {} |
| 117 | + if parsed_args.parent_region: |
| 118 | + kwargs['parent_region_id'] = parsed_args.parent_region |
| 119 | + |
| 120 | + columns_headers = ('Region', 'Parent Region', 'Description', 'URL') |
| 121 | + columns = ('ID', 'Parent Region Id', 'Description', 'URL') |
| 122 | + |
| 123 | + data = identity_client.regions.list(**kwargs) |
| 124 | + return (columns_headers, |
| 125 | + (utils.get_item_properties( |
| 126 | + s, columns, |
| 127 | + formatters={}, |
| 128 | + ) for s in data)) |
| 129 | + |
| 130 | + |
| 131 | +class SetRegion(command.Command): |
| 132 | + """Set region properties""" |
| 133 | + |
| 134 | + log = logging.getLogger(__name__ + '.SetRegion') |
| 135 | + |
| 136 | + def get_parser(self, prog_name): |
| 137 | + parser = super(SetRegion, self).get_parser(prog_name) |
| 138 | + parser.add_argument( |
| 139 | + 'region', |
| 140 | + metavar='<region>', |
| 141 | + help=_('Region to change'), |
| 142 | + ) |
| 143 | + parser.add_argument( |
| 144 | + '--parent-region', |
| 145 | + metavar='<parent-region>', |
| 146 | + help=_('New parent region of the region'), |
| 147 | + ) |
| 148 | + parser.add_argument( |
| 149 | + '--description', |
| 150 | + metavar='<region-description>', |
| 151 | + help=_('New region description'), |
| 152 | + ) |
| 153 | + parser.add_argument( |
| 154 | + '--url', |
| 155 | + metavar='<region-url>', |
| 156 | + help=_('New region url'), |
| 157 | + ) |
| 158 | + return parser |
| 159 | + |
| 160 | + def take_action(self, parsed_args): |
| 161 | + self.log.debug('take_action(%s)', parsed_args) |
| 162 | + identity_client = self.app.client_manager.identity |
| 163 | + |
| 164 | + if (not parsed_args.url |
| 165 | + and not parsed_args.parent_region |
| 166 | + and not parsed_args.description): |
| 167 | + return |
| 168 | + |
| 169 | + kwargs = {} |
| 170 | + if parsed_args.url: |
| 171 | + kwargs['url'] = parsed_args.url |
| 172 | + if parsed_args.description: |
| 173 | + kwargs['description'] = parsed_args.description |
| 174 | + if parsed_args.parent_region: |
| 175 | + kwargs['parent_region'] = parsed_args.parent_region |
| 176 | + |
| 177 | + identity_client.regions.update(parsed_args.region, **kwargs) |
| 178 | + return |
| 179 | + |
| 180 | + |
| 181 | +class ShowRegion(show.ShowOne): |
| 182 | + """Show region""" |
| 183 | + |
| 184 | + log = logging.getLogger(__name__ + '.ShowRegion') |
| 185 | + |
| 186 | + def get_parser(self, prog_name): |
| 187 | + parser = super(ShowRegion, self).get_parser(prog_name) |
| 188 | + parser.add_argument( |
| 189 | + 'region', |
| 190 | + metavar='<region>', |
| 191 | + help=_('Region to display'), |
| 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 | + |
| 199 | + region = utils.find_resource(identity_client.regions, |
| 200 | + parsed_args.region) |
| 201 | + |
| 202 | + region._info['region'] = region._info.pop('id') |
| 203 | + region._info['parent_region'] = region._info.pop('parent_region_id') |
| 204 | + region._info.pop('links', None) |
| 205 | + return zip(*sorted(six.iteritems(region._info))) |
0 commit comments