summaryrefslogtreecommitdiff
path: root/openstackclient/compute/v2/server.py
diff options
context:
space:
mode:
authorTerryHowe <terrylhowe@gmail.com>2015-07-08 13:52:45 -0600
committerTerryHowe <terrylhowe@gmail.com>2015-07-08 15:00:51 -0600
commitf89fc1ef3288fb432fe6f76268a92f6f1111a1eb (patch)
treed4d4620d91abd8381e1bdbc73f41e508380ec976 /openstackclient/compute/v2/server.py
parentf07f71661f8e215844dff6156781975041a3b1c4 (diff)
downloadpython-openstackclient-f89fc1ef3288fb432fe6f76268a92f6f1111a1eb.tar.gz
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
Diffstat (limited to 'openstackclient/compute/v2/server.py')
-rw-r--r--openstackclient/compute/v2/server.py52
1 files changed, 37 insertions, 15 deletions
diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py
index 5007b072..4efef975 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='<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))