From f89fc1ef3288fb432fe6f76268a92f6f1111a1eb Mon Sep 17 00:00:00 2001 From: TerryHowe Date: Wed, 8 Jul 2015 13:52:45 -0600 Subject: [PATCH] Fix address parsing for server ssh command There seem to be three formats for the server address field and the old code only supported the old format. This code adds a parser for all three formats. Change-Id: I7f12d2c69ff70556907ea6f31a0e0bba91b68b49 Closes-Bug: #1469843 --- openstackclient/compute/v2/server.py | 52 ++++++++++++------ .../tests/compute/v2/test_server.py | 54 +++++++++++++++++++ 2 files changed, 91 insertions(+), 15 deletions(-) diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 5007b072ac..4efef975bb 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -55,6 +55,39 @@ def _format_servers_list_networks(networks): return '; '.join(output) +def _get_ip_address(addresses, address_type, ip_address_family): + # Old style addresses + if address_type in addresses: + for addy in addresses[address_type]: + if int(addy['version']) in ip_address_family: + return addy['addr'] + + # New style addresses + new_address_type = address_type + if address_type == 'public': + new_address_type = 'floating' + if address_type == 'private': + new_address_type = 'fixed' + for network in addresses: + for addy in addresses[network]: + # Case where it is list of strings + if isinstance(addy, six.string_types): + if new_address_type == 'fixed': + return addresses[network][0] + else: + return addresses[network][-1] + # Case where it is a dict + if 'OS-EXT-IPS:type' not in addy: + continue + if addy['OS-EXT-IPS:type'] == new_address_type: + if int(addy['version']) in ip_address_family: + return addy['addr'] + raise exceptions.CommandError( + "ERROR: No %s IP version %s address found" % + (address_type, ip_address_family) + ) + + def _prep_server_detail(compute_client, server): """Prepare the detailed server dict for printing @@ -1283,6 +1316,7 @@ class SshServer(command.Command): ) parser.add_argument( '-l', + dest='login', metavar='', help=argparse.SUPPRESS, ) @@ -1381,13 +1415,6 @@ class SshServer(command.Command): # Build the command cmd = "ssh" - # Look for address type - if parsed_args.address_type: - address_type = parsed_args.address_type - if address_type not in server.addresses: - raise SystemExit("ERROR: No %s IP address found" % address_type) - - # Set up desired address family ip_address_family = [4, 6] if parsed_args.ipv4: ip_address_family = [4] @@ -1396,14 +1423,6 @@ class SshServer(command.Command): ip_address_family = [6] cmd += " -6" - # Grab the first matching IP address - ip_address = None - for addr in server.addresses[address_type]: - if int(addr['version']) in ip_address_family: - ip_address = addr['addr'] - if not ip_address: - raise SystemExit("ERROR: No IP address found") - if parsed_args.port: cmd += " -p %d" % parsed_args.port if parsed_args.identity: @@ -1418,6 +1437,9 @@ class SshServer(command.Command): cmd += " -v" cmd += " %s@%s" + ip_address = _get_ip_address(server.addresses, + parsed_args.address_type, + ip_address_family) self.log.debug("ssh command: %s", (cmd % (login, ip_address))) os.system(cmd % (login, ip_address)) diff --git a/openstackclient/tests/compute/v2/test_server.py b/openstackclient/tests/compute/v2/test_server.py index a8a1936d82..eed148a0cf 100644 --- a/openstackclient/tests/compute/v2/test_server.py +++ b/openstackclient/tests/compute/v2/test_server.py @@ -15,7 +15,9 @@ import copy import mock +import testtools +from openstackclient.common import exceptions from openstackclient.common import utils as common_utils from openstackclient.compute.v2 import server from openstackclient.tests.compute.v2 import fakes as compute_fakes @@ -580,3 +582,55 @@ class TestServerResize(TestServer): self.servers_mock.revert_resize.assert_called_with( self.servers_get_return_value, ) + + +class TestServerGeneral(testtools.TestCase): + OLD = { + 'private': [ + { + 'addr': '192.168.0.3', + 'version': 4, + }, + ] + } + NEW = { + 'foo': [ + { + 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:93:b3:01', + 'version': 4, + 'addr': '10.10.1.2', + 'OS-EXT-IPS:type': 'fixed', + }, + { + 'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:93:b3:02', + 'version': 6, + 'addr': '0:0:0:0:0:ffff:a0a:103', + 'OS-EXT-IPS:type': 'floating', + }, + ] + } + ODD = {'jenkins': ['10.3.3.18', '124.12.125.4']} + + def test_get_ip_address(self): + self.assertEqual("192.168.0.3", + server._get_ip_address(self.OLD, 'private', [4, 6])) + self.assertEqual("10.10.1.2", + server._get_ip_address(self.NEW, 'fixed', [4, 6])) + self.assertEqual("10.10.1.2", + server._get_ip_address(self.NEW, 'private', [4, 6])) + self.assertEqual("0:0:0:0:0:ffff:a0a:103", + server._get_ip_address(self.NEW, 'public', [6])) + self.assertEqual("0:0:0:0:0:ffff:a0a:103", + server._get_ip_address(self.NEW, 'floating', [6])) + self.assertEqual("124.12.125.4", + server._get_ip_address(self.ODD, 'public', [4, 6])) + self.assertEqual("10.3.3.18", + server._get_ip_address(self.ODD, 'private', [4, 6])) + self.assertRaises(exceptions.CommandError, + server._get_ip_address, self.NEW, 'public', [4]) + self.assertRaises(exceptions.CommandError, + server._get_ip_address, self.NEW, 'admin', [4]) + self.assertRaises(exceptions.CommandError, + server._get_ip_address, self.OLD, 'public', [4, 6]) + self.assertRaises(exceptions.CommandError, + server._get_ip_address, self.OLD, 'private', [6])