[lxc-devel] [pylxd/master] Add storage pool methods

deponian on Github lxc-bot at linuxcontainers.org
Wed Oct 18 07:00:15 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 413 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171018/a0892d8f/attachment.bin>
-------------- next part --------------
From 16a7d1f6a3cd0d9d617a05266a436d201641b34e Mon Sep 17 00:00:00 2001
From: Rufus Deponian <rufus.deponian at yandex.ru>
Date: Fri, 13 Oct 2017 16:18:16 +0300
Subject: [PATCH 1/2] Add create() method for StoragePool and a test for it

---
 pylxd/models/storage_pool.py       | 8 ++++++++
 pylxd/tests/mock_lxd.py            | 5 +++++
 pylxd/tests/models/test_storage.py | 8 ++++++++
 3 files changed, 21 insertions(+)

diff --git a/pylxd/models/storage_pool.py b/pylxd/models/storage_pool.py
index b8d2644..89cde82 100644
--- a/pylxd/models/storage_pool.py
+++ b/pylxd/models/storage_pool.py
@@ -46,6 +46,14 @@ def all(cls, client):
             storage_pools.append(cls(client, name=name))
         return storage_pools
 
+    @classmethod
+    def create(cls, client, config):
+        """Create a storage_pool from config."""
+        client.api.storage_pools.post(json=config)
+
+        storage_pool = cls.get(client, config['name'])
+        return storage_pool
+
     @property
     def api(self):
         return self.client.api.storage_pools[self.name]
diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index d42d2f2..6a89771 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -571,6 +571,11 @@ def profile_GET(request, context):
         'method': 'GET',
         'url': r'^http://pylxd.test/1.0/storage-pools/lxd$',
     },
+    {
+        'json': {'type': 'sync'},
+        'method': 'POST',
+        'url': r'^http://pylxd.test/1.0/storage-pools$',
+    },
 
     # Profiles
     {
diff --git a/pylxd/tests/models/test_storage.py b/pylxd/tests/models/test_storage.py
index f2373ab..cf21e85 100644
--- a/pylxd/tests/models/test_storage.py
+++ b/pylxd/tests/models/test_storage.py
@@ -38,6 +38,14 @@ def test_partial(self):
 
         self.assertEqual('zfs', an_storage_pool.driver)
 
+    def test_create(self):
+        """A new storage pool is created."""
+        config = {"config": {}, "driver": "zfs", "name": "lxd"}
+
+        an_storage_pool = models.StoragePool.create(self.client, config)
+
+        self.assertEqual(config['name'], an_storage_pool.name)
+
     def test_delete(self):
         """delete is not implemented in storage_pools."""
         an_storage_pool = models.StoragePool(self.client, name='lxd')

From 309ba1bbdf89b64e100ab5f0076e4cc7e3a3b063 Mon Sep 17 00:00:00 2001
From: Rufus Deponian <rufus.deponian at yandex.ru>
Date: Fri, 13 Oct 2017 17:54:03 +0300
Subject: [PATCH 2/2] Add exists() method for StoragePool and a tests for it

---
 pylxd/models/storage_pool.py       |  9 +++++++++
 pylxd/tests/models/test_storage.py | 26 ++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/pylxd/models/storage_pool.py b/pylxd/models/storage_pool.py
index 89cde82..caf723e 100644
--- a/pylxd/models/storage_pool.py
+++ b/pylxd/models/storage_pool.py
@@ -54,6 +54,15 @@ def create(cls, client, config):
         storage_pool = cls.get(client, config['name'])
         return storage_pool
 
+    @classmethod
+    def exists(cls, client, name):
+        """Determine whether a storage pool exists."""
+        try:
+            client.storage_pools.get(name)
+            return True
+        except cls.NotFound:
+            return False
+
     @property
     def api(self):
         return self.client.api.storage_pools[self.name]
diff --git a/pylxd/tests/models/test_storage.py b/pylxd/tests/models/test_storage.py
index cf21e85..dfbd96a 100644
--- a/pylxd/tests/models/test_storage.py
+++ b/pylxd/tests/models/test_storage.py
@@ -11,6 +11,8 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
+import json
+
 from pylxd import models
 from pylxd.tests import testing
 
@@ -46,6 +48,30 @@ def test_create(self):
 
         self.assertEqual(config['name'], an_storage_pool.name)
 
+    def test_exists(self):
+        """A storage pool exists."""
+        name = 'lxd'
+
+        self.assertTrue(models.StoragePool.exists(self.client, name))
+
+    def test_not_exists(self):
+        """A storage pool exists."""
+        def not_found(request, context):
+            context.status_code = 404
+            return json.dumps({
+                'type': 'error',
+                'error': 'Not found',
+                'error_code': 404})
+        self.add_rule({
+            'text': not_found,
+            'method': 'GET',
+            'url': r'^http://pylxd.test/1.0/storage-pools/an-missing-storage-pool$',  # NOQA
+        })
+
+        name = 'an-missing-storage-pool'
+
+        self.assertFalse(models.StoragePool.exists(self.client, name))
+
     def test_delete(self):
         """delete is not implemented in storage_pools."""
         an_storage_pool = models.StoragePool(self.client, name='lxd')


More information about the lxc-devel mailing list