]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
openstack: try to get private IP from neutron 697/head
authorLoic Dachary <ldachary@redhat.com>
Tue, 3 Nov 2015 22:45:13 +0000 (23:45 +0100)
committerLoic Dachary <ldachary@redhat.com>
Mon, 16 Nov 2015 10:41:53 +0000 (11:41 +0100)
Signed-off-by: Loic Dachary <ldachary@redhat.com>
teuthology/openstack/__init__.py
teuthology/openstack/test/test_openstack.py

index b65be1b7e02bdc9ecb5bc4443b7d6cbde96f9541..11f6775ade30d17efa37df4f1c9340d5a6221514 100644 (file)
@@ -313,14 +313,44 @@ class OpenStack(object):
                 if found:
                     return addresses
 
+    @staticmethod
+    def get_ip_neutron(instance_id):
+        subnets = json.loads(misc.sh("neutron subnet-list -f json -c id -c ip_version"))
+        subnet_id = None
+        for subnet in subnets:
+            if subnet['ip_version'] == 4:
+                subnet_id = subnet['id']
+                break
+        if not subnet_id:
+            raise Exception("no subnet with ip_version == 4")
+        ports = json.loads(misc.sh("neutron port-list -f json -c fixed_ips -c device_id"))
+        fixed_ips = None
+        for port in ports:
+            if port['device_id'] == instance_id:
+                fixed_ips = port['fixed_ips'].split("\n")
+                break
+        if not fixed_ips:
+            raise Exception("no fixed ip record found")
+        ip = None
+        for fixed_ip in fixed_ips:
+            record = json.loads(fixed_ip)
+            if record['subnet_id'] == subnet_id:
+                ip = record['ip_address']
+                break
+        if not ip:
+            raise Exception("no ip")
+        return ip
+
     def get_ip(self, instance_id, network):
         """
-        Return the private IP of the OpenStack instance_id. The network,
-        if not the empty string, disambiguate multiple networks attached
-        to the instance.
+        Return the private IP of the OpenStack instance_id.
         """
-        return re.findall(network + '=([\d.]+)',
-                          self.get_addresses(instance_id))[0]
+        try:
+            return self.get_ip_neutron(instance_id)
+        except Exception as e:
+            log.debug("ignoring get_ip_neutron exception " + str(e))
+            return re.findall(network + '=([\d.]+)',
+                              self.get_addresses(instance_id))[0]
 
 
 class TeuthologyOpenStack(OpenStack):
index c066cc8acea995167f534f330fde158b14c3e25a..feebaa0c8494cb5a3e87bf2dc98da62d6b9a57ea 100644 (file)
@@ -26,6 +26,7 @@ import logging
 import os
 import pytest
 import tempfile
+from mock import patch
 
 import teuthology
 from teuthology import misc
@@ -95,6 +96,49 @@ class TestOpenStack(object):
         else:
             del os.environ['OS_AUTH_URL']
 
+    def test_get_ip_neutron(self):
+        instance_id = '8e1fd70a-3065-46f8-9c30-84dc028c1834'
+        ip = '10.10.10.4'
+        def sh(cmd):
+            if 'neutron subnet-list' in cmd:
+                return """
+[
+  {
+    "ip_version": 6,
+    "id": "c45b9661-b2ba-4817-9e3a-f8f63bf32989"
+  },
+  {
+    "ip_version": 4,
+    "id": "e03a3dbc-afc8-4b52-952e-7bf755397b50"
+  }
+]
+                """
+            elif 'neutron port-list' in cmd:
+                return ("""
+[
+  {
+    "device_id": "915504ad-368b-4cce-be7c-4f8a83902e28",
+    "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"10.10.10.1\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc::1\\"}"
+  },
+  {
+    "device_id": "{instance_id}",
+    "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"{ip}\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc:f816:3eff:fe07:76c1\\"}"
+  },
+  {
+    "device_id": "17e4a968-4caa-4cee-8e4b-f950683a02bd",
+    "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"10.10.10.5\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc:f816:3eff:fe9c:37f0\\"}"
+  }
+]
+                """.replace('{instance_id}', instance_id).
+                        replace('{ip}', ip))
+            else:
+                raise Exception("unexpected " + cmd)
+        with patch.multiple(
+                misc,
+                sh=sh,
+        ):
+            assert ip == OpenStack.get_ip_neutron(instance_id)
+
 class TestTeuthologyOpenStack(object):
 
     @classmethod