Parcourir la source

Key Pair ID getter for GCE Instances

almahmoud il y a 7 ans
Parent
commit
5af757ee92
1 fichiers modifiés avec 12 ajouts et 8 suppressions
  1. 12 8
      cloudbridge/cloud/providers/gce/resources.py

+ 12 - 8
cloudbridge/cloud/providers/gce/resources.py

@@ -1066,14 +1066,18 @@ class GCEInstance(BaseInstance):
     def key_pair_id(self):
         """
         Get the id of the key pair associated with this instance.
-        For GCE, since keys apply to all instances, return first
-        key in metadata.
-        """
-        try:
-            kp = next(iter(self._provider.security.key_pairs))
-            return kp.id if kp else None
-        except StopIteration:
-            return None
+        Assume there is only 1 key pair
+        """
+        # Get instance again to avoid stale metadata
+        ins = self._provider.compute.instances.get(self.id)
+        meta = ins._gce_instance.get('metadata', {})
+        if meta:
+            items = meta.get("items", [])
+            for item in items:
+                if item.get("key") == "ssh-keys":
+                    # The key pair name/id is stored last, after the public key
+                    return item.get("value").split(" ")[-1]
+        return None
 
     @key_pair_id.setter
     # pylint:disable=arguments-differ