Skip to content

Commit 7e56102

Browse files
Fix output of interface-attach command
Add output of a result in the 'nova interface-attach' command when it is successful. Make the following methods return a 'NetworkInterface' object instead of a 'Server' object. * The 'interface_attach' method in the 'novaclient.v2.Server' class * The 'interface_attach' method in the 'novaclient.v2.ServerManager' class Remove unnecessary code in the 'nova interface-detach' command because the response body is not returned. Change-Id: Id5316d8ad4a4b67e8399b51e602aafc83bc128c6 Closes-Bug: #1816511
1 parent 81ea988 commit 7e56102

6 files changed

Lines changed: 58 additions & 14 deletions

File tree

novaclient/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,19 @@ def _get(self, url, response_key, filters=None):
358358
return self.resource_class(self, content, loaded=True,
359359
resp=resp)
360360

361-
def _create(self, url, body, response_key, return_raw=False, **kwargs):
361+
def _create(self, url, body, response_key, return_raw=False,
362+
obj_class=None, **kwargs):
362363
self.run_hooks('modify_body_for_create', body, **kwargs)
363364
resp, body = self.api.client.post(url, body=body)
364365
if return_raw:
365366
return self.convert_into_with_meta(body[response_key], resp)
366367

367-
with self.completion_cache('human_id', self.resource_class, mode="a"):
368-
with self.completion_cache('uuid', self.resource_class, mode="a"):
369-
return self.resource_class(self, body[response_key], resp=resp)
368+
if obj_class is None:
369+
obj_class = self.resource_class
370+
371+
with self.completion_cache('human_id', obj_class, mode="a"):
372+
with self.completion_cache('uuid', obj_class, mode="a"):
373+
return obj_class(self, body[response_key], resp=resp)
370374

371375
def _delete(self, url):
372376
resp, body = self.api.client.delete(url)

novaclient/tests/functional/v2/test_servers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,20 @@ def test_list(self):
326326
flavor_output = self.nova("flavor-show %s" % self.flavor.id)
327327
flavor_val = self._get_value_from_the_table(flavor_output, 'disk')
328328
self.assertEqual(flavor_val, server_flavor_val)
329+
330+
331+
class TestInterfaceAttach(base.ClientTestBase):
332+
333+
COMPUTE_API_VERSION = '2.latest'
334+
335+
def test_interface_attach(self):
336+
server = self._create_server()
337+
output = self.nova("interface-attach --net-id %s %s" %
338+
(self.network.id, server.id))
339+
340+
for key in ('ip_address', 'mac_addr', 'port_id', 'port_state'):
341+
self._get_value_from_the_table(output, key)
342+
343+
self.assertEqual(
344+
self.network.id,
345+
self._get_value_from_the_table(output, 'net_id'))

novaclient/tests/unit/v2/test_servers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ def test_interface_attach(self):
941941
ret = s.interface_attach(None, None, None)
942942
self.assert_request_id(ret, fakes.FAKE_REQUEST_ID_LIST)
943943
self.assert_called('POST', '/servers/1234/os-interface')
944+
self.assertIsInstance(ret, servers.NetworkInterface)
944945

945946
def test_interface_detach(self):
946947
s = self.cs.servers.get(1234)
@@ -1409,6 +1410,7 @@ def test_interface_attach_with_tag(self):
14091410
{'interfaceAttachment':
14101411
{'port_id': '7f42712e-63fe-484c-a6df-30ae4867ff66',
14111412
'tag': 'test_tag'}})
1413+
self.assertIsInstance(ret, servers.NetworkInterface)
14121414

14131415
def test_add_fixed_ip(self):
14141416
# novaclient.v2.servers.Server.add_fixed_ip()

novaclient/v2/servers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,8 @@ def interface_attach(self, server, port_id, net_id, fixed_ip):
19441944
{'ip_address': fixed_ip}]
19451945

19461946
return self._create('/servers/%s/os-interface' % base.getid(server),
1947-
body, 'interfaceAttachment')
1947+
body, 'interfaceAttachment',
1948+
obj_class=NetworkInterface)
19481949

19491950
@api_versions.wraps("2.49")
19501951
def interface_attach(self, server, port_id, net_id, fixed_ip, tag=None):
@@ -1973,7 +1974,8 @@ def interface_attach(self, server, port_id, net_id, fixed_ip, tag=None):
19731974
body['interfaceAttachment']['tag'] = tag
19741975

19751976
return self._create('/servers/%s/os-interface' % base.getid(server),
1976-
body, 'interfaceAttachment')
1977+
body, 'interfaceAttachment',
1978+
obj_class=NetworkInterface)
19771979

19781980
def interface_detach(self, server, port_id):
19791981
"""

novaclient/v2/shell.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4530,21 +4530,29 @@ def do_interface_attach(cs, args):
45304530
if 'tag' in args and args.tag:
45314531
update_kwargs['tag'] = args.tag
45324532

4533-
res = server.interface_attach(args.port_id, args.net_id, args.fixed_ip,
4534-
**update_kwargs)
4535-
if isinstance(res, dict):
4536-
utils.print_dict(res)
4533+
network_interface = server.interface_attach(
4534+
args.port_id, args.net_id, args.fixed_ip, **update_kwargs)
4535+
4536+
_print_interface(network_interface)
4537+
4538+
4539+
def _print_interface(interface):
4540+
ni_dict = interface.to_dict()
4541+
4542+
fixed_ips = ni_dict.pop('fixed_ips', None)
4543+
ni_dict['ip_address'] = (",".join(
4544+
[fip['ip_address'] for fip in fixed_ips])
4545+
if fixed_ips is not None else None)
4546+
4547+
utils.print_dict(ni_dict)
45374548

45384549

45394550
@utils.arg('server', metavar='<server>', help=_('Name or ID of server.'))
45404551
@utils.arg('port_id', metavar='<port_id>', help=_('Port ID.'))
45414552
def do_interface_detach(cs, args):
45424553
"""Detach a network interface from a server."""
45434554
server = _find_server(cs, args.server)
4544-
4545-
res = server.interface_detach(args.port_id)
4546-
if isinstance(res, dict):
4547-
utils.print_dict(res)
4555+
server.interface_detach(args.port_id)
45484556

45494557

45504558
@api_versions.wraps("2.17")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
upgrade:
3+
- The ``nova interface-attach`` command shows output of its result
4+
when it is successful.
5+
- |
6+
The following methods return a ``NetworkInterface`` object
7+
instead of a ``Server`` object.
8+
9+
* The ``interface_attach`` method in the ``novaclient.v2.Server`` class
10+
* The ``interface_attach`` method in the ``novaclient.v2.ServerManager``
11+
class

0 commit comments

Comments
 (0)