Enable to add/remove port to/from a server

This change enables to add/remove a specific port to/from a server using
the new commands:

 openstack server add port <vm> <port>
 openstack server remove port <vm> <port>

Closes-Bug: #1678137
Change-Id: I6ee57df089235ccc1fb9d38316bd484956b1134d
This commit is contained in:
Cedric Brandily 2017-03-31 23:11:47 +02:00
parent 6591154488
commit 21510ac1a9
5 changed files with 210 additions and 0 deletions

View File

@ -49,6 +49,26 @@ Add floating IP address to server
Floating IP address (IP address only) to assign to server Floating IP address (IP address only) to assign to server
server add port
---------------
Add port to server
.. program:: server add port
.. code:: bash
openstack server add port
<server>
<port>
.. describe:: <server>
Server to add the port to (name or ID)
.. describe:: <port>
Port to add to the server (name or ID)
server add security group server add security group
------------------------- -------------------------
@ -523,6 +543,26 @@ Remove floating IP address from server
Floating IP address (IP address only) to remove from server Floating IP address (IP address only) to remove from server
server remove port
------------------
Remove port from server
.. program:: server remove port
.. code:: bash
openstack server remove port
<server>
<port>
.. describe:: <server>
Server to remove the port from (name or ID)
.. describe:: <port>
Port to remove from the server (name or ID)
server remove security group server remove security group
---------------------------- ----------------------------

View File

@ -253,6 +253,39 @@ class AddFloatingIP(command.Command):
parsed_args.fixed_ip_address) parsed_args.fixed_ip_address)
class AddPort(command.Command):
_description = _("Add port to server")
def get_parser(self, prog_name):
parser = super(AddPort, self).get_parser(prog_name)
parser.add_argument(
"server",
metavar="<server>",
help=_("Server to add the port to (name or ID)"),
)
parser.add_argument(
"port",
metavar="<port>",
help=_("Port to add to the server (name or ID)"),
)
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)
if self.app.client_manager.is_network_endpoint_enabled():
network_client = self.app.client_manager.network
port_id = network_client.find_port(
parsed_args.port, ignore_missing=False).id
else:
port_id = parsed_args.port
server.interface_attach(port_id=port_id, net_id=None, fixed_ip=None)
class AddServerSecurityGroup(command.Command): class AddServerSecurityGroup(command.Command):
_description = _("Add security group to server") _description = _("Add security group to server")
@ -1342,6 +1375,39 @@ class RemoveFloatingIP(command.Command):
server.remove_floating_ip(parsed_args.ip_address) server.remove_floating_ip(parsed_args.ip_address)
class RemovePort(command.Command):
_description = _("Remove port from server")
def get_parser(self, prog_name):
parser = super(RemovePort, self).get_parser(prog_name)
parser.add_argument(
"server",
metavar="<server>",
help=_("Server to remove the port from (name or ID)"),
)
parser.add_argument(
"port",
metavar="<port>",
help=_("Port to remove from the server (name or ID)"),
)
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)
if self.app.client_manager.is_network_endpoint_enabled():
network_client = self.app.client_manager.network
port_id = network_client.find_port(
parsed_args.port, ignore_missing=False).id
else:
port_id = parsed_args.port
server.interface_detach(port_id)
class RemoveServerSecurityGroup(command.Command): class RemoveServerSecurityGroup(command.Command):
_description = _("Remove security group from server") _description = _("Remove security group from server")

View File

@ -146,6 +146,9 @@ class TestServerAddFloatingIP(TestServer):
'add_floating_ip': None, 'add_floating_ip': None,
} }
self.find_port = mock.Mock()
self.app.client_manager.network.find_port = self.find_port
def _test_server_add_floating_ip(self, extralist, fixed_ip_address): def _test_server_add_floating_ip(self, extralist, fixed_ip_address):
servers = self.setup_servers_mock(count=1) servers = self.setup_servers_mock(count=1)
@ -174,6 +177,53 @@ class TestServerAddFloatingIP(TestServer):
self._test_server_add_floating_ip(extralist, '5.6.7.8') self._test_server_add_floating_ip(extralist, '5.6.7.8')
class TestServerAddPort(TestServer):
def setUp(self):
super(TestServerAddPort, self).setUp()
# Get the command object to test
self.cmd = server.AddPort(self.app, None)
# Set add_fixed_ip method to be tested.
self.methods = {
'interface_attach': None,
}
self.find_port = mock.Mock()
self.app.client_manager.network.find_port = self.find_port
def _test_server_add_port(self, port_id):
servers = self.setup_servers_mock(count=1)
port = 'fake-port'
arglist = [
servers[0].id,
port,
]
verifylist = [
('server', servers[0].id),
('port', port)
]
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=port_id, net_id=None, fixed_ip=None)
self.assertIsNone(result)
def test_server_add_port(self):
self._test_server_add_port(self.find_port.return_value.id)
self.find_port.assert_called_once_with(
'fake-port', ignore_missing=False)
def test_server_add_port_no_neutron(self):
self.app.client_manager.network_endpoint_enabled = False
self._test_server_add_port('fake-port')
self.find_port.assert_not_called()
class TestServerAddSecurityGroup(TestServer): class TestServerAddSecurityGroup(TestServer):
def setUp(self): def setUp(self):
@ -1613,6 +1663,52 @@ class TestServerRemoveFloatingIP(TestServer):
self.assertIsNone(result) self.assertIsNone(result)
class TestServerRemovePort(TestServer):
def setUp(self):
super(TestServerRemovePort, self).setUp()
# Get the command object to test
self.cmd = server.RemovePort(self.app, None)
# Set method to be tested.
self.methods = {
'interface_detach': None,
}
self.find_port = mock.Mock()
self.app.client_manager.network.find_port = self.find_port
def _test_server_remove_port(self, port_id):
servers = self.setup_servers_mock(count=1)
port = 'fake-port'
arglist = [
servers[0].id,
port,
]
verifylist = [
('server', servers[0].id),
('port', port),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
servers[0].interface_detach.assert_called_once_with(port_id)
self.assertIsNone(result)
def test_server_remove_port(self):
self._test_server_remove_port(self.find_port.return_value.id)
self.find_port.assert_called_once_with(
'fake-port', ignore_missing=False)
def test_server_remove_port_no_neutron(self):
self.app.client_manager.network_endpoint_enabled = False
self._test_server_remove_port('fake-port')
self.find_port.assert_not_called()
class TestServerRemoveSecurityGroup(TestServer): class TestServerRemoveSecurityGroup(TestServer):
def setUp(self): def setUp(self):

View File

@ -0,0 +1,6 @@
---
features:
- |
Add ``server add port`` and ``server remove port`` commands which enable to
add/remove ports to/from a server
[Bug `1678137 <https://bugs.launchpad.net/python-openstackclient/+bug/1678137>`_]

View File

@ -102,6 +102,7 @@ openstack.compute.v2 =
server_add_fixed_ip = openstackclient.compute.v2.server:AddFixedIP server_add_fixed_ip = openstackclient.compute.v2.server:AddFixedIP
server_add_floating_ip = openstackclient.compute.v2.server:AddFloatingIP server_add_floating_ip = openstackclient.compute.v2.server:AddFloatingIP
server_add_port = openstackclient.compute.v2.server:AddPort
server_add_security_group = openstackclient.compute.v2.server:AddServerSecurityGroup server_add_security_group = openstackclient.compute.v2.server:AddServerSecurityGroup
server_add_volume = openstackclient.compute.v2.server:AddServerVolume server_add_volume = openstackclient.compute.v2.server:AddServerVolume
server_create = openstackclient.compute.v2.server:CreateServer server_create = openstackclient.compute.v2.server:CreateServer
@ -114,6 +115,7 @@ openstack.compute.v2 =
server_rebuild = openstackclient.compute.v2.server:RebuildServer server_rebuild = openstackclient.compute.v2.server:RebuildServer
server_remove_fixed_ip = openstackclient.compute.v2.server:RemoveFixedIP server_remove_fixed_ip = openstackclient.compute.v2.server:RemoveFixedIP
server_remove_floating_ip = openstackclient.compute.v2.server:RemoveFloatingIP server_remove_floating_ip = openstackclient.compute.v2.server:RemoveFloatingIP
server_remove_port = openstackclient.compute.v2.server:RemovePort
server_remove_security_group = openstackclient.compute.v2.server:RemoveServerSecurityGroup server_remove_security_group = openstackclient.compute.v2.server:RemoveServerSecurityGroup
server_remove_volume = openstackclient.compute.v2.server:RemoveServerVolume server_remove_volume = openstackclient.compute.v2.server:RemoveServerVolume
server_rescue = openstackclient.compute.v2.server:RescueServer server_rescue = openstackclient.compute.v2.server:RescueServer