2014-11-03 10:31:04 +08:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Identity v3 Region action implementations"""
|
|
|
|
|
2016-07-12 12:44:55 +08:00
|
|
|
import logging
|
|
|
|
|
2016-05-13 17:27:12 -05:00
|
|
|
from osc_lib.command import command
|
2016-07-12 12:44:55 +08:00
|
|
|
from osc_lib import exceptions
|
2016-05-13 16:14:09 -05:00
|
|
|
from osc_lib import utils
|
2014-11-03 10:31:04 +08:00
|
|
|
|
2016-05-13 13:14:02 -07:00
|
|
|
from openstackclient.i18n import _
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
|
2016-07-12 12:44:55 +08:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class CreateRegion(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Create new region")
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateRegion, self).get_parser(prog_name)
|
|
|
|
# NOTE(stevemar): The API supports an optional region ID, but that
|
|
|
|
# seems like poor UX, we will only support user-defined IDs.
|
|
|
|
parser.add_argument(
|
|
|
|
'region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
|
|
|
help=_('New region ID'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--parent-region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
|
|
|
help=_('Parent region ID'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--description',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<description>',
|
2014-11-03 10:31:04 +08:00
|
|
|
help=_('New region description'),
|
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
|
|
|
|
|
|
|
region = identity_client.regions.create(
|
|
|
|
id=parsed_args.region,
|
|
|
|
parent_region=parsed_args.parent_region,
|
|
|
|
description=parsed_args.description,
|
|
|
|
)
|
|
|
|
|
|
|
|
region._info['region'] = region._info.pop('id')
|
|
|
|
region._info['parent_region'] = region._info.pop('parent_region_id')
|
|
|
|
region._info.pop('links', None)
|
2017-07-07 11:48:48 +08:00
|
|
|
return zip(*sorted(region._info.items()))
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteRegion(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Delete region(s)")
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteRegion, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
2016-07-12 12:44:55 +08:00
|
|
|
nargs='+',
|
|
|
|
help=_('Region ID(s) to delete'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
2016-07-12 12:44:55 +08:00
|
|
|
result = 0
|
|
|
|
for i in parsed_args.region:
|
|
|
|
try:
|
|
|
|
identity_client.regions.delete(i)
|
|
|
|
except Exception as e:
|
|
|
|
result += 1
|
|
|
|
LOG.error(_("Failed to delete region with "
|
2017-02-13 17:36:56 +01:00
|
|
|
"ID '%(region)s': %(e)s"), {'region': i, 'e': e})
|
2016-07-12 12:44:55 +08:00
|
|
|
|
|
|
|
if result > 0:
|
|
|
|
total = len(parsed_args.region)
|
|
|
|
msg = (_("%(result)s of %(total)s regions failed "
|
|
|
|
"to delete.") % {'result': result, 'total': total})
|
|
|
|
raise exceptions.CommandError(msg)
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ListRegion(command.Lister):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("List regions")
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListRegion, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'--parent-region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
|
|
|
help=_('Filter by parent region ID'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
|
|
|
|
|
|
|
kwargs = {}
|
|
|
|
if parsed_args.parent_region:
|
|
|
|
kwargs['parent_region_id'] = parsed_args.parent_region
|
|
|
|
|
2015-10-18 15:32:45 -04:00
|
|
|
columns_headers = ('Region', 'Parent Region', 'Description')
|
|
|
|
columns = ('ID', 'Parent Region Id', 'Description')
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
data = identity_client.regions.list(**kwargs)
|
|
|
|
return (columns_headers,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={},
|
|
|
|
) for s in data))
|
|
|
|
|
|
|
|
|
|
|
|
class SetRegion(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Set region properties")
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(SetRegion, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
2015-01-09 19:13:03 -05:00
|
|
|
help=_('Region to modify'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--parent-region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
|
|
|
help=_('New parent region ID'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--description',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<description>',
|
2014-11-03 10:31:04 +08:00
|
|
|
help=_('New region description'),
|
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
|
|
|
|
|
|
|
kwargs = {}
|
|
|
|
if parsed_args.description:
|
|
|
|
kwargs['description'] = parsed_args.description
|
|
|
|
if parsed_args.parent_region:
|
|
|
|
kwargs['parent_region'] = parsed_args.parent_region
|
|
|
|
|
|
|
|
identity_client.regions.update(parsed_args.region, **kwargs)
|
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ShowRegion(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Display region details")
|
2014-11-03 10:31:04 +08:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowRegion, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'region',
|
2014-12-31 10:05:37 -06:00
|
|
|
metavar='<region-id>',
|
2015-01-09 19:13:03 -05:00
|
|
|
help=_('Region to display'),
|
2014-11-03 10:31:04 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
|
|
|
|
|
|
|
region = utils.find_resource(identity_client.regions,
|
|
|
|
parsed_args.region)
|
|
|
|
|
|
|
|
region._info['region'] = region._info.pop('id')
|
|
|
|
region._info['parent_region'] = region._info.pop('parent_region_id')
|
|
|
|
region._info.pop('links', None)
|
2017-07-07 11:48:48 +08:00
|
|
|
return zip(*sorted(region._info.items()))
|