瀏覽代碼

FIP delete and gateway in constructor

almahmoud 7 年之前
父節點
當前提交
9ceff4adaf

+ 5 - 1
cloudbridge/cloud/base/resources.py

@@ -814,8 +814,9 @@ class BaseSubnet(BaseCloudResource, BaseObjectLifeCycleMixin, Subnet):
 
 class BaseFloatingIP(BaseCloudResource, BaseObjectLifeCycleMixin, FloatingIP):
 
-    def __init__(self, provider):
+    def __init__(self, provider, gateway):
         super(BaseFloatingIP, self).__init__(provider)
+        self._gateway = gateway
 
     @property
     def name(self):
@@ -839,6 +840,9 @@ class BaseFloatingIP(BaseCloudResource, BaseObjectLifeCycleMixin, FloatingIP):
                 self._provider == other._provider and
                 self.id == other.id)
 
+    def delete(self):
+        self._provider.networking._floating_ips.delete(self._gateway, self)
+
 
 class BaseRouter(BaseCloudResource, Router):
 

+ 3 - 5
cloudbridge/cloud/providers/aws/resources.py

@@ -323,9 +323,7 @@ class AWSInstance(BaseInstance):
 
     def _get_fip(self, floating_ip):
         """Get a floating IP object based on the supplied allocation ID."""
-        return AWSFloatingIP(
-            self._provider, list(self._provider.ec2_conn.vpc_addresses.filter(
-                AllocationIds=[floating_ip]))[0])
+        return self._provider.networking._floating_ips.get(None, floating_ip)
 
     def add_floating_ip(self, floating_ip):
         fip = (floating_ip if isinstance(floating_ip, AWSFloatingIP)
@@ -983,8 +981,8 @@ class AWSSubnet(BaseSubnet):
 
 class AWSFloatingIP(BaseFloatingIP):
 
-    def __init__(self, provider, floating_ip):
-        super(AWSFloatingIP, self).__init__(provider)
+    def __init__(self, provider, gateway, floating_ip):
+        super(AWSFloatingIP, self).__init__(provider, gateway)
         self._ip = floating_ip
 
     @property

+ 1 - 0
cloudbridge/cloud/providers/aws/services.py

@@ -1238,6 +1238,7 @@ class AWSFloatingIPService(BaseFloatingIPService):
             Domain='vpc')
         return AWSFloatingIP(
             self.provider,
+            gateway,
             self.provider.ec2_conn.VpcAddress(ip.get('AllocationId')))
 
     @dispatch(event="provider.networking.floating_ips.delete",

+ 3 - 3
cloudbridge/cloud/providers/azure/resources.py

@@ -819,10 +819,10 @@ class AzureNetwork(BaseNetwork):
 
 class AzureFloatingIP(BaseFloatingIP):
 
-    def __init__(self, provider, floating_ip, network_id):
-        super(AzureFloatingIP, self).__init__(provider)
+    def __init__(self, provider, gateway, floating_ip):
+        super(AzureFloatingIP, self).__init__(provider, gateway)
         self._ip = floating_ip
-        self._network_id = network_id
+        self._network_id = gateway.network_id
 
     @property
     def id(self):

+ 4 - 4
cloudbridge/cloud/providers/azure/services.py

@@ -1340,8 +1340,7 @@ class AzureFloatingIPService(BaseFloatingIPService):
     @dispatch(event="provider.networking.floating_ips.list",
               priority=BaseFloatingIPService.STANDARD_EVENT_PRIORITY)
     def list(self, gateway, limit=None, marker=None):
-        floating_ips = [AzureFloatingIP(self.provider, floating_ip,
-                                        gateway.network_id)
+        floating_ips = [AzureFloatingIP(self.provider, gateway, floating_ip)
                         for floating_ip in self.provider.azure_client.
                         list_floating_ips()]
         return ClientPagedResultList(self.provider, floating_ips,
@@ -1355,11 +1354,12 @@ class AzureFloatingIPService(BaseFloatingIPService):
             'public_ip_allocation_method': 'Static'
         }
 
-        public_ip_name = AzureFloatingIP._generate_name_from_label('cb-fip-')
+        public_ip_name = AzureFloatingIP._generate_name_from_label(
+            None, 'cb-fip-')
 
         floating_ip = self.provider.azure_client.\
             create_floating_ip(public_ip_name, public_ip_parameters)
-        return AzureFloatingIP(self.provider, floating_ip, gateway.network_id)
+        return AzureFloatingIP(self.provider, gateway, floating_ip)
 
     @dispatch(event="provider.networking.floating_ips.delete",
               priority=BaseFloatingIPService.STANDARD_EVENT_PRIORITY)

+ 1 - 2
cloudbridge/cloud/providers/gce/resources.py

@@ -1327,8 +1327,7 @@ class GCEFloatingIP(BaseFloatingIP):
     _DEAD_INSTANCE = 'dead instance'
 
     def __init__(self, provider, gateway, floating_ip):
-        super(GCEFloatingIP, self).__init__(provider)
-        self._gateway = gateway
+        super(GCEFloatingIP, self).__init__(provider, gateway)
         self._ip = floating_ip
         self._process_ip_users()
 

+ 1 - 1
cloudbridge/cloud/providers/gce/services.py

@@ -1570,7 +1570,7 @@ class GCEFloatingIPService(BaseFloatingIPService):
                             body={'name': ip_name})
                     .execute())
         self.provider.wait_for_operation(response, region=region_name)
-        return self.get(ip_name)
+        return self.get(gateway, ip_name)
 
     @dispatch(event="provider.networking.floating_ips.delete",
               priority=BaseFloatingIPService.STANDARD_EVENT_PRIORITY)

+ 3 - 5
cloudbridge/cloud/providers/openstack/resources.py

@@ -443,9 +443,7 @@ class OpenStackInstance(BaseInstance):
 
     def _get_fip(self, floating_ip):
         """Get a floating IP object based on the supplied ID."""
-        return OpenStackFloatingIP(
-            self._provider,
-            self._provider.os_conn.network.get_ip(floating_ip))
+        return self._provider.networking._floating_ips.get(floating_ip)
 
     def add_floating_ip(self, floating_ip):
         """
@@ -905,8 +903,8 @@ class OpenStackSubnet(BaseSubnet):
 
 class OpenStackFloatingIP(BaseFloatingIP):
 
-    def __init__(self, provider, floating_ip):
-        super(OpenStackFloatingIP, self).__init__(provider)
+    def __init__(self, provider, gateway, floating_ip):
+        super(OpenStackFloatingIP, self).__init__(provider, gateway)
         self._ip = floating_ip
 
     @property

+ 4 - 3
cloudbridge/cloud/providers/openstack/services.py

@@ -1191,7 +1191,8 @@ class OpenStackFloatingIPService(BaseFloatingIPService):
     def get(self, gateway, fip_id):
         try:
             return OpenStackFloatingIP(
-                self.provider, self.provider.os_conn.network.get_ip(fip_id))
+                self.provider, gateway,
+                self.provider.os_conn.network.get_ip(fip_id))
         except (ResourceNotFound, NotFoundException):
             log.debug("Floating IP %s not found.", fip_id)
             return None
@@ -1199,7 +1200,7 @@ class OpenStackFloatingIPService(BaseFloatingIPService):
     @dispatch(event="provider.networking.floating_ips.list",
               priority=BaseFloatingIPService.STANDARD_EVENT_PRIORITY)
     def list(self, gateway, limit=None, marker=None):
-        fips = [OpenStackFloatingIP(self.provider, fip)
+        fips = [OpenStackFloatingIP(self.provider, gateway, fip)
                 for fip in self.provider.os_conn.network.ips(
                     floating_network_id=gateway.id
                 )]
@@ -1210,7 +1211,7 @@ class OpenStackFloatingIPService(BaseFloatingIPService):
               priority=BaseFloatingIPService.STANDARD_EVENT_PRIORITY)
     def create(self, gateway):
         return OpenStackFloatingIP(
-            self.provider, self.provider.os_conn.network.create_ip(
+            self.provider, gateway, self.provider.os_conn.network.create_ip(
                 floating_network_id=gateway.id))
 
     @dispatch(event="provider.networking.floating_ips.delete",