You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by GitBox <gi...@apache.org> on 2022/03/03 20:41:23 UTC

[GitHub] [libcloud] dpeschman commented on a change in pull request #1638: OpenStack: Move floating IP functions to use network service instead of nova

dpeschman commented on a change in pull request #1638:
URL: https://github.com/apache/libcloud/pull/1638#discussion_r819043873



##########
File path: libcloud/compute/drivers/openstack.py
##########
@@ -4441,6 +4510,64 @@ def __repr__(self):
         )
 
 
+class OpenStack_2_FloatingIpAddress(OpenStack_1_1_FloatingIpAddress):
+    """
+    Floating IP info 2.0.
+    """
+
+    def __init__(self, id, ip_address, pool, node_id=None, driver=None, extra=None):
+        self.id = str(id)
+        self.ip_address = ip_address
+        self.pool = pool
+        self.node_id = node_id
+        self.driver = driver
+        self.extra = extra if extra else {}
+
+    def get_pool(self):
+        if not self.pool:
+            try:
+                # If pool is not set, get the info from the floating_network_id
+                net = self.driver.ex_get_network(self.extra["floating_network_id"])
+            except Exception:
+                net = None
+            if net:
+                self.pool = OpenStack_2_FloatingIpPool(
+                    net.id, net.name, self.driver.network_connection
+                )
+        return self.pool
+
+    def get_node_id(self):
+        if not self.node_id:
+            # if not is not set, get it from port_details

Review comment:
       "if not" > "if node_id"

##########
File path: CHANGES.rst
##########
@@ -25,6 +25,20 @@ Compute
   (GITHUB-1629)
   [Miguel Caballer - @micafer]
 
+- [OpenStack] OpenStack: Move floating IP functions to use network service
+  instead of nova.
+
+  This change affects all the floating ip related functions of the
+  ``OpenStack_2_NodeDriver`` class. Two new classes have been added
+  ``OpenStack_2_FloatingIpPool`` and ``OpenStack_2_FloatingIpAddress``.
+  The main change applies to the FloatingIP class where ``node_id``
+  property cannot be directly obtained from FloatingIP information and it
+  must be get from the related Port information with the ``get_node_id``

Review comment:
       I think "must be get" should be "must be got" or "must be gotten".

##########
File path: libcloud/compute/drivers/openstack.py
##########
@@ -4330,6 +4331,74 @@ def ex_del_server_group(self, server_group):
         )
         return resp.status in (httplib.NO_CONTENT, httplib.ACCEPTED)
 
+    def _to_floating_ips(self, obj):
+        ip_elements = obj["floatingips"]
+        return [self._to_floating_ip(ip) for ip in ip_elements]
+
+    def _to_floating_ip(self, obj):
+        extra = {}
+
+        # In neutron version prior to 13.0.0 port_details does not exists
+        extra["port_details"] = obj.get("port_details")
+        extra["port_id"] = obj.get("port_id")
+        extra["floating_network_id"] = obj.get("floating_network_id")
+
+        return OpenStack_2_FloatingIpAddress(
+            id=obj["id"],
+            ip_address=obj["floating_ip_address"],
+            pool=None,
+            node_id=None,
+            driver=self,
+            extra=extra,
+        )
+
+    def ex_list_floating_ips(self):
+        """
+        List floating IPs
+        :rtype: ``list`` of :class:`OpenStack_2_FloatingIpAddress`
+        """
+        return self._to_floating_ips(
+            self.network_connection.request("/v2.0/floatingips").object
+        )
+
+    def ex_get_floating_ip(self, ip):
+        """
+        Get specified floating IP from the pool
+        :param      ip: floating IP to get
+        :type       ip: ``str``
+        :rtype: :class:`OpenStack_2_FloatingIpAddress`
+        """
+        floating_ips = self._to_floating_ips(
+            self.network_connection.request(
+                "/v2.0/floatingips?floating_ip_address" "=%s" % ip
+            ).object
+        )
+        return floating_ips[0] if floating_ips else None
+
+    def ex_create_floating_ip(self, ip_pool):
+        """
+        Create new floating IP. The ip_pool attribute is optional only if your
+        infrastructure has only one IP pool available.
+        :param      ip_pool: name or id of the floating IP pool
+        :type       ip_pool: ``str``
+        :rtype: :class:`OpenStack_2_FloatingIpAddress`
+        """
+        for pool in self.ex_list_floating_ip_pools():
+            if ip_pool == pool.name or ip_pool == pool.id:

Review comment:
       If ip_pool is None, as the docstring suggests should be possible if the infrastructure has only one pool, I do not think this implementation would create the IP.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@libcloud.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org