Fix and clean up BayModel and Bay api

There are several codes that were ported from Ironic are unused or
incorrect. This commit is for cleaning up these codes.

Change-Id: I0dd1cb84690c094c1775ac835136fb4a34e0fc6c
This commit is contained in:
Hongbin Lu 2015-01-22 22:37:19 +00:00
parent 18b098043a
commit 97e7fcdb7e
3 changed files with 31 additions and 98 deletions

View File

@ -34,7 +34,7 @@ class BayPatchType(types.JsonPatchType):
@staticmethod
def mandatory_attrs():
return ['/bay_uuid']
return ['/baymodel_id']
class Bay(base.APIBase):
@ -44,30 +44,23 @@ class Bay(base.APIBase):
between the internal object model and the API representation of a bay.
"""
_bay_uuid = None
_baymodel_id = None
def _get_bay_uuid(self):
return self._bay_uuid
def _get_baymodel_id(self):
return self._baymodel_id
def _set_bay_uuid(self, value):
if value and self._bay_uuid != value:
def _set_baymodel_id(self, value):
if value and self._baymodel_id != 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.
bay = objects.Bay.get(pecan.request.context, value)
self._bay_uuid = bay.uuid
# NOTE(lucasagomes): Create the bay_id attribute on-the-fly
# to satisfy the api -> rpc object
# conversion.
self.bay_id = bay.id
except exception.BayNotFound as e:
baymodel = objects.BayModel.get(pecan.request.context, value)
self._baymodel_id = baymodel.uuid
except exception.BayModelNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for a POST request to create a Bay
e.code = 400 # BadRequest
raise e
elif value == wtypes.Unset:
self._bay_uuid = wtypes.Unset
self._baymodel_id = wtypes.Unset
uuid = types.uuid
"""Unique UUID for this bay"""
@ -75,7 +68,8 @@ class Bay(base.APIBase):
name = wtypes.text
"""Name of this bay"""
baymodel_id = wtypes.text
baymodel_id = wsme.wsproperty(wtypes.text, _get_baymodel_id,
_set_baymodel_id, mandatory=True)
"""The bay model UUID or id"""
node_count = wtypes.IntegerType()
@ -88,33 +82,19 @@ class Bay(base.APIBase):
super(Bay, self).__init__()
self.fields = []
fields = list(objects.Bay.fields)
# NOTE(lucasagomes): bay_uuid is not part of objects.Bay.fields
# because it's an API-only attribute
fields.append('bay_uuid')
for field in fields:
for field in objects.Bay.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): bay_id is an attribute created on-the-fly
# by _set_bay_uuid(), it needs to be present in the fields so
# that as_dict() will contain bay_id field when converting it
# before saving it in the database.
self.fields.append('bay_id')
setattr(self, 'bay_uuid', kwargs.get('bay_id', wtypes.Unset))
@staticmethod
def _convert_with_links(bay, url, expand=True):
if not expand:
bay.unset_fields_except(['uuid', 'name', 'baymodel_id',
'node_count'])
# never expose the bay_id attribute
bay.bay_id = wtypes.Unset
bay.links = [link.Link.make_link('self', url,
'bays', bay.uuid),
link.Link.make_link('bookmark', url,
@ -132,13 +112,10 @@ class Bay(base.APIBase):
def sample(cls, expand=True):
sample = cls(uuid='27e3153e-d5bf-4b7e-b517-fb518e17f34c',
name='example',
image_id='Fedora-k8s',
baymodel_id='4a96ac4b-2447-43f1-8ca6-9fd6f36d146d',
node_count=1,
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
# NOTE(lucasagomes): bay_uuid getter() method look at the
# _bay_uuid variable
sample._bay_uuid = '7ae81bb3-dec3-4289-8d6c-da80bd8001ae'
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
@ -280,11 +257,6 @@ class BaysController(rest.RestController):
rpc_bay = objects.Bay.get_by_uuid(pecan.request.context, bay_uuid)
try:
bay_dict = rpc_bay.as_dict()
# NOTE(lucasagomes):
# 1) Remove bay_id because it's an internal value and
# not present in the API object
# 2) Add bay_uuid
bay_dict['bay_uuid'] = bay_dict.pop('bay_id', None)
bay = Bay(**api_utils.apply_jsonpatch(bay_dict, patch))
except api_utils.JSONPATCH_EXCEPTIONS as e:
raise exception.PatchError(patch=patch, reason=e)

View File

@ -30,10 +30,7 @@ from magnum import objects
class BayModelPatchType(types.JsonPatchType):
@staticmethod
def mandatory_attrs():
return ['/baymodel_uuid']
pass
class BayModel(base.APIBase):
@ -43,28 +40,6 @@ class BayModel(base.APIBase):
between the internal object model and the API representation of a baymodel.
"""
_baymodel_uuid = None
def _get_baymodel_uuid(self):
return self._baymodel_uuid
def _set_baymodel_uuid(self, value):
if value and self._baymodel_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.
baymodel = objects.BayModel.get(pecan.request.context, value)
self._baymodel_uuid = baymodel.uuid
self.baymodel_id = baymodel.id
except exception.BayModelNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for a POST request to create a BayModel
e.code = 400 # BadRequest
raise e
elif value == wtypes.Unset:
self._baymodel_uuid = wtypes.Unset
uuid = types.uuid
"""Unique UUID for this baymodel"""
@ -94,31 +69,19 @@ class BayModel(base.APIBase):
def __init__(self, **kwargs):
self.fields = []
fields = list(objects.BayModel.fields)
fields.append('baymodel_uuid')
for field in fields:
for field in objects.BayModel.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): baymodel_id is an attribute created on-the-fly
# by _set_baymodel_uuid(), it needs to be present in the fields so
# that as_dict() will contain baymodel_id field when converting it
# before saving it in the database.
self.fields.append('baymodel_id')
setattr(self, 'baymodel_uuid', kwargs.get('baymodel_id', wtypes.Unset))
@staticmethod
def _convert_with_links(baymodel, url, expand=True):
if not expand:
baymodel.unset_fields_except(['uuid', 'name', 'image_id',
'apiserver_port'])
# never expose the baymodel_id attribute
baymodel.baymodel_id = wtypes.Unset
baymodel.links = [link.Link.make_link('self', url,
'baymodels', baymodel.uuid),
link.Link.make_link('bookmark', url,
@ -136,16 +99,15 @@ class BayModel(base.APIBase):
@classmethod
def sample(cls, expand=True):
sample = cls(uuid='27e3153e-d5bf-4b7e-b517-fb518e17f34c',
name='example',
type='virt',
image_id='Fedora-k8s',
apiserver_port=8080,
baymodel_count=1,
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
# NOTE(lucasagomes): baymodel_uuid getter() method look at the
# _baymodel_uuid variable
sample._baymodel_uuid = '7ae81bb3-dec3-4289-8d6c-da80bd8001ae'
name='example',
image_id='Fedora-k8s',
flavor_id='m1.small',
dns_nameserver='8.8.1.1',
keypair_id='keypair1',
external_network_id='ffc44e4a-2319-4062-bce0-9ae1c38b05ba',
apiserver_port=8080,
created_at=datetime.datetime.utcnow(),
updated_at=datetime.datetime.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
@ -290,12 +252,6 @@ class BayModelsController(rest.RestController):
baymodel_uuid)
try:
baymodel_dict = rpc_baymodel.as_dict()
# NOTE(lucasagomes):
# 1) Remove baymodel_id because it's an internal value and
# not present in the API object
# 2) Add baymodel_uuid
baymodel_dict['baymodel_uuid'] = baymodel_dict.pop('baymodel_id',
None)
baymodel = BayModel(**api_utils.apply_jsonpatch(baymodel_dict,
patch))
except api_utils.JSONPATCH_EXCEPTIONS as e:

View File

@ -12,6 +12,7 @@
from magnum.conductor import api
from magnum import objects
from magnum.tests.db import base as db_base
from magnum.tests.db import utils as db_utils
from mock import patch
@ -27,10 +28,14 @@ class TestBayController(db_base.DbTestCase):
def test_bay_api(self):
with patch.object(api.API, 'bay_create') as mock_method:
# Create a baymodel
baymodel = db_utils.get_test_baymodel()
self.dbapi.create_baymodel(baymodel)
# Create a bay
mock_method.side_effect = self.simulate_rpc_bay_create
params = '{"name": "bay_example_A", "baymodel_id": "12345", \
"node_count": "3"}'
"node_count": "3", "baymodel_id": "%s"}' % baymodel['uuid']
response = self.app.post('/v1/bays',
params=params,
content_type='application/json')