Browse Source

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
tags/3.10.0
Cedric Brandily 2 years ago
parent
commit
21510ac1a9

+ 40
- 0
doc/source/command-objects/server.rst View File

@@ -49,6 +49,26 @@ Add floating IP address to server
49 49
 
50 50
     Floating IP address (IP address only) to assign to server
51 51
 
52
+server add port
53
+---------------
54
+
55
+Add port to server
56
+
57
+.. program:: server add port
58
+.. code:: bash
59
+
60
+    openstack server add port
61
+        <server>
62
+        <port>
63
+
64
+.. describe:: <server>
65
+
66
+    Server to add the port to (name or ID)
67
+
68
+.. describe:: <port>
69
+
70
+    Port to add to the server (name or ID)
71
+
52 72
 server add security group
53 73
 -------------------------
54 74
 
@@ -523,6 +543,26 @@ Remove floating IP address from server
523 543
 
524 544
     Floating IP address (IP address only) to remove from server
525 545
 
546
+server remove port
547
+------------------
548
+
549
+Remove port from server
550
+
551
+.. program:: server remove port
552
+.. code:: bash
553
+
554
+    openstack server remove port
555
+        <server>
556
+        <port>
557
+
558
+.. describe:: <server>
559
+
560
+    Server to remove the port from (name or ID)
561
+
562
+.. describe:: <port>
563
+
564
+    Port to remove from the server (name or ID)
565
+
526 566
 server remove security group
527 567
 ----------------------------
528 568
 

+ 66
- 0
openstackclient/compute/v2/server.py View File

@@ -253,6 +253,39 @@ class AddFloatingIP(command.Command):
253 253
                                parsed_args.fixed_ip_address)
254 254
 
255 255
 
256
+class AddPort(command.Command):
257
+    _description = _("Add port to server")
258
+
259
+    def get_parser(self, prog_name):
260
+        parser = super(AddPort, self).get_parser(prog_name)
261
+        parser.add_argument(
262
+            "server",
263
+            metavar="<server>",
264
+            help=_("Server to add the port to (name or ID)"),
265
+        )
266
+        parser.add_argument(
267
+            "port",
268
+            metavar="<port>",
269
+            help=_("Port to add to the server (name or ID)"),
270
+        )
271
+        return parser
272
+
273
+    def take_action(self, parsed_args):
274
+        compute_client = self.app.client_manager.compute
275
+
276
+        server = utils.find_resource(
277
+            compute_client.servers, parsed_args.server)
278
+
279
+        if self.app.client_manager.is_network_endpoint_enabled():
280
+            network_client = self.app.client_manager.network
281
+            port_id = network_client.find_port(
282
+                parsed_args.port, ignore_missing=False).id
283
+        else:
284
+            port_id = parsed_args.port
285
+
286
+        server.interface_attach(port_id=port_id, net_id=None, fixed_ip=None)
287
+
288
+
256 289
 class AddServerSecurityGroup(command.Command):
257 290
     _description = _("Add security group to server")
258 291
 
@@ -1342,6 +1375,39 @@ class RemoveFloatingIP(command.Command):
1342 1375
         server.remove_floating_ip(parsed_args.ip_address)
1343 1376
 
1344 1377
 
1378
+class RemovePort(command.Command):
1379
+    _description = _("Remove port from server")
1380
+
1381
+    def get_parser(self, prog_name):
1382
+        parser = super(RemovePort, self).get_parser(prog_name)
1383
+        parser.add_argument(
1384
+            "server",
1385
+            metavar="<server>",
1386
+            help=_("Server to remove the port from (name or ID)"),
1387
+        )
1388
+        parser.add_argument(
1389
+            "port",
1390
+            metavar="<port>",
1391
+            help=_("Port to remove from the server (name or ID)"),
1392
+        )
1393
+        return parser
1394
+
1395
+    def take_action(self, parsed_args):
1396
+        compute_client = self.app.client_manager.compute
1397
+
1398
+        server = utils.find_resource(
1399
+            compute_client.servers, parsed_args.server)
1400
+
1401
+        if self.app.client_manager.is_network_endpoint_enabled():
1402
+            network_client = self.app.client_manager.network
1403
+            port_id = network_client.find_port(
1404
+                parsed_args.port, ignore_missing=False).id
1405
+        else:
1406
+            port_id = parsed_args.port
1407
+
1408
+        server.interface_detach(port_id)
1409
+
1410
+
1345 1411
 class RemoveServerSecurityGroup(command.Command):
1346 1412
     _description = _("Remove security group from server")
1347 1413
 

+ 96
- 0
openstackclient/tests/unit/compute/v2/test_server.py View File

@@ -146,6 +146,9 @@ class TestServerAddFloatingIP(TestServer):
146 146
             'add_floating_ip': None,
147 147
         }
148 148
 
149
+        self.find_port = mock.Mock()
150
+        self.app.client_manager.network.find_port = self.find_port
151
+
149 152
     def _test_server_add_floating_ip(self, extralist, fixed_ip_address):
150 153
         servers = self.setup_servers_mock(count=1)
151 154
 
@@ -174,6 +177,53 @@ class TestServerAddFloatingIP(TestServer):
174 177
         self._test_server_add_floating_ip(extralist, '5.6.7.8')
175 178
 
176 179
 
180
+class TestServerAddPort(TestServer):
181
+
182
+    def setUp(self):
183
+        super(TestServerAddPort, self).setUp()
184
+
185
+        # Get the command object to test
186
+        self.cmd = server.AddPort(self.app, None)
187
+
188
+        # Set add_fixed_ip method to be tested.
189
+        self.methods = {
190
+            'interface_attach': None,
191
+        }
192
+
193
+        self.find_port = mock.Mock()
194
+        self.app.client_manager.network.find_port = self.find_port
195
+
196
+    def _test_server_add_port(self, port_id):
197
+        servers = self.setup_servers_mock(count=1)
198
+        port = 'fake-port'
199
+
200
+        arglist = [
201
+            servers[0].id,
202
+            port,
203
+        ]
204
+        verifylist = [
205
+            ('server', servers[0].id),
206
+            ('port', port)
207
+        ]
208
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
209
+
210
+        result = self.cmd.take_action(parsed_args)
211
+
212
+        servers[0].interface_attach.assert_called_once_with(
213
+            port_id=port_id, net_id=None, fixed_ip=None)
214
+        self.assertIsNone(result)
215
+
216
+    def test_server_add_port(self):
217
+        self._test_server_add_port(self.find_port.return_value.id)
218
+        self.find_port.assert_called_once_with(
219
+            'fake-port', ignore_missing=False)
220
+
221
+    def test_server_add_port_no_neutron(self):
222
+        self.app.client_manager.network_endpoint_enabled = False
223
+        self._test_server_add_port('fake-port')
224
+        self.find_port.assert_not_called()
225
+
226
+
177 227
 class TestServerAddSecurityGroup(TestServer):
178 228
 
179 229
     def setUp(self):
@@ -1613,6 +1663,52 @@ class TestServerRemoveFloatingIP(TestServer):
1613 1663
         self.assertIsNone(result)
1614 1664
 
1615 1665
 
1666
+class TestServerRemovePort(TestServer):
1667
+
1668
+    def setUp(self):
1669
+        super(TestServerRemovePort, self).setUp()
1670
+
1671
+        # Get the command object to test
1672
+        self.cmd = server.RemovePort(self.app, None)
1673
+
1674
+        # Set method to be tested.
1675
+        self.methods = {
1676
+            'interface_detach': None,
1677
+        }
1678
+
1679
+        self.find_port = mock.Mock()
1680
+        self.app.client_manager.network.find_port = self.find_port
1681
+
1682
+    def _test_server_remove_port(self, port_id):
1683
+        servers = self.setup_servers_mock(count=1)
1684
+        port = 'fake-port'
1685
+
1686
+        arglist = [
1687
+            servers[0].id,
1688
+            port,
1689
+        ]
1690
+        verifylist = [
1691
+            ('server', servers[0].id),
1692
+            ('port', port),
1693
+        ]
1694
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1695
+
1696
+        result = self.cmd.take_action(parsed_args)
1697
+
1698
+        servers[0].interface_detach.assert_called_once_with(port_id)
1699
+        self.assertIsNone(result)
1700
+
1701
+    def test_server_remove_port(self):
1702
+        self._test_server_remove_port(self.find_port.return_value.id)
1703
+        self.find_port.assert_called_once_with(
1704
+            'fake-port', ignore_missing=False)
1705
+
1706
+    def test_server_remove_port_no_neutron(self):
1707
+        self.app.client_manager.network_endpoint_enabled = False
1708
+        self._test_server_remove_port('fake-port')
1709
+        self.find_port.assert_not_called()
1710
+
1711
+
1616 1712
 class TestServerRemoveSecurityGroup(TestServer):
1617 1713
 
1618 1714
     def setUp(self):

+ 6
- 0
releasenotes/notes/allow-to-add-remove-vm-ports-273593d7cc1982de.yaml View File

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

+ 2
- 0
setup.cfg View File

@@ -102,6 +102,7 @@ openstack.compute.v2 =
102 102
 
103 103
     server_add_fixed_ip = openstackclient.compute.v2.server:AddFixedIP
104 104
     server_add_floating_ip = openstackclient.compute.v2.server:AddFloatingIP
105
+    server_add_port = openstackclient.compute.v2.server:AddPort
105 106
     server_add_security_group = openstackclient.compute.v2.server:AddServerSecurityGroup
106 107
     server_add_volume = openstackclient.compute.v2.server:AddServerVolume
107 108
     server_create = openstackclient.compute.v2.server:CreateServer
@@ -114,6 +115,7 @@ openstack.compute.v2 =
114 115
     server_rebuild = openstackclient.compute.v2.server:RebuildServer
115 116
     server_remove_fixed_ip = openstackclient.compute.v2.server:RemoveFixedIP
116 117
     server_remove_floating_ip = openstackclient.compute.v2.server:RemoveFloatingIP
118
+    server_remove_port = openstackclient.compute.v2.server:RemovePort
117 119
     server_remove_security_group = openstackclient.compute.v2.server:RemoveServerSecurityGroup
118 120
     server_remove_volume = openstackclient.compute.v2.server:RemoveServerVolume
119 121
     server_rescue = openstackclient.compute.v2.server:RescueServer

Loading…
Cancel
Save