Merge "Fix address parsing for server ssh command"

This commit is contained in:
Jenkins 2015-07-12 14:40:29 +00:00 committed by Gerrit Code Review
commit 6b80efb429
2 changed files with 91 additions and 15 deletions

View File

@ -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='<login-name>',
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))

View File

@ -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])