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
This commit is contained in:
parent
b6cef9d145
commit
c398af18b0
|
@ -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())
|
||||
|
|
|
@ -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'])
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -2,3 +2,4 @@ argparse
|
|||
httplib2
|
||||
prettytable==0.6
|
||||
python-keystoneclient>=0.1,<0.2
|
||||
warlock==0.1.0
|
||||
|
|
Loading…
Reference in New Issue