[lxc-devel] [pylxd/master] Add support for the resources endpoint.

ltrager on Github lxc-bot at linuxcontainers.org
Fri Feb 28 08:46:15 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 510 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200228/cf400b87/attachment.bin>
-------------- next part --------------
From e993878b50c1681ccb5697bc537dc7958135783c Mon Sep 17 00:00:00 2001
From: Lee Trager <lee.trager at canonical.com>
Date: Fri, 28 Feb 2020 08:41:59 +0000
Subject: [PATCH] Add support for the resources endpoint.

The LXD resources endpoint returns hardware information about LXD host. This
data is retrieved only when called. The data is cached as it won't change.

Signed-off-by: Lee Trager <lee.trager at canonical.com>
---
 pylxd/client.py            | 10 ++++++++++
 pylxd/tests/test_client.py | 25 +++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/pylxd/client.py b/pylxd/client.py
index aa5118ca..c79c3a7a 100644
--- a/pylxd/client.py
+++ b/pylxd/client.py
@@ -322,11 +322,21 @@ def __init__(
         self.operations = managers.OperationManager(self)
         self.profiles = managers.ProfileManager(self)
         self.storage_pools = managers.StoragePoolManager(self)
+        self._resource_cache = None
 
     @property
     def trusted(self):
         return self.host_info['auth'] == 'trusted'
 
+    @property
+    def resources(self):
+        if self._resource_cache is None:
+            response = self.api.resources.get()
+            if response.status_code != 200:
+                raise exceptions.ClientConnectionFailed()
+            self._resource_cache = response.json()['metadata']
+        return self._resource_cache
+
     def has_api_extension(self, name):
         """Return True if the `name` api extension exists.
 
diff --git a/pylxd/tests/test_client.py b/pylxd/tests/test_client.py
index 4e954f83..49a66f46 100644
--- a/pylxd/tests/test_client.py
+++ b/pylxd/tests/test_client.py
@@ -280,6 +280,31 @@ def powerset(types):
             else:
                 self.assertEqual(expect_resource.query, actual_resource.query)
 
+    def test_resources(self):
+        a_client = client.Client()
+        response = mock.MagicMock(status_code=200)
+        response.json.return_value = {'metadata': {
+            'cpu': {},
+        }}
+        self.get.return_value = response
+        self.assertIn('cpu', a_client.resources)
+
+    def test_resources_raises_exception(self):
+        a_client = client.Client()
+        response = mock.MagicMock(status_code=400)
+        self.get.return_value = response
+        with self.assertRaises(exceptions.ClientConnectionFailed):
+            a_client.resources
+
+    def test_resources_uses_cache(self):
+        a_client = client.Client()
+        a_client._resource_cache = {'cpu': {}}
+        # Client.__init__ calls get, reset the mock before trying
+        # resources to confirm it wasn't called.
+        self.get.called = False
+        self.assertIn('cpu', a_client.resources)
+        self.assertFalse(self.get.called)
+
     def test_has_api_extension(self):
         a_client = client.Client()
         a_client.host_info = {'api_extensions': ["one", "two"]}


More information about the lxc-devel mailing list