diff --git a/magnum/api/controllers/v1/pod.py b/magnum/api/controllers/v1/pod.py index 1e3c50c4c1..59e88032ec 100644 --- a/magnum/api/controllers/v1/pod.py +++ b/magnum/api/controllers/v1/pod.py @@ -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) diff --git a/magnum/db/sqlalchemy/api.py b/magnum/db/sqlalchemy/api.py index 47091cccc0..1f086a0114 100644 --- a/magnum/db/sqlalchemy/api.py +++ b/magnum/db/sqlalchemy/api.py @@ -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) diff --git a/magnum/tests/api/controllers/v1/test_pod.py b/magnum/tests/api/controllers/v1/test_pod.py index e6e9927056..ff4a3909b9 100644 --- a/magnum/tests/api/controllers/v1/test_pod.py +++ b/magnum/tests/api/controllers/v1/test_pod.py @@ -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) diff --git a/magnum/tests/conductor/test_rpcapi.py b/magnum/tests/conductor/test_rpcapi.py index eb9076f839..30ffb688a5 100644 --- a/magnum/tests/conductor/test_rpcapi.py +++ b/magnum/tests/conductor/test_rpcapi.py @@ -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', diff --git a/magnum/tests/db/test_pod.py b/magnum/tests/db/test_pod.py index eb8b384012..6e9633b9b1 100644 --- a/magnum/tests/db/test_pod.py +++ b/magnum/tests/db/test_pod.py @@ -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) diff --git a/magnum/tests/objects/test_pod.py b/magnum/tests/objects/test_pod.py index 8b8395d507..b9081d514d 100644 --- a/magnum/tests/objects/test_pod.py +++ b/magnum/tests/objects/test_pod.py @@ -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: