2016-02-12 13:31:15 +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.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Subnet pool action implementations"""
|
|
|
|
|
2016-05-13 16:14:09 -05:00
|
|
|
from osc_lib import utils
|
|
|
|
|
2016-02-12 13:31:15 +08:00
|
|
|
from openstackclient.common import command
|
2016-03-07 11:51:39 +08:00
|
|
|
from openstackclient.common import parseractions
|
2016-04-16 10:59:37 +08:00
|
|
|
from openstackclient.i18n import _
|
2016-03-23 11:27:37 +08:00
|
|
|
from openstackclient.identity import common as identity_common
|
2016-02-12 13:31:15 +08:00
|
|
|
|
|
|
|
|
2016-02-13 10:13:14 +08:00
|
|
|
def _get_columns(item):
|
2016-03-12 10:58:28 +08:00
|
|
|
columns = list(item.keys())
|
2016-02-13 10:13:14 +08:00
|
|
|
if 'tenant_id' in columns:
|
|
|
|
columns.remove('tenant_id')
|
|
|
|
columns.append('project_id')
|
|
|
|
return tuple(sorted(columns))
|
|
|
|
|
|
|
|
|
|
|
|
_formatters = {
|
|
|
|
'prefixes': utils.format_list,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-23 11:27:37 +08:00
|
|
|
def _get_attrs(client_manager, parsed_args):
|
2016-03-07 11:51:39 +08:00
|
|
|
attrs = {}
|
2016-03-24 19:22:07 +08:00
|
|
|
network_client = client_manager.network
|
|
|
|
|
2016-03-07 11:51:39 +08:00
|
|
|
if parsed_args.name is not None:
|
|
|
|
attrs['name'] = str(parsed_args.name)
|
|
|
|
if parsed_args.prefixes is not None:
|
|
|
|
attrs['prefixes'] = parsed_args.prefixes
|
|
|
|
if parsed_args.default_prefix_length is not None:
|
2016-03-30 13:21:45 -05:00
|
|
|
attrs['default_prefixlen'] = parsed_args.default_prefix_length
|
2016-03-07 11:51:39 +08:00
|
|
|
if parsed_args.min_prefix_length is not None:
|
2016-03-30 13:21:45 -05:00
|
|
|
attrs['min_prefixlen'] = parsed_args.min_prefix_length
|
2016-03-07 11:51:39 +08:00
|
|
|
if parsed_args.max_prefix_length is not None:
|
2016-03-30 13:21:45 -05:00
|
|
|
attrs['max_prefixlen'] = parsed_args.max_prefix_length
|
2016-03-07 11:51:39 +08:00
|
|
|
|
2016-03-24 19:22:07 +08:00
|
|
|
if parsed_args.address_scope is not None:
|
|
|
|
attrs['address_scope_id'] = network_client.find_address_scope(
|
|
|
|
parsed_args.address_scope, ignore_missing=False).id
|
|
|
|
if 'no_address_scope' in parsed_args and parsed_args.no_address_scope:
|
|
|
|
attrs['address_scope_id'] = None
|
|
|
|
|
2016-04-18 21:22:24 -05:00
|
|
|
if parsed_args.default:
|
|
|
|
attrs['is_default'] = True
|
|
|
|
if parsed_args.no_default:
|
|
|
|
attrs['is_default'] = False
|
|
|
|
|
|
|
|
if 'share' in parsed_args and parsed_args.share:
|
|
|
|
attrs['shared'] = True
|
|
|
|
if 'no_share' in parsed_args and parsed_args.no_share:
|
|
|
|
attrs['shared'] = False
|
|
|
|
|
2016-03-23 11:27:37 +08:00
|
|
|
# "subnet pool set" command doesn't support setting project.
|
|
|
|
if 'project' in parsed_args and parsed_args.project is not None:
|
|
|
|
identity_client = client_manager.identity
|
|
|
|
project_id = identity_common.find_project(
|
|
|
|
identity_client,
|
|
|
|
parsed_args.project,
|
|
|
|
parsed_args.project_domain,
|
|
|
|
).id
|
|
|
|
attrs['tenant_id'] = project_id
|
|
|
|
|
2016-03-07 11:51:39 +08:00
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
2016-04-25 12:22:14 +00:00
|
|
|
def _add_prefix_options(parser, for_create=False):
|
2016-03-07 11:51:39 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--pool-prefix',
|
|
|
|
metavar='<pool-prefix>',
|
|
|
|
dest='prefixes',
|
|
|
|
action='append',
|
2016-04-25 12:22:14 +00:00
|
|
|
required=for_create,
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set subnet pool prefixes (in CIDR notation) "
|
|
|
|
"(repeat option to set multiple prefixes)")
|
2016-03-07 11:51:39 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--default-prefix-length',
|
|
|
|
metavar='<default-prefix-length>',
|
2016-04-28 15:15:57 -04:00
|
|
|
type=int,
|
2016-03-07 11:51:39 +08:00
|
|
|
action=parseractions.NonNegativeAction,
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set subnet pool default prefix length")
|
2016-03-07 11:51:39 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--min-prefix-length',
|
|
|
|
metavar='<min-prefix-length>',
|
|
|
|
action=parseractions.NonNegativeAction,
|
2016-04-28 15:15:57 -04:00
|
|
|
type=int,
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set subnet pool minimum prefix length")
|
2016-03-07 11:51:39 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--max-prefix-length',
|
|
|
|
metavar='<max-prefix-length>',
|
2016-04-28 15:15:57 -04:00
|
|
|
type=int,
|
2016-03-07 11:51:39 +08:00
|
|
|
action=parseractions.NonNegativeAction,
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set subnet pool maximum prefix length")
|
2016-03-07 11:51:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-04-18 21:22:24 -05:00
|
|
|
def _add_default_options(parser):
|
|
|
|
default_group = parser.add_mutually_exclusive_group()
|
|
|
|
default_group.add_argument(
|
|
|
|
'--default',
|
|
|
|
action='store_true',
|
|
|
|
help=_("Set this as a default subnet pool"),
|
|
|
|
)
|
|
|
|
default_group.add_argument(
|
|
|
|
'--no-default',
|
|
|
|
action='store_true',
|
|
|
|
help=_("Set this as a non-default subnet pool"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-03-07 20:48:03 +08:00
|
|
|
class CreateSubnetPool(command.ShowOne):
|
|
|
|
"""Create subnet pool"""
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateSubnetPool, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'name',
|
2016-03-23 11:27:37 +08:00
|
|
|
metavar='<name>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Name of the new subnet pool")
|
2016-03-07 20:48:03 +08:00
|
|
|
)
|
2016-04-25 12:22:14 +00:00
|
|
|
_add_prefix_options(parser, for_create=True)
|
2016-03-23 11:27:37 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Owner's project (name or ID)")
|
2016-03-23 11:27:37 +08:00
|
|
|
)
|
|
|
|
identity_common.add_project_domain_option_to_parser(parser)
|
2016-03-24 19:22:07 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--address-scope',
|
|
|
|
metavar='<address-scope>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set address scope associated with the subnet pool "
|
|
|
|
"(name or ID), prefixes must be unique across address "
|
|
|
|
"scopes")
|
2016-03-24 19:22:07 +08:00
|
|
|
)
|
2016-04-18 21:22:24 -05:00
|
|
|
_add_default_options(parser)
|
|
|
|
shared_group = parser.add_mutually_exclusive_group()
|
|
|
|
shared_group.add_argument(
|
|
|
|
'--share',
|
|
|
|
action='store_true',
|
|
|
|
help=_("Set this subnet pool as shared"),
|
|
|
|
)
|
|
|
|
shared_group.add_argument(
|
|
|
|
'--no-share',
|
|
|
|
action='store_true',
|
|
|
|
help=_("Set this subnet pool as not shared"),
|
|
|
|
)
|
2016-03-07 20:48:03 +08:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = self.app.client_manager.network
|
2016-03-23 11:27:37 +08:00
|
|
|
attrs = _get_attrs(self.app.client_manager, parsed_args)
|
2016-03-31 17:33:32 +09:00
|
|
|
# NeutronServer expects prefixes to be a List
|
|
|
|
if "prefixes" not in attrs:
|
|
|
|
attrs['prefixes'] = []
|
2016-03-07 20:48:03 +08:00
|
|
|
obj = client.create_subnet_pool(**attrs)
|
|
|
|
columns = _get_columns(obj)
|
|
|
|
data = utils.get_item_properties(obj, columns, formatters=_formatters)
|
|
|
|
return (columns, data)
|
|
|
|
|
|
|
|
|
2016-02-12 13:31:15 +08:00
|
|
|
class DeleteSubnetPool(command.Command):
|
|
|
|
"""Delete subnet pool"""
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteSubnetPool, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'subnet_pool',
|
2016-03-07 11:51:39 +08:00
|
|
|
metavar='<subnet-pool>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Subnet pool to delete (name or ID)")
|
2016-02-12 13:31:15 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = self.app.client_manager.network
|
|
|
|
obj = client.find_subnet_pool(parsed_args.subnet_pool)
|
|
|
|
client.delete_subnet_pool(obj)
|
2016-02-13 09:49:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ListSubnetPool(command.Lister):
|
|
|
|
"""List subnet pools"""
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListSubnetPool, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'--long',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("List additional fields in output")
|
2016-02-13 09:49:46 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
data = self.app.client_manager.network.subnet_pools()
|
|
|
|
|
|
|
|
if parsed_args.long:
|
|
|
|
headers = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Prefixes',
|
|
|
|
'Default Prefix Length',
|
|
|
|
'Address Scope',
|
2016-04-18 21:22:24 -05:00
|
|
|
'Default Subnet Pool',
|
|
|
|
'Shared',
|
2016-02-13 09:49:46 +08:00
|
|
|
)
|
|
|
|
columns = (
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'prefixes',
|
|
|
|
'default_prefixlen',
|
|
|
|
'address_scope_id',
|
2016-04-18 21:22:24 -05:00
|
|
|
'is_default',
|
|
|
|
'shared',
|
2016-02-13 09:49:46 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
headers = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Prefixes',
|
|
|
|
)
|
|
|
|
columns = (
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'prefixes',
|
|
|
|
)
|
|
|
|
|
|
|
|
return (headers,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
2016-04-12 12:42:13 -05:00
|
|
|
formatters=_formatters,
|
2016-02-13 09:49:46 +08:00
|
|
|
) for s in data))
|
2016-02-13 10:13:14 +08:00
|
|
|
|
|
|
|
|
2016-03-07 11:51:39 +08:00
|
|
|
class SetSubnetPool(command.Command):
|
|
|
|
"""Set subnet pool properties"""
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(SetSubnetPool, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'subnet_pool',
|
|
|
|
metavar='<subnet-pool>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Subnet pool to modify (name or ID)")
|
2016-03-07 11:51:39 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--name',
|
|
|
|
metavar='<name>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set subnet pool name")
|
2016-03-07 11:51:39 +08:00
|
|
|
)
|
|
|
|
_add_prefix_options(parser)
|
2016-03-24 19:22:07 +08:00
|
|
|
address_scope_group = parser.add_mutually_exclusive_group()
|
|
|
|
address_scope_group.add_argument(
|
|
|
|
'--address-scope',
|
|
|
|
metavar='<address-scope>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Set address scope associated with the subnet pool "
|
|
|
|
"(name or ID), prefixes must be unique across address "
|
|
|
|
"scopes")
|
2016-03-24 19:22:07 +08:00
|
|
|
)
|
|
|
|
address_scope_group.add_argument(
|
|
|
|
'--no-address-scope',
|
|
|
|
action='store_true',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Remove address scope associated with the subnet pool")
|
2016-03-24 19:22:07 +08:00
|
|
|
)
|
2016-04-18 21:22:24 -05:00
|
|
|
_add_default_options(parser)
|
|
|
|
|
2016-03-07 11:51:39 +08:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = self.app.client_manager.network
|
|
|
|
obj = client.find_subnet_pool(parsed_args.subnet_pool,
|
|
|
|
ignore_missing=False)
|
|
|
|
|
2016-03-23 11:27:37 +08:00
|
|
|
attrs = _get_attrs(self.app.client_manager, parsed_args)
|
2016-03-07 11:51:39 +08:00
|
|
|
|
|
|
|
# Existing prefixes must be a subset of the new prefixes.
|
|
|
|
if 'prefixes' in attrs:
|
|
|
|
attrs['prefixes'].extend(obj.prefixes)
|
|
|
|
|
|
|
|
client.update_subnet_pool(obj, **attrs)
|
|
|
|
|
|
|
|
|
2016-02-13 10:13:14 +08:00
|
|
|
class ShowSubnetPool(command.ShowOne):
|
2016-02-20 15:01:07 +08:00
|
|
|
"""Display subnet pool details"""
|
2016-02-13 10:13:14 +08:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowSubnetPool, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'subnet_pool',
|
2016-03-07 11:51:39 +08:00
|
|
|
metavar='<subnet-pool>',
|
2016-04-16 10:59:37 +08:00
|
|
|
help=_("Subnet pool to display (name or ID)")
|
2016-02-13 10:13:14 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
client = self.app.client_manager.network
|
|
|
|
obj = client.find_subnet_pool(
|
|
|
|
parsed_args.subnet_pool,
|
|
|
|
ignore_missing=False
|
|
|
|
)
|
|
|
|
columns = _get_columns(obj)
|
|
|
|
data = utils.get_item_properties(obj, columns, formatters=_formatters)
|
|
|
|
return (columns, data)
|