From d1f9ea3f750bb4c1f440ebfa93d06a40673ec0aa Mon Sep 17 00:00:00 2001 From: Tang Chen Date: Thu, 7 Apr 2016 14:10:37 +0800 Subject: [PATCH] Transfer "ip floating add/remove" to "server add/remove floating ip" This patch does the following things to transfer "ip floating add/remove" to "server add/remove floating ip": * Add new command "server add/remove floating ip", and unit tests and doc. * Deprecate "ip floating add/remove" command. compute/v2/floatingip.py is not removed because the arguments' positions are different between the new and old commands. * ip floating add server add floating ip * ip floating remove server remove floating ip Change-Id: Ic0dd22ca6fb7b7bc3e820fd5a14d7c551e7ab963 Implements: blueprint rework-ip-commands Partial-bug: 1555990 Co-Authored-By: Dean Troyer --- doc/source/command-objects/ip-floating.rst | 2 + doc/source/command-objects/server.rst | 40 +++++++++++ doc/source/commands.rst | 1 + openstackclient/compute/v2/floatingip.py | 34 ++++++++-- openstackclient/compute/v2/server.py | 55 +++++++++++++++ .../tests/compute/v2/test_server.py | 67 +++++++++++++++++++ .../ip-command-rework-8d3fe0858f51e6b8.yaml | 5 ++ setup.cfg | 2 + 8 files changed, 202 insertions(+), 4 deletions(-) diff --git a/doc/source/command-objects/ip-floating.rst b/doc/source/command-objects/ip-floating.rst index 8dd9478218..378812d123 100644 --- a/doc/source/command-objects/ip-floating.rst +++ b/doc/source/command-objects/ip-floating.rst @@ -8,6 +8,7 @@ ip floating add --------------- Add floating IP address to server +(Deprecated, please use ``server add floating ip`` instead) .. program:: ip floating add .. code:: bash @@ -92,6 +93,7 @@ ip floating remove ------------------ Remove floating IP address from server +(Deprecated, please use ``server remove floating ip`` instead) .. program:: ip floating remove .. code:: bash diff --git a/doc/source/command-objects/server.rst b/doc/source/command-objects/server.rst index f11355b6dc..3393e7fbd2 100644 --- a/doc/source/command-objects/server.rst +++ b/doc/source/command-objects/server.rst @@ -4,6 +4,26 @@ server Compute v2 +server add floating ip +---------------------- + +Add floating IP address to server + +.. program:: server add floating ip +.. code:: bash + + os server add floating ip + + + +.. describe:: + + Server (name or ID) to receive the floating IP address + +.. describe:: + + Floating IP address (IP address only) to assign to server + server add security group ------------------------- @@ -418,6 +438,26 @@ Rebuild server Server (name or ID) +server remove floating ip +------------------------- + +Remove floating IP address from server + +.. program:: server remove floating ip +.. code:: bash + + os server remove floating ip + + + +.. describe:: + + Server (name or ID) to remove the floating IP address from + +.. describe:: + + Floating IP address (IP address only) to remove from server + server remove security group ---------------------------- diff --git a/doc/source/commands.rst b/doc/source/commands.rst index 42268c25bd..714cd518a8 100644 --- a/doc/source/commands.rst +++ b/doc/source/commands.rst @@ -91,6 +91,7 @@ referring to both Compute and Volume quotas. * ``extension``: (**Compute**, **Identity**, **Network**, **Volume**) OpenStack server API extensions * ``federation protocol``: (**Identity**) the underlying protocol used while federating identities * ``flavor``: (**Compute**) predefined server configurations: ram, root disk and so on +* ``floating ip``: (**Compute**, **Network**) - a public IP address that can be mapped to a server * ``floating ip pool``: (**Compute**, **Network**) - a pool of public IP addresses * ``group``: (**Identity**) a grouping of users * ``host``: (**Compute**) - the physical computer running compute services diff --git a/openstackclient/compute/v2/floatingip.py b/openstackclient/compute/v2/floatingip.py index 98079fbc38..8398ea57db 100644 --- a/openstackclient/compute/v2/floatingip.py +++ b/openstackclient/compute/v2/floatingip.py @@ -15,28 +15,43 @@ """Floating IP action implementations""" +import logging + from osc_lib.command import command from osc_lib import utils +from openstackclient.i18n import _ + class AddFloatingIP(command.Command): """Add floating IP address to server""" + # TODO(tangchen): Remove this class and ``ip floating add`` command + # two cycles after Mitaka. + + # This notifies cliff to not display the help for this command + deprecated = True + + log = logging.getLogger('deprecated') + def get_parser(self, prog_name): parser = super(AddFloatingIP, self).get_parser(prog_name) parser.add_argument( "ip_address", metavar="", - help="IP address to add to server (name only)", + help=_("IP address to add to server (name only)"), ) parser.add_argument( "server", metavar="", - help="Server to receive the IP address (name or ID)", + help=_("Server to receive the IP address (name or ID)"), ) return parser def take_action(self, parsed_args): + self.log.warning(_('This command has been deprecated. ' + 'Please use "server add floating ip" instead.')) + compute_client = self.app.client_manager.compute server = utils.find_resource( @@ -48,21 +63,32 @@ class AddFloatingIP(command.Command): class RemoveFloatingIP(command.Command): """Remove floating IP address from server""" + # TODO(tangchen): Remove this class and ``ip floating remove`` command + # two cycles after Mitaka. + + # This notifies cliff to not display the help for this command + deprecated = True + + log = logging.getLogger('deprecated') + def get_parser(self, prog_name): parser = super(RemoveFloatingIP, self).get_parser(prog_name) parser.add_argument( "ip_address", metavar="", - help="IP address to remove from server (name only)", + help=_("IP address to remove from server (name only)"), ) parser.add_argument( "server", metavar="", - help="Server to remove the IP address from (name or ID)", + help=_("Server to remove the IP address from (name or ID)"), ) return parser def take_action(self, parsed_args): + self.log.warning(_('This command has been deprecated. ' + 'Please use "server remove floating ip" instead.')) + compute_client = self.app.client_manager.compute server = utils.find_resource( diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 7e4b0dc1f7..d7c3a6566e 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -174,6 +174,33 @@ def _show_progress(progress): sys.stdout.flush() +class AddFloatingIP(command.Command): + """Add floating IP address to server""" + + def get_parser(self, prog_name): + parser = super(AddFloatingIP, self).get_parser(prog_name) + parser.add_argument( + "server", + metavar="", + help=_("Server (name or ID) to receive the floating IP address"), + ) + parser.add_argument( + "ip_address", + metavar="", + help=_("Floating IP address (IP address only) to assign " + "to server"), + ) + return parser + + def take_action(self, parsed_args): + compute_client = self.app.client_manager.compute + + server = utils.find_resource( + compute_client.servers, parsed_args.server) + + server.add_floating_ip(parsed_args.ip_address) + + class AddServerSecurityGroup(command.Command): """Add security group to server""" @@ -1081,6 +1108,34 @@ class RebuildServer(command.ShowOne): return zip(*sorted(six.iteritems(details))) +class RemoveFloatingIP(command.Command): + """Remove floating IP address from server""" + + def get_parser(self, prog_name): + parser = super(RemoveFloatingIP, self).get_parser(prog_name) + parser.add_argument( + "server", + metavar="", + help=_("Server (name or ID) to remove the " + "floating IP address from"), + ) + parser.add_argument( + "ip_address", + metavar="", + help=_("Floating IP address (IP address only) " + "to remove from server"), + ) + return parser + + def take_action(self, parsed_args): + compute_client = self.app.client_manager.compute + + server = utils.find_resource( + compute_client.servers, parsed_args.server) + + server.remove_floating_ip(parsed_args.ip_address) + + class RemoveServerSecurityGroup(command.Command): """Remove security group from server""" diff --git a/openstackclient/tests/compute/v2/test_server.py b/openstackclient/tests/compute/v2/test_server.py index 0f155601a8..81f2185698 100644 --- a/openstackclient/tests/compute/v2/test_server.py +++ b/openstackclient/tests/compute/v2/test_server.py @@ -88,6 +88,41 @@ class TestServer(compute_fakes.TestComputev2): self.assertIsNone(result) +class TestServerAddFloatingIP(TestServer): + + def setUp(self): + super(TestServerAddFloatingIP, self).setUp() + + # Get a shortcut to the compute client ServerManager Mock + self.networks_mock = self.app.client_manager.compute.networks + + # Get the command object to test + self.cmd = server.AddFloatingIP(self.app, None) + + # Set add_floating_ip method to be tested. + self.methods = { + 'add_floating_ip': None, + } + + def test_server_add_floating_ip(self): + servers = self.setup_servers_mock(count=1) + + arglist = [ + servers[0].id, + '1.2.3.4', + ] + verifylist = [ + ('server', servers[0].id), + ('ip_address', '1.2.3.4'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + + servers[0].add_floating_ip.assert_called_once_with('1.2.3.4') + self.assertIsNone(result) + + class TestServerCreate(TestServer): columns = ( @@ -843,6 +878,38 @@ class TestServerRebuild(TestServer): self.server.rebuild.assert_called_with(self.image, None) +class TestServerRemoveFloatingIP(TestServer): + + def setUp(self): + super(TestServerRemoveFloatingIP, self).setUp() + + # Get the command object to test + self.cmd = server.RemoveFloatingIP(self.app, None) + + # Set unshelve method to be tested. + self.methods = { + 'remove_floating_ip': None, + } + + def test_server_remove_floating_ip(self): + servers = self.setup_servers_mock(count=1) + + arglist = [ + servers[0].id, + '1.2.3.4', + ] + verifylist = [ + ('server', servers[0].id), + ('ip_address', '1.2.3.4'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + + servers[0].remove_floating_ip.assert_called_once_with('1.2.3.4') + self.assertIsNone(result) + + class TestServerResize(TestServer): def setUp(self): diff --git a/releasenotes/notes/ip-command-rework-8d3fe0858f51e6b8.yaml b/releasenotes/notes/ip-command-rework-8d3fe0858f51e6b8.yaml index c5e36cfa08..3cfb11badd 100644 --- a/releasenotes/notes/ip-command-rework-8d3fe0858f51e6b8.yaml +++ b/releasenotes/notes/ip-command-rework-8d3fe0858f51e6b8.yaml @@ -4,6 +4,11 @@ features: pools. This command is used to replace the old command ``ip floating pool list``. [Blueprint rework-ip-commands ``_] + - Add new commands ``server add/remove floating ip``. They are used to + replace the old commands ``ip floating add/remove``. + [Blueprint rework-ip-commands ``_] deprecations: - Deprecate command ``ip floating pool list``. [Blueprint rework-ip-commands ``_] + - Deprecate commands ``ip floating add/remove``. + [Blueprint rework-ip-commands ``_] diff --git a/setup.cfg b/setup.cfg index cf78baf798..bab657ff2d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -98,6 +98,7 @@ openstack.compute.v2 = keypair_list = openstackclient.compute.v2.keypair:ListKeypair keypair_show = openstackclient.compute.v2.keypair:ShowKeypair + server_add_floating_ip = openstackclient.compute.v2.server:AddFloatingIP server_add_security_group = openstackclient.compute.v2.server:AddServerSecurityGroup server_add_volume = openstackclient.compute.v2.server:AddServerVolume server_create = openstackclient.compute.v2.server:CreateServer @@ -108,6 +109,7 @@ openstack.compute.v2 = server_pause = openstackclient.compute.v2.server:PauseServer server_reboot = openstackclient.compute.v2.server:RebootServer server_rebuild = openstackclient.compute.v2.server:RebuildServer + server_remove_floating_ip = openstackclient.compute.v2.server:RemoveFloatingIP server_remove_security_group = openstackclient.compute.v2.server:RemoveServerSecurityGroup server_remove_volume = openstackclient.compute.v2.server:RemoveServerVolume server_rescue = openstackclient.compute.v2.server:RescueServer