فهرست منبع

Rename _target_instance

_get_target_instance() instead of _target_instance and
_get_existing_target_instance() instead of _existing_target_instance
is easier to read.
Ehsan Chiniforooshan 9 سال پیش
والد
کامیت
9df4b4ccf7
2فایلهای تغییر یافته به همراه16 افزوده شده و 15 حذف شده
  1. 3 5
      cloudbridge/cloud/providers/gce/provider.py
  2. 13 10
      cloudbridge/cloud/providers/gce/resources.py

+ 3 - 5
cloudbridge/cloud/providers/gce/provider.py

@@ -82,10 +82,8 @@ class GCPResources(object):
 
         # We will not mutate self._desc; it's OK to use items() in Python 2.x.
         for resource, resource_desc in desc['resources'].items():
-            if 'methods' not in resource_desc:
-                continue
-            methods = resource_desc['methods']
-            if 'get' not in methods:
+            methods = resource_desc.get('methods', {})
+            if not methods.get('get'):
                 continue
             method = methods['get']
             parameters = method['parameterOrder']
@@ -165,7 +163,7 @@ class GCECloudProvider(BaseCloudProvider):
         self._network = GCENetworkService(self)
 
         self._compute_resources = GCPResources(self.gce_compute)
-        # self._storage_resources = GCPResources(self.gcp_storage)
+        self._storage_resources = GCPResources(self.gcp_storage)
 
     @property
     def compute(self):

+ 13 - 10
cloudbridge/cloud/providers/gce/resources.py

@@ -914,8 +914,7 @@ class GCEInstance(BaseInstance):
         raise NotImplementedError(
             'To be implemented after GCEVolumeService.')
 
-    @property
-    def _existing_target_instance(self):
+    def _get_existing_target_instance(self):
         """
         Return the target instance corrsponding to this instance.
 
@@ -939,14 +938,13 @@ class GCEInstance(BaseInstance):
             cb.log.warning('Exception while listing target instances: %s', e)
         return None
 
-    @property
-    def _target_instance(self):
+    def _get_target_instance(self):
         """
         Return the target instance corresponding to this instance.
 
         If there is no target instance for this instance, create one.
         """
-        existing_target_instance = self._existing_target_instance
+        existing_target_instance = self._get_existing_target_instance()
         if existing_target_instance:
             return existing_target_instance
 
@@ -970,7 +968,7 @@ class GCEInstance(BaseInstance):
 
         # The following method should find the target instance that we
         # successfully created above.
-        return self._existing_target_instance
+        return self._get_existing_target_instance()
 
     def _redirect_existing_rule(self, ip, target_instance):
         """
@@ -1090,7 +1088,7 @@ class GCEInstance(BaseInstance):
                             'Floating IP "%s" is already associated to "%s".',
                             ip_address, self.name)
                     return
-                target_instance = self._target_instance
+                target_instance = self._get_target_instance()
                 if not target_instance:
                     cb.log.warning('Could not create a targetInstance for "%s"',
                                    self.name)
@@ -1112,7 +1110,7 @@ class GCEInstance(BaseInstance):
                         'Floating IP "%s" is not associated to "%s".',
                          ip_address, self.name)
                     return
-                target_instance = self._target_instance
+                target_instance = self._get_target_instance()
                 if not target_instance:
                     # We should not be here.
                     cb.log.warning('Something went wrong! "%s" is associated '
@@ -1194,6 +1192,7 @@ class GCENetwork(BaseNetwork):
         return self.state
 
 class GCEFloatingIP(BaseFloatingIP):
+    _DEAD_INSTANCE = 'dead instance'
 
     def __init__(self, provider, floating_ip):
         super(GCEFloatingIP, self).__init__(provider)
@@ -1218,7 +1217,10 @@ class GCEFloatingIP(BaseFloatingIP):
                 target = provider.parse_url(resource['target']).get()
                 if target['kind'] == 'compute#targetInstance':
                     url = provider.parse_url(target['instance'])
-                    self._target_instance = url.get()
+                    try:
+                      self._target_instance = url.get()
+                    except:
+                      self._target_instance = GCEFloatingIP._DEAD_INSTANCE
                 else:
                     cb.log.warning('Address "%s" is forwarded to a %s',
                                    floating_ip['address'], target['kind'])
@@ -1240,7 +1242,8 @@ class GCEFloatingIP(BaseFloatingIP):
 
     @property
     def private_ip(self):
-        if not self._target_instance:
+        if (not self._target_instance or
+            self._target_instance == GCEFloatingIP._DEAD_INSTANCE):
             return None
         return self._target_instance['networkInterfaces'][0]['networkIP']