diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index e5a7a32833..5370a63f81 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -354,6 +354,14 @@ class AddPort(command.Command): metavar="", help=_("Port to add to the server (name or ID)"), ) + parser.add_argument( + '--tag', + metavar='', + help=_( + "Tag for the attached interface. " + "(Supported by API versions '2.49' - '2.latest')" + ) + ) return parser def take_action(self, parsed_args): @@ -369,7 +377,22 @@ class AddPort(command.Command): else: port_id = parsed_args.port - server.interface_attach(port_id=port_id, net_id=None, fixed_ip=None) + kwargs = { + 'port_id': port_id, + 'net_id': None, + '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 AddNetwork(command.Command): diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 59282b4a12..c16bb333f7 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -465,6 +465,59 @@ class TestServerAddPort(TestServer): self._test_server_add_port('fake-port') self.find_port.assert_not_called() + def test_server_add_port_with_tag(self): + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.49') + + servers = self.setup_servers_mock(count=1) + self.find_port.return_value.id = 'fake-port' + arglist = [ + servers[0].id, + 'fake-port', + '--tag', 'tag1', + ] + verifylist = [ + ('server', servers[0].id), + ('port', 'fake-port'), + ('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='fake-port', + net_id=None, + fixed_ip=None, + tag='tag1') + + def test_server_add_port_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_port.return_value.id = 'fake-port' + arglist = [ + servers[0].id, + 'fake-port', + '--tag', 'tag1', + ] + verifylist = [ + ('server', servers[0].id), + ('port', 'fake-port'), + ('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)) + class TestServerVolume(TestServer): diff --git a/releasenotes/notes/add-tag-support-server-add-port-7e30aa38202d0839.yaml b/releasenotes/notes/add-tag-support-server-add-port-7e30aa38202d0839.yaml new file mode 100644 index 0000000000..a5a63128c7 --- /dev/null +++ b/releasenotes/notes/add-tag-support-server-add-port-7e30aa38202d0839.yaml @@ -0,0 +1,5 @@ +--- +features: + - Add ``--tag`` option to ``server add port`` command when + add a port to server. Only available starting with + ``--os-compute-api-version 2.49``.