The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/pylxd/pull/254
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) === `exist()` and `create()` methods have been added for StoragePool class. Appropriate tests have been added too.
From 16a7d1f6a3cd0d9d617a05266a436d201641b34e Mon Sep 17 00:00:00 2001 From: Rufus Deponian <[email protected]> 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 <[email protected]> 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')
_______________________________________________ lxc-devel mailing list [email protected] http://lists.linuxcontainers.org/listinfo/lxc-devel
