moved rest of build logic into builder

This commit is contained in:
William Wolf
2011-07-27 16:34:02 -04:00
parent 2ac60cd773
commit f2d8e91b83
3 changed files with 52 additions and 63 deletions

View File

@@ -135,49 +135,13 @@ class Versions(wsgi.Resource):
def dispatch(self, request, *args):
"""Respond to a request for all OpenStack API versions."""
builder = nova.api.openstack.views.versions.get_view_builder(request)
if request.path == '/':
# List Versions
return self._versions_list(request)
return builder.build(VERSIONS)
else:
# Versions Multiple Choice
return self._versions_multi_choice(request)
def _versions_list(self, request):
version_objs = []
for version in VERSIONS:
version = VERSIONS[version]['version']
version_objs.append({
"id": version['id'],
"status": version['status'],
"updated": version['updated'],
})
builder = nova.api.openstack.views.versions.get_view_builder(request)
versions = [builder.build(version) for version in version_objs]
return dict(versions=versions)
def _versions_multi_choice(self, request):
#TODO
version_objs = []
for version in VERSIONS:
version = VERSIONS[version]['version']
version_objs.append({
"id": version['id'],
"status": version['status'],
"links": [
{
"rel": "self"
}
],
"media-types": version['media-types']
})
builder = nova.api.openstack.views.versions.get_view_builder(request)
choices = [
builder.build_choices(version, request)
for version in version_objs]
return dict(choices=choices)
return builder.build_choices(VERSIONS, request)
class VersionV10(object):

View File

@@ -31,21 +31,37 @@ class ViewBuilder(object):
"""
self.base_url = base_url
def build_choices(self, version_data, request):
version_data['links'][0]['href'] = self._build_versioned_link(request,
version_data['id'])
return version_data
def build_choices(self, VERSIONS, request):
version_objs = []
for version in VERSIONS:
version = VERSIONS[version]['version']
version_objs.append({
"id": version['id'],
"status": version['status'],
"links": [
{
"rel": "self",
"href": self._build_versioned_link(request,
version['id'])
}
],
"media-types": version['media-types']
})
def build(self, version_data):
"""Generic method used to generate a version entity."""
version = {
"id": version_data["id"],
"status": version_data["status"],
"updated": version_data["updated"],
"links": self._build_links(version_data),
}
return dict(choices=version_objs)
return version
def build(self, VERSIONS):
version_objs = []
for version in VERSIONS:
version = VERSIONS[version]['version']
version_objs.append({
"id": version['id'],
"status": version['status'],
"updated": version['updated'],
"links": self._build_links(version),
})
return dict(versions=version_objs)
def _build_versioned_link(self, req, version):
return '%s://%s/%s%s' % (req.scheme, req.host, version, req.path)

View File

@@ -529,20 +529,29 @@ class VersionsTest(test.TestCase):
base_url = "http://example.org/"
version_data = {
"id": "3.2.1",
"status": "CURRENT",
"updated": "2011-07-18T11:30:00Z"}
"v3.2.1": {
"version": {
"id": "3.2.1",
"status": "CURRENT",
"updated": "2011-07-18T11:30:00Z",
}
}
}
expected = {
"id": "3.2.1",
"status": "CURRENT",
"updated": "2011-07-18T11:30:00Z",
"links": [
"versions": [
{
"rel": "self",
"href": "http://example.org/3.2.1/",
},
],
"id": "3.2.1",
"status": "CURRENT",
"updated": "2011-07-18T11:30:00Z",
"links": [
{
"rel": "self",
"href": "http://example.org/3.2.1/",
},
],
}
]
}
builder = views.versions.ViewBuilder(base_url)