Implement "address scope create" command
This patch supports creating a new address scope, with --ip-version,--project,--project-domain and --share or --no-share options. Change-Id: I37c73391a41ac239dd72d55dbc0adbebd7701f4a Partial-Bug: #1566269
This commit is contained in:
parent
4639148b1d
commit
98bee08e0f
48
doc/source/command-objects/address-scope.rst
Normal file
48
doc/source/command-objects/address-scope.rst
Normal file
@ -0,0 +1,48 @@
|
||||
=============
|
||||
address scope
|
||||
=============
|
||||
|
||||
An **address scope** is a scope of IPv4 or IPv6 addresses that belongs
|
||||
to a given project and may be shared between projects.
|
||||
|
||||
Network v2
|
||||
|
||||
address scope create
|
||||
--------------------
|
||||
|
||||
Create new address scope
|
||||
|
||||
.. program:: address scope create
|
||||
.. code:: bash
|
||||
|
||||
os address scope create
|
||||
[--project <project> [--project-domain <project-domain>]]
|
||||
[--ip-version <ip-version>]
|
||||
[--share | --no-share]
|
||||
<name>
|
||||
|
||||
.. option:: --project <project>
|
||||
|
||||
Owner's project (name or ID)
|
||||
|
||||
.. option:: --project-domain <project-domain>
|
||||
|
||||
Domain the project belongs to (name or ID).
|
||||
This can be used in case collisions between project names exist.
|
||||
|
||||
.. option:: --ip-version <ip-version>
|
||||
|
||||
IP version (4 or 6, default is 4)
|
||||
|
||||
.. option:: --share
|
||||
|
||||
Share the address scope between projects
|
||||
|
||||
.. option:: --no-share
|
||||
|
||||
Do not share the address scope between projects (default)
|
||||
|
||||
.. _address_scope_create-name:
|
||||
.. describe:: <name>
|
||||
|
||||
New address scope name
|
@ -70,6 +70,7 @@ the API resources will be merged, as in the ``quota`` object that has options
|
||||
referring to both Compute and Volume quotas.
|
||||
|
||||
* ``access token``: (**Identity**) long-lived OAuth-based token
|
||||
* ``address scope``: (**Network**) a scope of IPv4 or IPv6 addresses
|
||||
* ``aggregate``: (**Compute**) a grouping of compute hosts
|
||||
* ``availability zone``: (**Compute**, **Network**, **Volume**) a logical partition of hosts or block storage or network services
|
||||
* ``backup``: (**Volume**) a volume copy
|
||||
|
96
openstackclient/network/v2/address_scope.py
Normal file
96
openstackclient/network/v2/address_scope.py
Normal file
@ -0,0 +1,96 @@
|
||||
# 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.
|
||||
#
|
||||
|
||||
"""Address scope action implementations"""
|
||||
|
||||
from openstackclient.common import command
|
||||
from openstackclient.common import utils
|
||||
from openstackclient.i18n import _
|
||||
from openstackclient.identity import common as identity_common
|
||||
|
||||
|
||||
def _get_columns(item):
|
||||
columns = list(item.keys())
|
||||
if 'tenant_id' in columns:
|
||||
columns.remove('tenant_id')
|
||||
columns.append('project_id')
|
||||
|
||||
return tuple(sorted(columns))
|
||||
|
||||
|
||||
def _get_attrs(client_manager, parsed_args):
|
||||
attrs = {}
|
||||
attrs['name'] = parsed_args.name
|
||||
attrs['ip_version'] = parsed_args.ip_version
|
||||
if parsed_args.share:
|
||||
attrs['shared'] = True
|
||||
if parsed_args.no_share:
|
||||
attrs['shared'] = False
|
||||
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
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class CreateAddressScope(command.ShowOne):
|
||||
"""Create a new Address Scope"""
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(CreateAddressScope, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'name',
|
||||
metavar="<name>",
|
||||
help=_("New address scope name")
|
||||
)
|
||||
parser.add_argument(
|
||||
'--ip-version',
|
||||
type=int,
|
||||
default=4,
|
||||
choices=[4, 6],
|
||||
help=_("IP version (default is 4)")
|
||||
)
|
||||
parser.add_argument(
|
||||
'--project',
|
||||
metavar="<project>",
|
||||
help=_("Owner's project (name or ID)")
|
||||
)
|
||||
identity_common.add_project_domain_option_to_parser(parser)
|
||||
|
||||
share_group = parser.add_mutually_exclusive_group()
|
||||
share_group.add_argument(
|
||||
'--share',
|
||||
action='store_true',
|
||||
help=_('Share the address scope between projects')
|
||||
)
|
||||
share_group.add_argument(
|
||||
'--no-share',
|
||||
action='store_true',
|
||||
help=_('Do not share the address scope between projects (default)')
|
||||
)
|
||||
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
client = self.app.client_manager.network
|
||||
attrs = _get_attrs(self.app.client_manager, parsed_args)
|
||||
obj = client.create_address_scope(**attrs)
|
||||
columns = _get_columns(obj)
|
||||
data = utils.get_item_properties(obj, columns, formatters={})
|
||||
|
||||
return columns, data
|
164
openstackclient/tests/network/v2/test_address_scope.py
Normal file
164
openstackclient/tests/network/v2/test_address_scope.py
Normal file
@ -0,0 +1,164 @@
|
||||
# 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.
|
||||
#
|
||||
|
||||
import copy
|
||||
import mock
|
||||
|
||||
from openstackclient.network.v2 import address_scope
|
||||
from openstackclient.tests import fakes
|
||||
from openstackclient.tests.identity.v3 import fakes as identity_fakes_v3
|
||||
from openstackclient.tests.network.v2 import fakes as network_fakes
|
||||
from openstackclient.tests import utils as tests_utils
|
||||
|
||||
|
||||
class TestAddressScope(network_fakes.TestNetworkV2):
|
||||
|
||||
def setUp(self):
|
||||
super(TestAddressScope, self).setUp()
|
||||
|
||||
# Get a shortcut to the network client
|
||||
self.network = self.app.client_manager.network
|
||||
|
||||
|
||||
class TestCreateAddressScope(TestAddressScope):
|
||||
|
||||
# The new address scope created.
|
||||
new_address_scope = (
|
||||
network_fakes.FakeAddressScope.create_one_address_scope(
|
||||
attrs={
|
||||
'tenant_id': identity_fakes_v3.project_id,
|
||||
}
|
||||
))
|
||||
columns = (
|
||||
'id',
|
||||
'ip_version',
|
||||
'name',
|
||||
'project_id',
|
||||
'shared'
|
||||
)
|
||||
data = (
|
||||
new_address_scope.id,
|
||||
new_address_scope.ip_version,
|
||||
new_address_scope.name,
|
||||
new_address_scope.project_id,
|
||||
new_address_scope.shared,
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super(TestCreateAddressScope, self).setUp()
|
||||
self.network.create_address_scope = mock.Mock(
|
||||
return_value=self.new_address_scope)
|
||||
|
||||
# Get the command object to test
|
||||
self.cmd = address_scope.CreateAddressScope(self.app, self.namespace)
|
||||
|
||||
# Set identity client v3. And get a shortcut to Identity client.
|
||||
identity_client = identity_fakes_v3.FakeIdentityv3Client(
|
||||
endpoint=fakes.AUTH_URL,
|
||||
token=fakes.AUTH_TOKEN,
|
||||
)
|
||||
self.app.client_manager.identity = identity_client
|
||||
self.identity = self.app.client_manager.identity
|
||||
|
||||
# Get a shortcut to the ProjectManager Mock
|
||||
self.projects_mock = self.identity.projects
|
||||
self.projects_mock.get.return_value = fakes.FakeResource(
|
||||
None,
|
||||
copy.deepcopy(identity_fakes_v3.PROJECT),
|
||||
loaded=True,
|
||||
)
|
||||
|
||||
# Get a shortcut to the DomainManager Mock
|
||||
self.domains_mock = self.identity.domains
|
||||
self.domains_mock.get.return_value = fakes.FakeResource(
|
||||
None,
|
||||
copy.deepcopy(identity_fakes_v3.DOMAIN),
|
||||
loaded=True,
|
||||
)
|
||||
|
||||
def test_create_no_options(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
|
||||
# Missing required args should bail here
|
||||
self.assertRaises(tests_utils.ParserException, self.check_parser,
|
||||
self.cmd, arglist, verifylist)
|
||||
|
||||
def test_create_default_options(self):
|
||||
arglist = [
|
||||
self.new_address_scope.name,
|
||||
]
|
||||
verifylist = [
|
||||
('project', None),
|
||||
('ip_version', self.new_address_scope.ip_version),
|
||||
('name', self.new_address_scope.name),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
columns, data = (self.cmd.take_action(parsed_args))
|
||||
|
||||
self.network.create_address_scope.assert_called_once_with(**{
|
||||
'ip_version': self.new_address_scope.ip_version,
|
||||
'name': self.new_address_scope.name,
|
||||
})
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, data)
|
||||
|
||||
def test_create_all_options(self):
|
||||
arglist = [
|
||||
'--ip-version', str(self.new_address_scope.ip_version),
|
||||
'--share',
|
||||
'--project', identity_fakes_v3.project_name,
|
||||
'--project-domain', identity_fakes_v3.domain_name,
|
||||
self.new_address_scope.name,
|
||||
]
|
||||
verifylist = [
|
||||
('ip_version', self.new_address_scope.ip_version),
|
||||
('share', True),
|
||||
('project', identity_fakes_v3.project_name),
|
||||
('project_domain', identity_fakes_v3.domain_name),
|
||||
('name', self.new_address_scope.name),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
columns, data = (self.cmd.take_action(parsed_args))
|
||||
|
||||
self.network.create_address_scope.assert_called_once_with(**{
|
||||
'ip_version': self.new_address_scope.ip_version,
|
||||
'shared': True,
|
||||
'tenant_id': identity_fakes_v3.project_id,
|
||||
'name': self.new_address_scope.name,
|
||||
})
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, data)
|
||||
|
||||
def test_create_no_share(self):
|
||||
arglist = [
|
||||
'--no-share',
|
||||
self.new_address_scope.name,
|
||||
]
|
||||
verifylist = [
|
||||
('no_share', True),
|
||||
('name', self.new_address_scope.name),
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
columns, data = self.cmd.take_action(parsed_args)
|
||||
|
||||
self.network.create_address_scope.assert_called_once_with(**{
|
||||
'ip_version': self.new_address_scope.ip_version,
|
||||
'shared': False,
|
||||
'name': self.new_address_scope.name,
|
||||
})
|
||||
self.assertEqual(self.columns, columns)
|
||||
self.assertEqual(self.data, data)
|
@ -319,6 +319,8 @@ openstack.image.v2 =
|
||||
image_set = openstackclient.image.v2.image:SetImage
|
||||
|
||||
openstack.network.v2 =
|
||||
address_scope_create = openstackclient.network.v2.address_scope:CreateAddressScope
|
||||
|
||||
ip_floating_create = openstackclient.network.v2.floating_ip:CreateFloatingIP
|
||||
ip_floating_delete = openstackclient.network.v2.floating_ip:DeleteFloatingIP
|
||||
ip_floating_list = openstackclient.network.v2.floating_ip:ListFloatingIP
|
||||
|
Loading…
x
Reference in New Issue
Block a user