diff options
author | Brian Waldon <bcwaldon@gmail.com> | 2012-07-14 01:11:22 +0000 |
---|---|---|
committer | Brian Waldon <bcwaldon@gmail.com> | 2012-07-13 18:38:41 -0700 |
commit | c398af18b0b8fb5fb075be22563812e179290b2a (patch) | |
tree | bd5d428318e6e1e64a5289d581e7286786e52a0a | |
parent | b6cef9d145f870dd717843751f0c5d68867e07d5 (diff) | |
download | python-glanceclient-c398af18b0b8fb5fb075be22563812e179290b2a.tar.gz |
Replace static v2 Image model with warlock model
* Add warlock v0.1.0 as a dependency
* Generate a pythonic, self-validating Image model using warlock
* Add raw method to Schema model
* Related to bp glance-client-v2
Change-Id: Ib98e912a7af0bb570b4fd738733edd9b837d1a04
-rw-r--r-- | glanceclient/v2/client.py | 9 | ||||
-rw-r--r-- | glanceclient/v2/images.py | 25 | ||||
-rw-r--r-- | glanceclient/v2/schemas.py | 5 | ||||
-rw-r--r-- | tests/v2/test_images.py | 15 | ||||
-rw-r--r-- | tests/v2/test_schemas.py | 5 | ||||
-rw-r--r-- | tools/pip-requires | 1 |
6 files changed, 37 insertions, 23 deletions
diff --git a/glanceclient/v2/client.py b/glanceclient/v2/client.py index 0435f64..cc0c383 100644 --- a/glanceclient/v2/client.py +++ b/glanceclient/v2/client.py @@ -15,6 +15,8 @@ import logging +import warlock + from glanceclient.common import http from glanceclient.v2 import images from glanceclient.v2 import schemas @@ -36,5 +38,10 @@ class Client(object): def __init__(self, endpoint, token=None, timeout=600, **kwargs): self.http_client = http.HTTPClient( endpoint, token=token, timeout=timeout) - self.images = images.Controller(self.http_client) self.schemas = schemas.Controller(self.http_client) + self.images = images.Controller(self.http_client, + self._get_image_model()) + + def _get_image_model(self): + schema = self.schemas.get('image') + return warlock.model_factory(schema.raw()) diff --git a/glanceclient/v2/images.py b/glanceclient/v2/images.py index 660e259..e5de052 100644 --- a/glanceclient/v2/images.py +++ b/glanceclient/v2/images.py @@ -14,24 +14,25 @@ # under the License. -class Image(object): - def __init__(self, id, name): - self.id = id - self.name = name - - def iteritems(self): - return {'id': self.id, 'name': self.name}.iteritems() - - class Controller(object): - def __init__(self, http_client): + def __init__(self, http_client, model): self.http_client = http_client + self.model = model def list(self): resp, body = self.http_client.json_request('GET', '/v2/images') - return [Image(i['id'], i['name']) for i in body['images']] + images = [] + for image in body['images']: + #NOTE(bcwaldon): remove 'self' for now until we have an elegant + # way to pass it into the model constructor without conflict + image.pop('self', None) + images.append(self.model(**image)) + return images def get(self, image_id): url = '/v2/images/%s' % image_id resp, body = self.http_client.json_request('GET', url) - return Image(body['image']['id'], body['image']['name']) + #NOTE(bcwaldon): remove 'self' for now until we have an elegant + # way to pass it into the model constructor without conflict + body['image'].pop('self', None) + return self.model(**body['image']) diff --git a/glanceclient/v2/schemas.py b/glanceclient/v2/schemas.py index b4ed61c..6e178d2 100644 --- a/glanceclient/v2/schemas.py +++ b/glanceclient/v2/schemas.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +import copy + from glanceclient import exc @@ -40,6 +42,9 @@ class Schema(object): raw_properties = raw_schema['properties'] self.properties = translate_schema_properties(raw_properties) + def raw(self): + return copy.deepcopy(self._raw_schema) + class Controller(object): def __init__(self, http_client): diff --git a/tests/v2/test_images.py b/tests/v2/test_images.py index f6f1292..471c9ee 100644 --- a/tests/v2/test_images.py +++ b/tests/v2/test_images.py @@ -15,6 +15,8 @@ import unittest +import warlock + from glanceclient.v2 import images from tests import utils @@ -49,22 +51,15 @@ fixtures = { } -class TestImage(unittest.TestCase): - def test_image_minimum(self): - raw_image = { - 'id': '8a5b2424-9751-498b-925f-66f62747c501', - 'name': 'image-7', - } - image = images.Image(**raw_image) - self.assertEqual(image.id, '8a5b2424-9751-498b-925f-66f62747c501') - self.assertEqual(image.name, 'image-7') +fake_schema = {'name': 'image', 'properties': {'id': {}, 'name': {}}} +FakeModel = warlock.model_factory(fake_schema) class TestController(unittest.TestCase): def setUp(self): super(TestController, self).setUp() self.api = utils.FakeAPI(fixtures) - self.controller = images.Controller(self.api) + self.controller = images.Controller(self.api, FakeModel) def test_list_images(self): images = self.controller.list() diff --git a/tests/v2/test_schemas.py b/tests/v2/test_schemas.py index 6e64c29..b11b480 100644 --- a/tests/v2/test_schemas.py +++ b/tests/v2/test_schemas.py @@ -67,6 +67,11 @@ class TestSchema(unittest.TestCase): self.assertEqual(schema.name, 'Country') self.assertEqual([p.name for p in schema.properties], ['size']) + def test_raw(self): + raw_schema = {'name': 'Country', 'properties': {}} + schema = schemas.Schema(raw_schema) + self.assertEqual(schema.raw(), raw_schema) + class TestController(unittest.TestCase): def setUp(self): diff --git a/tools/pip-requires b/tools/pip-requires index f517a02..eec261e 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -2,3 +2,4 @@ argparse httplib2 prettytable==0.6 python-keystoneclient>=0.1,<0.2 +warlock==0.1.0 |