libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anthonys...@apache.org
Subject [2/5] libcloud git commit: Vultr compute driver doesn't authenticate calls that don't need it
Date Sun, 18 Jun 2017 02:25:49 GMT
Vultr compute driver doesn't authenticate calls that don't need it

This minimizes the likelihood of hitting the rate-limit of Vultr api.


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

Branch: refs/heads/trunk
Commit: ee7a2c28a6ec01bc6a5af0f30a3715163974496f
Parents: aed1119
Author: Francisco Ros <fjros@doalitic.com>
Authored: Tue May 23 18:10:22 2017 +0200
Committer: Anthony Shaw <anthonyshaw@apache.org>
Committed: Sun Jun 18 12:24:08 2017 +1000

----------------------------------------------------------------------
 libcloud/compute/drivers/vultr.py   | 26 +++++++++++++++++++++++++-
 libcloud/test/compute/test_vultr.py |  8 ++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/ee7a2c28/libcloud/compute/drivers/vultr.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/vultr.py b/libcloud/compute/drivers/vultr.py
index c4d14e8..7c31dfb 100644
--- a/libcloud/compute/drivers/vultr.py
+++ b/libcloud/compute/drivers/vultr.py
@@ -57,6 +57,15 @@ class VultrConnection(ConnectionKey):
 
     host = 'api.vultr.com'
     responseCls = VultrResponse
+    unauthenticated_endpoints = {  # {path: actions}
+        '/v1/app/list': ['GET'],
+        '/v1/os/list': ['GET'],
+        '/v1/plans/list': ['GET'],
+        '/v1/plans/list_vc2': ['GET'],
+        '/v1/plans/list_vdc2': ['GET'],
+        '/v1/regions/availability': ['GET'],
+        '/v1/regions/list': ['GET']
+    }
 
     def add_default_headers(self, headers):
         """
@@ -66,7 +75,8 @@ class VultrConnection(ConnectionKey):
         :rtype: dict
         """
 
-        headers.update({'API-Key': self.key})
+        if self.require_api_key():
+            headers.update({'API-Key': self.key})
         return headers
 
     def encode_data(self, data):
@@ -79,6 +89,20 @@ class VultrConnection(ConnectionKey):
         headers = {'Content-Type': 'application/x-www-form-urlencoded'}
         return self.request(url, data=data, headers=headers, method='POST')
 
+    def require_api_key(self):
+        """
+        Check whether this call (method + action) must be authenticated.
+
+        :return: True if ``API-Key`` header required, False otherwise.
+        :rtype: bool
+        """
+
+        try:
+            return self.method \
+                   not in self.unauthenticated_endpoints[self.action]
+        except KeyError:
+            return True
+
 
 class VultrNodeDriver(NodeDriver):
     """

http://git-wip-us.apache.org/repos/asf/libcloud/blob/ee7a2c28/libcloud/test/compute/test_vultr.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_vultr.py b/libcloud/test/compute/test_vultr.py
index d416383..303f206 100644
--- a/libcloud/test/compute/test_vultr.py
+++ b/libcloud/test/compute/test_vultr.py
@@ -37,6 +37,10 @@ class VultrTests(LibcloudTestCase):
         VultrMockHttp.type = None
         self.driver = VultrNodeDriver(*VULTR_PARAMS)
 
+    def test_list_images_dont_require_api_key(self):
+        self.driver.list_images()
+        self.assertFalse(self.driver.connection.require_api_key())
+
     def test_list_images_success(self):
         images = self.driver.list_images()
         self.assertTrue(len(images) >= 1)
@@ -67,6 +71,10 @@ class VultrTests(LibcloudTestCase):
         self.assertEqual(location.id, '1')
         self.assertEqual(location.name, 'New Jersey')
 
+    def test_list_nodes_require_api_key(self):
+        self.driver.list_nodes()
+        self.assertTrue(self.driver.connection.require_api_key())
+
     def test_list_nodes_success(self):
         nodes = self.driver.list_nodes()
         self.assertEqual(len(nodes), 2)


Mime
View raw message