The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/pylxd/pull/388

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
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.tra...@canonical.com>
From e993878b50c1681ccb5697bc537dc7958135783c Mon Sep 17 00:00:00 2001
From: Lee Trager <lee.tra...@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.tra...@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"]}
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to