Browse Source

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 năm trước cách đây
mục cha
commit
9df4b4ccf7

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

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

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