summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--glanceclient/v2/client.py9
-rw-r--r--glanceclient/v2/images.py25
-rw-r--r--glanceclient/v2/schemas.py5
-rw-r--r--tests/v2/test_images.py15
-rw-r--r--tests/v2/test_schemas.py5
-rw-r--r--tools/pip-requires1
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