Переглянути джерело

Allow floating IP ID to be supplied when attaching one to an instance

Enis Afgan 8 роки тому
батько
коміт
2ac9ab9efe

+ 6 - 4
cloudbridge/cloud/interfaces/resources.py

@@ -583,11 +583,12 @@ class Instance(ObjectLifeCycleMixin, CloudResource):
         """
         Add a public IP address to this instance.
 
-        :type floating_ip: :class:``.FloatingIP``
+        :type floating_ip: :class:``.FloatingIP`` or floating IP ID
         :param floating_ip: The FloatingIP object to associate with the
                             instance. Note that is not the actual public IP
                             address but the CloudBridge object encapsulating
-                            the IP.
+                            the IP or the respective provider ID that
+                            identifies the address.
         """
         pass
 
@@ -596,11 +597,12 @@ class Instance(ObjectLifeCycleMixin, CloudResource):
         """
         Remove a public IP address from this instance.
 
-        :type floating_ip: :class:``.FloatingIP``
+        :type floating_ip: :class:``.FloatingIP`` or floating IP ID
         :param floating_ip: The FloatingIP object to remove from the
                             instance. Note that is not the actual public IP
                             address but the CloudBridge object encapsulating
-                            the IP.
+                            the IP or the respective provider ID that
+                            identifies the address.
         """
         pass
 

+ 14 - 4
cloudbridge/cloud/providers/aws/resources.py

@@ -298,22 +298,32 @@ class AWSInstance(BaseInstance):
         image.refresh()
         return image
 
+    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])
+
     def add_floating_ip(self, floating_ip):
+        fip = (floating_ip if isinstance(floating_ip, AWSFloatingIP)
+               else self._get_fip(floating_ip))
         params = trim_empty_params({
             'InstanceId': self.id,
             'PublicIp': None if self._ec2_instance.vpc_id else
-            floating_ip.public_ip,
+            fip.public_ip,
             # pylint:disable=protected-access
-            'AllocationId': floating_ip._ip.allocation_id})
+            'AllocationId': fip._ip.allocation_id})
         self._provider.ec2_conn.meta.client.associate_address(**params)
         self.refresh()
 
     def remove_floating_ip(self, floating_ip):
+        fip = (floating_ip if isinstance(floating_ip, AWSFloatingIP)
+               else self._get_fip(floating_ip))
         params = trim_empty_params({
             'PublicIp': None if self._ec2_instance.vpc_id else
-            floating_ip.public_ip,
+            fip.public_ip,
             # pylint:disable=protected-access
-            'AssociationId': floating_ip._ip.association_id})
+            'AssociationId': fip._ip.association_id})
         self._provider.ec2_conn.meta.client.disassociate_address(**params)
         self.refresh()
 

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

@@ -1480,7 +1480,7 @@ class AzureInstance(BaseInstance):
         """
         nic = self._provider.azure_client.get_nic(self._nic_ids[0])
         nic.ip_configurations[0].public_ip_address = {
-            'id': floating_ip.id
+            'id': floating_ip
         }
         self._provider.azure_client.update_nic(self._nic_ids[0], nic)
 
@@ -1490,7 +1490,7 @@ class AzureInstance(BaseInstance):
         """
         nic = self._provider.azure_client.get_nic(self._nic_ids[0])
         for ip_config in nic.ip_configurations:
-            if ip_config.public_ip_address.id == floating_ip.id:
+            if ip_config.public_ip_address.id == floating_ip:
                 nic.ip_configurations[0].public_ip_address = None
                 self._provider.azure_client.update_nic(self._nic_ids[0],
                                                        nic)

+ 12 - 2
cloudbridge/cloud/providers/openstack/resources.py

@@ -398,19 +398,29 @@ class OpenStackInstance(BaseInstance):
         return OpenStackMachineImage(
             self._provider, self._provider.compute.images.get(image_id))
 
+    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))
+
     def add_floating_ip(self, floating_ip):
         """
         Add a floating IP address to this instance.
         """
         log.debug("Adding floating IP adress: %s", floating_ip)
-        self._os_instance.add_floating_ip(floating_ip.public_ip)
+        fip = (floating_ip if isinstance(floating_ip, OpenStackFloatingIP)
+               else self._get_fip(floating_ip))
+        self._os_instance.add_floating_ip(fip.public_ip)
 
     def remove_floating_ip(self, floating_ip):
         """
         Remove a floating IP address from this instance.
         """
         log.debug("Removing floating IP adress: %s", floating_ip)
-        self._os_instance.remove_floating_ip(floating_ip.public_ip)
+        fip = (floating_ip if isinstance(floating_ip, OpenStackFloatingIP)
+               else self._get_fip(floating_ip))
+        self._os_instance.remove_floating_ip(fip.public_ip)
 
     def add_vm_firewall(self, firewall):
         """