Merge "Allow pod resource management by "name"."

This commit is contained in:
Jenkins 2015-03-19 01:17:26 +00:00 committed by Gerrit Code Review
commit 112558dab4
6 changed files with 85 additions and 8 deletions

View File

@ -216,16 +216,17 @@ class PodsController(rest.RestController):
sort_key, sort_dir, expand,
resource_url)
@wsme_pecan.wsexpose(Pod, types.uuid)
def get_one(self, pod_uuid):
@wsme_pecan.wsexpose(Pod, types.uuid_or_name)
def get_one(self, pod_ident):
"""Retrieve information about the given pod.
:param pod_uuid: UUID of a pod.
:param pod_ident: UUID of a pod or logical name of the pod.
"""
if self.from_pods:
raise exception.OperationNotPermitted
rpc_pod = objects.Pod.get_by_uuid(pecan.request.context, pod_uuid)
rpc_pod = api_utils.get_rpc_resource('Pod', pod_ident)
return Pod.convert_with_links(rpc_pod)
@wsme_pecan.wsexpose(Pod, body=Pod, status_code=201)
@ -287,13 +288,15 @@ class PodsController(rest.RestController):
rpc_pod.save()
return Pod.convert_with_links(rpc_pod)
@wsme_pecan.wsexpose(None, types.uuid, status_code=204)
def delete(self, pod_uuid):
@wsme_pecan.wsexpose(None, types.uuid_or_name, status_code=204)
def delete(self, pod_ident):
"""Delete a pod.
:param pod_uuid: UUID of a pod.
:param pod_ident: UUID of a pod or logical name of the pod.
"""
if self.from_pods:
raise exception.OperationNotPermitted
pecan.request.rpcapi.pod_delete(pod_uuid)
rpc_pod = api_utils.get_rpc_resource('Pod', pod_ident)
pecan.request.rpcapi.pod_delete(rpc_pod.uuid)

View File

@ -604,6 +604,9 @@ class Connection(api.Connection):
query = model_query(models.Pod).filter_by(name=pod_name)
try:
return query.one()
except MultipleResultsFound:
raise exception.Conflict('Multiple pods exist with same name.'
' Please use the pod uuid instead.')
except NoResultFound:
raise exception.PodNotFound(pod=pod_name)

View File

@ -64,6 +64,28 @@ class TestListPod(api_base.FunctionalTest):
self.assertEqual(pod.uuid, response['uuid'])
self._assert_pod_fields(response)
def test_get_one_by_name(self):
pod = obj_utils.create_test_pod(self.context)
response = self.get_json('/pods/%s' % pod['name'])
self.assertEqual(pod.uuid, response['uuid'])
self._assert_pod_fields(response)
def test_get_one_by_name_not_found(self):
response = self.get_json('/pods/not_found', expect_errors=True)
self.assertEqual(response.status_int, 404)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_get_one_by_name_multiple_pod(self):
obj_utils.create_test_pod(self.context, name='test_pod',
uuid=utils.generate_uuid())
obj_utils.create_test_pod(self.context, name='test_pod',
uuid=utils.generate_uuid())
response = self.get_json('/pods/test_pod', expect_errors=True)
self.assertEqual(response.status_int, 409)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_detail(self):
pod = obj_utils.create_test_pod(self.context)
response = self.get_json('/pods/detail')
@ -395,6 +417,30 @@ class TestDelete(api_base.FunctionalTest):
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_delete_pod_by_name(self):
self.delete('/pods/%s' % self.pod.name)
response = self.get_json('/pods/%s' % self.pod.name,
expect_errors=True)
self.assertEqual(404, response.status_int)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_delete_pod_by_name_not_found(self):
response = self.delete('/pods/not_found', expect_errors=True)
self.assertEqual(404, response.status_int)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_delete_multiple_pod_by_name(self):
obj_utils.create_test_pod(self.context, name='test_pod',
uuid=utils.generate_uuid())
obj_utils.create_test_pod(self.context, name='test_pod',
uuid=utils.generate_uuid())
response = self.delete('/pods/test_pod', expect_errors=True)
self.assertEqual(409, response.status_int)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_delete_pod_not_found(self):
uuid = utils.generate_uuid()
response = self.delete('/pods/%s' % uuid, expect_errors=True)

View File

@ -116,6 +116,11 @@ class RPCAPITestCase(base.DbTestCase):
version='1.0',
uuid=self.fake_pod['uuid'])
self._test_rpcapi('pod_delete',
'call',
version='1.1',
uuid=self.fake_pod['name'])
def test_rc_create(self):
self._test_rpcapi('rc_create',
'call',

View File

@ -53,6 +53,17 @@ class DbPodTestCase(base.DbTestCase):
self.assertEqual(self.pod.id, res.id)
self.assertEqual(self.pod.uuid, res.uuid)
def test_get_pod_by_name_multiple_pods(self):
utils.create_test_pod(bay_uuid=self.bay.uuid,
uuid=magnum_utils.generate_uuid())
self.assertRaises(exception.Conflict, self.dbapi.get_pod_by_name,
self.pod.name)
def test_get_pod_by_name_not_found(self):
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_name,
'not_found')
def test_get_pod_that_does_not_exist(self):
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_id, self.context, 999)

View File

@ -46,6 +46,15 @@ class TestPodObject(base.DbTestCase):
mock_get_pod.assert_called_once_with(self.context, uuid)
self.assertEqual(self.context, pod._context)
def test_get_by_name(self):
name = self.fake_pod['name']
with mock.patch.object(self.dbapi, 'get_pod_by_name',
autospec=True) as mock_get_pod:
mock_get_pod.return_value = self.fake_pod
pod = objects.Pod.get_by_name(self.context, name)
mock_get_pod.assert_called_once_with(name)
self.assertEqual(self.context, pod._context)
def test_list(self):
with mock.patch.object(self.dbapi, 'get_pod_list',
autospec=True) as mock_get_list: