libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From quent...@apache.org
Subject libcloud git commit: azure_arm: Fix api version used to list and delete NICs
Date Thu, 12 Oct 2017 04:30:08 GMT
Repository: libcloud
Updated Branches:
  refs/heads/trunk a1c0e60fc -> 3ffac42c2


azure_arm: Fix api version used to list and delete NICs

Adds ex_destroy_nic.

Also adjust codes to reflect that Azure returns 204 when attempting to
destroy a resource that doesn't exist.

Closes #1128

Signed-off-by: Quentin Pradet <quentinp@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/3ffac42c
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/3ffac42c
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/3ffac42c

Branch: refs/heads/trunk
Commit: 3ffac42c2a950cb3ea0ad48f0cf67c2f194f5d41
Parents: a1c0e60
Author: Peter Amstutz <peter.amstutz@curoverse.com>
Authored: Wed Oct 11 10:24:43 2017 -0400
Committer: Quentin Pradet <quentinp@apache.org>
Committed: Thu Oct 12 08:29:45 2017 +0400

----------------------------------------------------------------------
 CHANGES.rst                             |  3 ++
 libcloud/compute/drivers/azure_arm.py   | 54 +++++++++++++++++++---------
 libcloud/test/compute/test_azure_arm.py | 12 +++----
 3 files changed, 47 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ffac42c/CHANGES.rst
----------------------------------------------------------------------
diff --git a/CHANGES.rst b/CHANGES.rst
index 9e37c80..e45a279 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -55,6 +55,9 @@ Compute
   (GITHUB-1120)
   [Lucas Di Pentima]
 
+- [ARM] Fix api version used to list and delete NICs (GITHUB-1128)
+  [Peter Amstutz]
+
 
 Changes in Apache Libcloud 2.2.1
 --------------------------------

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ffac42c/libcloud/compute/drivers/azure_arm.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/azure_arm.py b/libcloud/compute/drivers/azure_arm.py
index 3e5a3f1..78b97b4 100644
--- a/libcloud/compute/drivers/azure_arm.py
+++ b/libcloud/compute/drivers/azure_arm.py
@@ -700,7 +700,7 @@ class AzureNodeDriver(NodeDriver):
         :rtype: ``bool``
         """
 
-        do_node_polling = True
+        do_node_polling = (ex_destroy_nic or ex_destroy_vhd)
 
         # This returns a 202 (Accepted) which means that the delete happens
         # asynchronously.
@@ -714,13 +714,14 @@ class AzureNodeDriver(NodeDriver):
         except BaseHTTPError as h:
             if h.code == 202:
                 pass
-            elif h.code == 404:
-                # No need to ask again, node already down.
+            elif h.code == 204:
+                # Returns 204 if node already deleted.
                 do_node_polling = False
             else:
                 raise
 
-        # Need to poll until the node actually goes away.
+        # Poll until the node actually goes away (otherwise attempt to delete
+        # NIC and VHD will fail with "resource in use" errors).
         while do_node_polling:
             try:
                 time.sleep(10)
@@ -728,7 +729,8 @@ class AzureNodeDriver(NodeDriver):
                     node.id,
                     params={"api-version": RESOURCE_API_VERSION})
             except BaseHTTPError as h:
-                if h.code == 404:
+                if h.code in (204, 404):
+                    # Node is gone
                     break
                 else:
                     raise
@@ -741,16 +743,11 @@ class AzureNodeDriver(NodeDriver):
             for nic in interfaces:
                 while True:
                     try:
-                        self.connection.request(
-                            nic["id"],
-                            params={"api-version": RESOURCE_API_VERSION},
-                            method='DELETE')
+                        self.ex_destroy_nic(self._to_nic(nic))
                         break
                     except BaseHTTPError as h:
-                        if h.code == 202 or h.code == 404:
-                            break
-                        inuse = h.message.startswith("[NicInUse]")
-                        if h.code == 400 and inuse:
+                        if (h.code == 400 and
+                                h.message.startswith("[NicInUse]")):
                             time.sleep(10)
                         else:
                             raise
@@ -1521,7 +1518,7 @@ class AzureNodeDriver(NodeDriver):
                      (self.subscription_id, resource_group)
         r = self.connection.request(
             action,
-            params={"api-version": RESOURCE_API_VERSION})
+            params={"api-version": "2015-06-15"})
         return [self._to_nic(net) for net in r.object["value"]]
 
     def ex_get_nic(self, id):
@@ -1538,6 +1535,31 @@ class AzureNodeDriver(NodeDriver):
         r = self.connection.request(id, params={"api-version": "2015-06-15"})
         return self._to_nic(r.object)
 
+    def ex_destroy_nic(self, nic):
+        """
+        Destroy a NIC.
+
+        :param id: The NIC to destroy.
+        :type id: ``.AzureNic``
+
+        :return: True on success
+        :rtype: ``bool``
+        """
+
+        try:
+            self.connection.request(
+                nic.id,
+                params={"api-version": "2015-06-15"},
+                method='DELETE')
+            return True
+        except BaseHTTPError as h:
+            if h.code in (202, 204):
+                # Deletion is accepted (but deferred), or NIC is already
+                # deleted
+                return True
+            else:
+                raise
+
     def ex_get_node(self, id):
         """
         Fetch information about a node.
@@ -2009,8 +2031,8 @@ class AzureNodeDriver(NodeDriver):
                                "maxDataDiskCount": data["maxDataDiskCount"]})
 
     def _to_nic(self, data):
-        return AzureNic(data["id"], data["name"], data["location"],
-                        data["properties"])
+        return AzureNic(data["id"], data.get("name"), data.get("location"),
+                        data.get("properties"))
 
     def _to_ip_address(self, data):
         return AzureIPAddress(data["id"], data["name"], data["properties"])

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ffac42c/libcloud/test/compute/test_azure_arm.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_azure_arm.py b/libcloud/test/compute/test_azure_arm.py
index 9e462d3..b4dd1fd 100644
--- a/libcloud/test/compute/test_azure_arm.py
+++ b/libcloud/test/compute/test_azure_arm.py
@@ -141,7 +141,7 @@ class AzureNodeDriverTests(LibcloudTestCase):
         AzureMockHttp.responses = [
             # OK to the DELETE request
             lambda f: (httplib.OK, None, {}, 'OK'),
-            # 404 means node destroyed successfully
+            # 404 means node is gone
             lambda f: error(BaseHTTPError, code=404, message='Not found'),
         ]
         ret = self.driver.destroy_node(node)
@@ -151,15 +151,15 @@ class AzureNodeDriverTests(LibcloudTestCase):
         """
         This simulates the case when destroy_node is being called for the 2nd
         time because some related resource failed to clean up, so the DELETE
-        operation on the node will return 404 (because it was already deleted)
+        operation on the node will return 204 (because it was already deleted)
         but the method should return success.
         """
         def error(e, **kwargs):
             raise e(**kwargs)
         node = self.driver.list_nodes()[0]
         AzureMockHttp.responses = [
-            # 404 (Not Found) to the DELETE request
-            lambda f: error(BaseHTTPError, code=404, message='Not found'),
+            # 204 (No content) to the DELETE request on a deleted/non-existent node
+            lambda f: error(BaseHTTPError, code=204, message='No content'),
         ]
         ret = self.driver.destroy_node(node)
         self.assertTrue(ret)
@@ -172,7 +172,7 @@ class AzureNodeDriverTests(LibcloudTestCase):
         AzureMockHttp.responses = [
             # 202 - The delete will happen asynchronously
             lambda f: error(BaseHTTPError, code=202, message='Deleting'),
-            # 404 means node destroyed successfully
+            # 404 means node is gone
             lambda f: error(BaseHTTPError, code=404, message='Not found'),
         ]
         ret = self.driver.destroy_node(node)
@@ -186,7 +186,7 @@ class AzureNodeDriverTests(LibcloudTestCase):
         AzureMockHttp.responses = [
             # OK to the DELETE request
             lambda f: (httplib.OK, None, {}, 'OK'),
-            # 404 means node destroyed successfully
+            # 404 means node is gone
             lambda f: error(BaseHTTPError, code=404, message='Not found'),
             # 500 - transient error when trying to clean up the NIC
             lambda f: error(BaseHTTPError, code=500, message="Cloud weather"),


Mime
View raw message