From 6f1602312b00bcba6e04a34f7f04af9d69cf2d9c Mon Sep 17 00:00:00 2001 From: tianhui Date: Wed, 11 Jul 2018 05:52:32 +0000 Subject: [PATCH] Compute: Add tag support for server add network Change-Id: I31a66b2d4dac44052a71f43a5a67836247ccac64 Story: 2002195 Task: 21678 --- openstackclient/compute/v2/server.py | 26 ++++++++- .../tests/unit/compute/v2/test_server.py | 56 +++++++++++++++++++ ...t-server-add-network-a8590cab5d7babf0.yaml | 5 ++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/add-tag-support-server-add-network-a8590cab5d7babf0.yaml diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index e5a7a32833..adbbb930c7 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -387,6 +387,14 @@ class AddNetwork(command.Command): metavar="", help=_("Network to add to the server (name or ID)"), ) + parser.add_argument( + '--tag', + metavar='', + help=_( + 'Tag for the attached interface. ' + '(supported by --os-compute-api-version 2.49 or above)' + ), + ) return parser def take_action(self, parsed_args): @@ -402,7 +410,23 @@ class AddNetwork(command.Command): else: net_id = parsed_args.network - server.interface_attach(port_id=None, net_id=net_id, fixed_ip=None) + kwargs = { + 'port_id': None, + 'net_id': net_id, + 'fixed_ip': None, + } + + 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 AddServerSecurityGroup(command.Command): diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 59282b4a12..fd5412e682 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -681,6 +681,62 @@ class TestServerAddNetwork(TestServer): self._test_server_add_network('fake-network') self.find_network.assert_not_called() + def test_server_add_network_with_tag(self): + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.49') + + servers = self.setup_servers_mock(count=1) + self.find_network.return_value.id = 'fake-network' + + arglist = [ + servers[0].id, + 'fake-network', + '--tag', 'tag1', + ] + verifylist = [ + ('server', servers[0].id), + ('network', 'fake-network'), + ('tag', 'tag1'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + self.assertIsNone(result) + + servers[0].interface_attach.assert_called_once_with( + port_id=None, + net_id='fake-network', + fixed_ip=None, + tag='tag1' + ) + + def test_server_add_network_with_tag_pre_v249(self): + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.48') + + servers = self.setup_servers_mock(count=1) + self.find_network.return_value.id = 'fake-network' + + arglist = [ + servers[0].id, + 'fake-network', + '--tag', 'tag1', + ] + verifylist = [ + ('server', servers[0].id), + ('network', 'fake-network'), + ('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.security_group_find' diff --git a/releasenotes/notes/add-tag-support-server-add-network-a8590cab5d7babf0.yaml b/releasenotes/notes/add-tag-support-server-add-network-a8590cab5d7babf0.yaml new file mode 100644 index 0000000000..3442ad6a29 --- /dev/null +++ b/releasenotes/notes/add-tag-support-server-add-network-a8590cab5d7babf0.yaml @@ -0,0 +1,5 @@ +--- +features: + - Add ``--tag`` option to ``server add network`` command + when add network to server. Only available starting + with ``--os-compute-api-version 2.49``.