Compute: Add tag support for server add fixed ip

Change-Id: I62ed4729dead9f91630d1f568c834c9642965558
Story: 2002195
Task: 21679
This commit is contained in:
tianhui 2018-07-27 06:20:56 +00:00 committed by Stephen Finucane
parent a48c05b90a
commit 742c80a825
3 changed files with 96 additions and 5 deletions

View File

@ -236,6 +236,14 @@ class AddFixedIP(command.Command):
metavar="<ip-address>",
help=_("Requested fixed IP address"),
)
parser.add_argument(
'--tag',
metavar='<tag>',
help=_(
'Tag for the attached interface. '
'(supported by --os-compute-api-version 2.52 or above)'
)
)
return parser
def take_action(self, parsed_args):
@ -246,11 +254,23 @@ class AddFixedIP(command.Command):
network = compute_client.api.network_find(parsed_args.network)
server.interface_attach(
port_id=None,
net_id=network['id'],
fixed_ip=parsed_args.fixed_ip_address,
kwargs = {
'port_id': None,
'net_id': network['id'],
'fixed_ip': parsed_args.fixed_ip_address,
}
if parsed_args.tag:
if compute_client.api_version < api_versions.APIVersion('2.49'):
msg = _(
'--os-compute-api-version 2.49 or greater is required to '
'support the --tag option'
)
raise exceptions.CommandError(msg)
kwargs['tag'] = parsed_args.tag
server.interface_attach(**kwargs)
class AddFloatingIP(network_common.NetworkAndComputeCommand):

View File

@ -177,6 +177,72 @@ class TestServerAddFixedIP(TestServer):
extralist = ['--fixed-ip-address', '5.6.7.8']
self._test_server_add_fixed_ip(extralist, '5.6.7.8')
def test_server_add_fixed_ip_with_tag(self):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.49')
servers = self.setup_servers_mock(count=1)
network = compute_fakes.FakeNetwork.create_one_network()
with mock.patch(
'openstackclient.api.compute_v2.APIv2.network_find'
) as net_mock:
net_mock.return_value = network
arglist = [
servers[0].id,
network['id'],
'--fixed-ip-address', '5.6.7.8',
'--tag', 'tag1',
]
verifylist = [
('server', servers[0].id),
('network', network['id']),
('fixed_ip_address', '5.6.7.8'),
('tag', 'tag1'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
servers[0].interface_attach.assert_called_once_with(
port_id=None,
net_id=network['id'],
fixed_ip='5.6.7.8',
tag='tag1'
)
self.assertIsNone(result)
def test_server_add_fixed_ip_with_tag_pre_v249(self):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.48')
servers = self.setup_servers_mock(count=1)
network = compute_fakes.FakeNetwork.create_one_network()
with mock.patch(
'openstackclient.api.compute_v2.APIv2.network_find'
) as net_mock:
net_mock.return_value = network
arglist = [
servers[0].id,
network['id'],
'--fixed-ip-address', '5.6.7.8',
'--tag', 'tag1',
]
verifylist = [
('server', servers[0].id),
('network', network['id']),
('fixed_ip_address', '5.6.7.8'),
('tag', 'tag1'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.49 or greater is required',
str(ex))
@mock.patch(
'openstackclient.api.compute_v2.APIv2.floating_ip_add'

View File

@ -0,0 +1,5 @@
---
features:
- Add ``--tag`` option to ``server add fixed ip`` command
when adding a fixed IP to server. Only available starting
with ``--os-compute-api-version 2.49``.