Refactor api

- renaming v1.py to cluster.py
- moving cluster.py to v1 module
- adding v1 controller to support querying version v1
- required some changes in api tests for refactoring
- adding test cases for root and v1 controller

Closes-Bug: 1426142
Change-Id: I27091bf13934864e89cb6dc8bbdb55296b2a67c7
This commit is contained in:
Abitha Palaniappan
2015-02-25 13:32:34 -08:00
parent fe53ce2c2f
commit 31e6562b6e
10 changed files with 180 additions and 46 deletions

View File

@@ -64,6 +64,7 @@ class Root(base.APIBase):
@classmethod @classmethod
def convert(self): def convert(self):
"""Builds link to v1 controller."""
root = Root() root = Root()
root.name = "OpenStack Cue API" root.name = "OpenStack Cue API"
root.description = ("Cue is an OpenStack project which aims to " root.description = ("Cue is an OpenStack project which aims to "

View File

@@ -0,0 +1,78 @@
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Version 1 of the Cue API
"""
import pecan
from pecan import rest
from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan
from cue.api.controllers import base
from cue.api.controllers import link
from cue.api.controllers.v1 import cluster
class V1(base.APIBase):
"""The representation of the version 1 of the API."""
id = wtypes.text
"""The ID of the version, also acts as the release number"""
status = wtypes.text
"""The status of this version"""
clusters = [link.Link]
"""Links to the clusters resource"""
@staticmethod
def convert():
"""Builds link to clusters controller."""
v1 = V1()
v1.id = "v1"
v1.status = "Stable"
v1.clusters = [link.Link.make_link('self', pecan.request.host_url,
'clusters', ''),
link.Link.make_link('bookmark',
pecan.request.host_url, v1.id,
'clusters',
bookmark=True)
]
return v1
class V1Controller(rest.RestController):
"""Version 1 Cue API controller root."""
_versions = ['v1']
"All supported API versions"
_default_version = 'v1'
"The default API version"
clusters = cluster.ClustersController()
@wsme_pecan.wsexpose(V1)
def get(self):
# NOTE: The reason why convert() it's being called for every
# request is because we need to get the host url from
# the request object to make the links.
return V1.convert()
@pecan.expose()
def _route(self, args):
return super(V1Controller, self)._route(args)

View File

@@ -271,8 +271,3 @@ class ClustersController(rest.RestController):
@pecan.expose() @pecan.expose()
def _lookup(self, cluster_id, *remainder): def _lookup(self, cluster_id, *remainder):
return ClusterController(cluster_id), remainder return ClusterController(cluster_id), remainder
class V1Controller(object):
"""Version 1 MSGaaS API controller root."""
clusters = ClustersController()

View File

@@ -0,0 +1,27 @@
# Copyright 2013 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from cue.tests import api
class TestRootController(api.FunctionalTest):
def test_get_root(self):
"""test case for get in root controller."""
data = self.get_json('/')
self.assertEqual('v1', data['default_version']['id'])
self.assertEqual('STABLE', data['default_version']['status'])
# Check fields are not empty
[self.assertNotIn(f, ['', []]) for f in data.keys()]

View File

View File

@@ -0,0 +1,33 @@
# Copyright 2013 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from cue.tests import api
class TestV1Controller(api.FunctionalTest):
def test_get_v1_root(self):
"""test case for get in v1 controller."""
data = self.get_json('/v1')
self.assertEqual('v1', data['id'])
# Check fields are not empty
for f in data.keys():
self.assertNotIn(f, ['', []])
# Check if all known resources are present and there are no extra ones.
not_resources = ('id', 'status')
actual_resource = list(set(data.keys()) - set(not_resources))
expected_resource = ['clusters']
self.assertEqual(expected_resource, actual_resource)

View File

@@ -23,7 +23,7 @@ import mock
from oslo.utils import timeutils from oslo.utils import timeutils
import testtools import testtools
from cue.api.controllers import v1 from cue.api.controllers.v1 import cluster
from cue.common import exception from cue.common import exception
from cue.db import api as db_api from cue.db import api as db_api
from cue.db.sqlalchemy import models from cue.db.sqlalchemy import models
@@ -118,7 +118,7 @@ class ClusterObjectsTests(base.TestCase):
api_cluster = test_utils.create_api_test_cluster_all() api_cluster = test_utils.create_api_test_cluster_all()
object_cluster = objects.Cluster(**api_cluster.as_dict()) object_cluster = objects.Cluster(**api_cluster.as_dict())
self.validate_cluster_values(api_cluster, object_cluster) self.validate_cluster_values(api_cluster, object_cluster)
api_cluster_2 = v1.ClusterDetails(**object_cluster.as_dict()) api_cluster_2 = cluster.ClusterDetails(**object_cluster.as_dict())
self.validate_cluster_values(api_cluster, api_cluster_2) self.validate_cluster_values(api_cluster, api_cluster_2)
def test_cluster_db_to_object_to_db(self): def test_cluster_db_to_object_to_db(self):

View File

@@ -16,7 +16,7 @@
from oslo.utils import timeutils from oslo.utils import timeutils
from cue.api.controllers import v1 from cue.api.controllers.v1 import cluster
from cue.db.sqlalchemy import models from cue.db.sqlalchemy import models
from cue import objects from cue import objects
@@ -49,17 +49,17 @@ def create_api_test_cluster(**kw):
:returns: Test Cluster API object. :returns: Test Cluster API object.
""" """
cluster = get_test_cluster(**kw) test_cluster = get_test_cluster(**kw)
cluster_parameters = { cluster_parameters = {
'name': cluster['name'], 'name': test_cluster['name'],
'network_id': cluster['network_id'], 'network_id': test_cluster['network_id'],
'flavor': cluster['flavor'], 'flavor': test_cluster['flavor'],
'size': str(cluster['size']), 'size': str(test_cluster['size']),
'volume_size': str(cluster['volume_size']), 'volume_size': str(test_cluster['volume_size']),
} }
new_cluster = v1.ClusterDetails(**cluster_parameters) new_cluster = cluster.ClusterDetails(**cluster_parameters)
return new_cluster return new_cluster
@@ -73,22 +73,22 @@ def create_api_test_cluster_all(**kw):
:returns: Test Cluster API object. :returns: Test Cluster API object.
""" """
cluster = get_test_cluster(**kw) test_cluster = get_test_cluster(**kw)
cluster_parameters = { cluster_parameters = {
'name': cluster['name'], 'name': test_cluster['name'],
'network_id': cluster['network_id'], 'network_id': test_cluster['network_id'],
'flavor': cluster['flavor'], 'flavor': test_cluster['flavor'],
'size': cluster['size'], 'size': test_cluster['size'],
'volume_size': cluster['volume_size'], 'volume_size': test_cluster['volume_size'],
'id': cluster['id'], 'id': test_cluster['id'],
'project_id': cluster['project_id'], 'project_id': test_cluster['project_id'],
'status': cluster['status'], 'status': test_cluster['status'],
'created_at': cluster['created_at'], 'created_at': test_cluster['created_at'],
'updated_at': cluster['updated_at'], 'updated_at': test_cluster['updated_at'],
} }
new_cluster = v1.ClusterDetails(**cluster_parameters) new_cluster = cluster.ClusterDetails(**cluster_parameters)
return new_cluster return new_cluster
@@ -103,14 +103,14 @@ def create_db_test_cluster_from_objects_api(context, **kw):
:returns: Test Cluster DB object. :returns: Test Cluster DB object.
""" """
cluster = get_test_cluster(**kw) test_cluster = get_test_cluster(**kw)
cluster_parameters = { cluster_parameters = {
'name': cluster['name'], 'name': test_cluster['name'],
'network_id': cluster['network_id'], 'network_id': test_cluster['network_id'],
'flavor': cluster['flavor'], 'flavor': test_cluster['flavor'],
'size': cluster['size'], 'size': test_cluster['size'],
'volume_size': cluster['volume_size'], 'volume_size': test_cluster['volume_size'],
} }
new_cluster = objects.Cluster(**cluster_parameters) new_cluster = objects.Cluster(**cluster_parameters)
@@ -141,21 +141,21 @@ def create_db_test_cluster_model_object(context, **kw):
:returns: Test Cluster DB model object. :returns: Test Cluster DB model object.
""" """
cluster = get_test_cluster(**kw) test_cluster = get_test_cluster(**kw)
cluster_parameters = { cluster_parameters = {
'name': cluster['name'], 'name': test_cluster['name'],
'network_id': cluster['network_id'], 'network_id': test_cluster['network_id'],
'flavor': cluster['flavor'], 'flavor': test_cluster['flavor'],
'size': cluster['size'], 'size': test_cluster['size'],
'volume_size': cluster['volume_size'], 'volume_size': test_cluster['volume_size'],
'id': cluster['id'], 'id': test_cluster['id'],
'project_id': cluster['project_id'], 'project_id': test_cluster['project_id'],
'status': cluster['status'], 'status': test_cluster['status'],
'deleted': cluster['deleted'], 'deleted': test_cluster['deleted'],
'created_at': cluster['created_at'], 'created_at': test_cluster['created_at'],
'updated_at': cluster['updated_at'], 'updated_at': test_cluster['updated_at'],
'deleted_at': cluster['deleted_at'], 'deleted_at': test_cluster['deleted_at'],
} }
new_cluster = models.Cluster() new_cluster = models.Cluster()