Get the Pod REST API and tests working

Change-Id: Ib765d1c3ed8a6c06136233abd393fb1efae71e41
This commit is contained in:
Davanum Srinivas
2014-12-04 11:46:55 -05:00
parent dbb697208e
commit 7cb8e559a3
6 changed files with 347 additions and 106 deletions

View File

@ -29,6 +29,7 @@ import wsmeext.pecan as wsme_pecan
from magnum.api.controllers import link
from magnum.api.controllers.v1 import bay
from magnum.api.controllers.v1 import container
from magnum.api.controllers.v1 import pod
class APIBase(wtypes.Base):
@ -145,7 +146,7 @@ class Controller(rest.RestController):
bays = bay.BaysController()
containers = container.ContainersController()
# pods = pod.PodsController()
pods = pod.PodsController()
# services = service.ServicesController()
@wsme_pecan.wsexpose(V1)

View File

@ -1,108 +1,317 @@
# 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
# 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.
# 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.
import uuid
import datetime
import pecan
from pecan import rest
import wsme
from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan
from magnum.api.controllers.v1.base import Base
from magnum.api.controllers.v1.base import Query
# NOTE(dims): We don't depend on oslo*i18n yet
_ = _LI = _LW = _LE = _LC = lambda x: x
from magnum.api.controllers import base
from magnum.api.controllers import link
from magnum.api.controllers.v1 import collection
from magnum.api.controllers.v1 import types
from magnum.api.controllers.v1 import utils as api_utils
from magnum.common import exception
from magnum import objects
class Pod(Base):
id = wtypes.text
""" The ID of the pods."""
class PodPatchType(types.JsonPatchType):
name = wsme.wsattr(wtypes.text, mandatory=True)
""" The name of the pod."""
@staticmethod
def mandatory_attrs():
return ['/pod_uuid']
desc = wsme.wsattr(wtypes.text, mandatory=True)
class Pod(base.APIBase):
"""API representation of a pod.
This class enforces type checking and value constraints, and converts
between the internal object model and the API representation of a pod.
"""
_pod_uuid = None
def _get_pod_uuid(self):
return self._pod_uuid
def _set_pod_uuid(self, value):
if value and self._pod_uuid != value:
try:
# FIXME(comstud): One should only allow UUID here, but
# there seems to be a bug in that tests are passing an
# ID. See bug #1301046 for more details.
pod = objects.Pod.get(pecan.request.context, value)
self._pod_uuid = pod.uuid
# NOTE(lucasagomes): Create the pod_id attribute on-the-fly
# to satisfy the api -> rpc object
# conversion.
self.pod_id = pod.id
except exception.PodNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for a POST request to create a Pod
e.code = 400 # BadRequest
raise e
elif value == wtypes.Unset:
self._pod_uuid = wtypes.Unset
uuid = types.uuid
"""Unique UUID for this pod"""
name = wtypes.text
"""Name of this pod"""
desc = wtypes.text
"""Description of this pod"""
links = wsme.wsattr([link.Link], readonly=True)
"""A list containing a self link and associated pod links"""
def __init__(self, **kwargs):
super(Pod, self).__init__(**kwargs)
self.fields = []
fields = list(objects.Pod.fields)
# NOTE(lucasagomes): pod_uuid is not part of objects.Pod.fields
# because it's an API-only attribute
fields.append('pod_uuid')
for field in fields:
# Skip fields we do not expose.
if not hasattr(self, field):
continue
self.fields.append(field)
setattr(self, field, kwargs.get(field, wtypes.Unset))
# NOTE(lucasagomes): pod_id is an attribute created on-the-fly
# by _set_pod_uuid(), it needs to be present in the fields so
# that as_dict() will contain pod_id field when converting it
# before saving it in the database.
self.fields.append('pod_id')
setattr(self, 'pod_uuid', kwargs.get('pod_id', wtypes.Unset))
@staticmethod
def _convert_with_links(pod, url, expand=True):
if not expand:
pod.unset_fields_except(['uuid', 'name', 'desc'])
# never expose the pod_id attribute
pod.pod_id = wtypes.Unset
pod.links = [link.Link.make_link('self', url,
'pods', pod.uuid),
link.Link.make_link('bookmark', url,
'pods', pod.uuid,
bookmark=True)
]
return pod
@classmethod
def convert_with_links(cls, rpc_pod, expand=True):
pod = Pod(**rpc_pod.as_dict())
return cls._convert_with_links(pod, pecan.request.host_url, expand)
@classmethod
def sample(cls, expand=True):
sample = cls(uuid='f978db47-9a37-4e9f-8572-804a10abc0aa',
name='MyPod',
desc='Pod - Description',
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
# NOTE(lucasagomes): pod_uuid getter() method look at the
# _pod_uuid variable
sample._pod_uuid = '87504bd9-ca50-40fd-b14e-bcb23ed42b27'
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
class PodCollection(collection.Collection):
"""API representation of a collection of pods."""
pods = [Pod]
"""A list containing pods objects"""
def __init__(self, **kwargs):
self._type = 'pods'
@staticmethod
def convert_with_links(rpc_pods, limit, url=None, expand=False, **kwargs):
collection = PodCollection()
collection.pods = [Pod.convert_with_links(p, expand)
for p in rpc_pods]
collection.next = collection.get_next(limit, url=url, **kwargs)
return collection
@classmethod
def sample(cls):
return cls(id=str(uuid.uuid1()),
name="Docker",
desc='Docker Pods')
sample = cls()
sample.pods = [Pod.sample(expand=False)]
return sample
class PodController(rest.RestController):
"""Manages Pods."""
def __init__(self, **kwargs):
super(PodController, self).__init__(**kwargs)
class PodsController(rest.RestController):
"""REST controller for Pods."""
self.pod_list = []
from_pods = False
"""A flag to indicate if the requests to this controller are coming
from the top-level resource Pods."""
@wsme_pecan.wsexpose(Pod, wtypes.text)
def get_one(self, id):
"""Retrieve details about one pod.
_custom_actions = {
'detail': ['GET'],
}
:param id: An ID of the pod.
def _get_pods_collection(self, marker, limit,
sort_key, sort_dir, expand=False,
resource_url=None):
limit = api_utils.validate_limit(limit)
sort_dir = api_utils.validate_sort_dir(sort_dir)
marker_obj = None
if marker:
marker_obj = objects.Pod.get_by_uuid(pecan.request.context,
marker)
pods = objects.Pod.list(pecan.request.context, limit,
marker_obj, sort_key=sort_key,
sort_dir=sort_dir)
return PodCollection.convert_with_links(pods, limit,
url=resource_url,
expand=expand,
sort_key=sort_key,
sort_dir=sort_dir)
@wsme_pecan.wsexpose(PodCollection, types.uuid,
types.uuid, int, wtypes.text, wtypes.text)
def get_all(self, pod_uuid=None, marker=None, limit=None,
sort_key='id', sort_dir='asc'):
"""Retrieve a list of pods.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
"""
for pod in self.pod_list:
if pod.id == id:
return pod
return None
return self._get_pods_collection(marker, limit, sort_key,
sort_dir)
@wsme_pecan.wsexpose([Pod], [Query], int)
def get_all(self, q=None, limit=None):
"""Retrieve definitions of all of the pods.
@wsme_pecan.wsexpose(PodCollection, types.uuid,
types.uuid, int, wtypes.text, wtypes.text)
def detail(self, pod_uuid=None, marker=None, limit=None,
sort_key='id', sort_dir='asc'):
"""Retrieve a list of pods with detail.
:param query: query parameters.
:param limit: The number of pods to retrieve.
:param pod_uuid: UUID of a pod, to get only pods for that pod.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
"""
if len(self.pod_list) == 0:
return []
return self.pod_list
# NOTE(lucasagomes): /detail should only work agaist collections
parent = pecan.request.path.split('/')[:-1][-1]
if parent != "pods":
raise exception.HTTPNotFound
@wsme_pecan.wsexpose(Pod, wtypes.text, wtypes.text)
def post(self, name, desc):
expand = True
resource_url = '/'.join(['pods', 'detail'])
return self._get_pods_collection(marker, limit,
sort_key, sort_dir, expand,
resource_url)
@wsme_pecan.wsexpose(Pod, types.uuid)
def get_one(self, pod_uuid):
"""Retrieve information about the given pod.
:param pod_uuid: UUID of a pod.
"""
if self.from_pods:
raise exception.OperationNotPermitted
rpc_pod = objects.Pod.get_by_uuid(pecan.request.context, pod_uuid)
return Pod.convert_with_links(rpc_pod)
@wsme_pecan.wsexpose(Pod, body=Pod, status_code=201)
def post(self, pod):
"""Create a new pod.
:param pod: a pod within the request body.
"""
pod = Pod(id=str(uuid.uuid1()), name=name, desc=desc)
self.pod_list.append(pod)
if self.from_pods:
raise exception.OperationNotPermitted
return pod
new_pod = objects.Pod(pecan.request.context,
**pod.as_dict())
new_pod.create()
# Set the HTTP Location Header
pecan.response.location = link.build_url('pods', new_pod.uuid)
return Pod.convert_with_links(new_pod)
@wsme_pecan.wsexpose(Pod, wtypes.text, body=Pod)
def put(self, id, pod):
"""Modify this pod.
@wsme.validate(types.uuid, [PodPatchType])
@wsme_pecan.wsexpose(Pod, types.uuid, body=[PodPatchType])
def patch(self, pod_uuid, patch):
"""Update an existing pod.
:param id: An ID of the pod.
:param pod: a pod within the request body.
:param pod_uuid: UUID of a pod.
:param patch: a json PATCH document to apply to this pod.
"""
pass
if self.from_pods:
raise exception.OperationNotPermitted
@wsme_pecan.wsexpose(wtypes.text, wtypes.text)
def delete(self, id):
"""Delete this pod.
rpc_pod = objects.Pod.get_by_uuid(pecan.request.context, pod_uuid)
try:
pod_dict = rpc_pod.as_dict()
# NOTE(lucasagomes):
# 1) Remove pod_id because it's an internal value and
# not present in the API object
# 2) Add pod_uuid
pod_dict['pod_uuid'] = pod_dict.pop('pod_id', None)
pod = Pod(**api_utils.apply_jsonpatch(pod_dict, patch))
except api_utils.JSONPATCH_EXCEPTIONS as e:
raise exception.PatchError(patch=patch, reason=e)
:param id: An ID of the pod.
# Update only the fields that have changed
for field in objects.Pod.fields:
try:
patch_val = getattr(pod, field)
except AttributeError:
# Ignore fields that aren't exposed in the API
continue
if patch_val == wtypes.Unset:
patch_val = None
if rpc_pod[field] != patch_val:
rpc_pod[field] = patch_val
if hasattr(pecan.request, 'rpcapi'):
rpc_pod = objects.Pod.get_by_id(pecan.request.context,
rpc_pod.pod_id)
topic = pecan.request.rpcapi.get_topic_for(rpc_pod)
new_pod = pecan.request.rpcapi.update_pod(
pecan.request.context, rpc_pod, topic)
return Pod.convert_with_links(new_pod)
else:
rpc_pod.save()
return Pod.convert_with_links(rpc_pod)
@wsme_pecan.wsexpose(None, types.uuid, status_code=204)
def delete(self, pod_uuid):
"""Delete a pod.
:param pod_uuid: UUID of a pod.
"""
count = 0
for pod in self.pod_list:
if pod.id == id:
self.pod_list.remove(pod)
return id
count = count + 1
if self.from_pods:
raise exception.OperationNotPermitted
return None
rpc_pod = objects.Pod.get_by_uuid(pecan.request.context,
pod_uuid)
rpc_pod.destroy()

View File

@ -466,6 +466,33 @@ class Connection(api.Connection):
ref.update(values)
return ref
def _add_pods_filters(self, query, filters):
if filters is None:
filters = []
if 'associated' in filters:
if filters['associated']:
query = query.filter(models.Pod.instance_uuid is not None)
else:
query = query.filter(models.Pod.instance_uuid is None)
if 'reserved' in filters:
if filters['reserved']:
query = query.filter(models.Pod.reservation is not None)
else:
query = query.filter(models.Pod.reservation is None)
if 'maintenance' in filters:
query = query.filter_by(maintenance=filters['maintenance'])
if 'driver' in filters:
query = query.filter_by(driver=filters['driver'])
if 'provision_state' in filters:
query = query.filter_by(provision_state=filters['provision_state'])
if 'provisioned_before' in filters:
limit = timeutils.utcnow() - datetime.timedelta(
seconds=filters['provisioned_before'])
query = query.filter(models.Pod.provision_updated_at < limit)
return query
def get_podinfo_list(self, columns=None, filters=None, limit=None,
marker=None, sort_key=None, sort_dir=None):
# list-ify columns default values because it is bad form

View File

@ -147,6 +147,8 @@ class Pod(Base):
)
id = Column(Integer, primary_key=True)
uuid = Column(String(36))
name = Column(String(255))
desc = Column(String(255))
class AbrviceObject(Base):

View File

@ -34,6 +34,8 @@ class Pod(base.MagnumObject):
fields = {
'id': int,
'uuid': obj_utils.str_or_none,
'name': obj_utils.str_or_none,
'desc': obj_utils.str_or_none
}
@staticmethod

View File

@ -108,44 +108,44 @@ class TestBayController(db_base.DbTestCase):
self.assertEqual(0, len(c))
# class TestPodController(tests.FunctionalTest):
# def test_pod_api(self):
# # Create a pod
# params = '{"desc": "my pod", "name": "pod_example_A"}'
# response = self.app.post('/v1/pods',
# params=params,
# content_type='application/json')
# self.assertEqual(response.status_int, 200)
#
# # Get all bays
# response = self.app.get('/v1/pods')
# self.assertEqual(response.status_int, 200)
# self.assertEqual(1, len(response.json))
# c = response.json[0]
# self.assertIsNotNone(c.get('uuid'))
# self.assertEqual('pod_example_A', c.get('name'))
# self.assertEqual('my pod', c.get('desc'))
#
# # Get just the one we created
# response = self.app.get('/v1/pods/%s' % c.get('uuid'))
# self.assertEqual(response.status_int, 200)
#
# # Update the description
# params = ('{"uuid":"' + c.get('uuid') + '", '
# '"desc": "your pod", '
# '"name": "pod_example_A"}')
# response = self.app.put('/v1/pods',
# params=params,
# content_type='application/json')
# self.assertEqual(response.status_int, 200)
#
# # Delete the bay we created
# response = self.app.delete('/v1/pods/%s' % c.get('uuid'))
# self.assertEqual(response.status_int, 200)
#
# response = self.app.get('/v1/pods')
# self.assertEqual(response.status_int, 200)
# self.assertEqual(0, len(response.json))
class TestPodController(db_base.DbTestCase):
def test_pod_api(self):
# Create a pod
params = '{"name": "pod_example_A", "desc": "My Pod"}'
response = self.app.post('/v1/pods',
params=params,
content_type='application/json')
self.assertEqual(response.status_int, 201)
# Get all pods
response = self.app.get('/v1/pods')
self.assertEqual(response.status_int, 200)
self.assertEqual(1, len(response.json))
c = response.json['pods'][0]
self.assertIsNotNone(c.get('uuid'))
self.assertEqual('pod_example_A', c.get('name'))
self.assertEqual('My Pod', c.get('desc'))
# Get just the one we created
response = self.app.get('/v1/pods/%s' % c.get('uuid'))
self.assertEqual(response.status_int, 200)
# Update the description
params = [{'path': '/name',
'value': 'pod_example_B',
'op': 'replace'}]
response = self.app.patch_json('/v1/pods/%s' % c.get('uuid'),
params=params)
self.assertEqual(response.status_int, 200)
# Delete the pod we created
response = self.app.delete('/v1/pods/%s' % c.get('uuid'))
self.assertEqual(response.status_int, 204)
response = self.app.get('/v1/pods')
self.assertEqual(response.status_int, 200)
c = response.json['pods']
self.assertEqual(0, len(c))
# class TestContainerController(tests.FunctionalTest):