From 27c30e541b0d934898390d116146af371442705c Mon Sep 17 00:00:00 2001 From: Madhuri Kumari Date: Tue, 21 Apr 2015 11:02:27 +0000 Subject: [PATCH] Allow service-update with service name also. This patch lets service-update API to be called with name also. Change-Id: I276350fd2bda14fbca2321da7de2b436be93f308 Partially-Implements: blueprint name-based-resource-management --- magnum/api/controllers/v1/service.py | 9 ++-- .../unit/api/controllers/v1/test_service.py | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/magnum/api/controllers/v1/service.py b/magnum/api/controllers/v1/service.py index af036fecd7..e2263ef049 100644 --- a/magnum/api/controllers/v1/service.py +++ b/magnum/api/controllers/v1/service.py @@ -270,18 +270,17 @@ class ServicesController(rest.RestController): return Service.convert_with_links(new_service) @wsme.validate(types.uuid, [ServicePatchType]) - @wsme_pecan.wsexpose(Service, types.uuid, body=[ServicePatchType]) - def patch(self, service_uuid, patch): + @wsme_pecan.wsexpose(Service, types.uuid_or_name, body=[ServicePatchType]) + def patch(self, service_ident, patch): """Update an existing service. - :param service_uuid: UUID of a service. + :param service_ident: UUID or logical name of a service. :param patch: a json PATCH document to apply to this service. """ if self.from_services: raise exception.OperationNotPermitted - rpc_service = objects.Service.get_by_uuid(pecan.request.context, - service_uuid) + rpc_service = api_utils.get_rpc_resource('Service', service_ident) try: service_dict = rpc_service.as_dict() service = Service(**api_utils.apply_jsonpatch(service_dict, patch)) diff --git a/magnum/tests/unit/api/controllers/v1/test_service.py b/magnum/tests/unit/api/controllers/v1/test_service.py index 9cf30c00ad..143e35c3d4 100644 --- a/magnum/tests/unit/api/controllers/v1/test_service.py +++ b/magnum/tests/unit/api/controllers/v1/test_service.py @@ -233,6 +233,56 @@ class TestPatch(api_base.FunctionalTest): self.assertEqual('application/json', response.content_type) self.assertTrue(response.json['error_message']) + @mock.patch('oslo_utils.timeutils.utcnow') + def test_replace_ok_by_name(self, mock_utcnow): + test_time = datetime.datetime(2000, 1, 1, 0, 0) + mock_utcnow.return_value = test_time + + response = self.patch_json('/services/%s' % self.service.name, + [{'path': '/bay_uuid', + 'value': self.bay2.uuid, + 'op': 'replace'}]) + self.assertEqual('application/json', response.content_type) + self.assertEqual(200, response.status_code) + + response = self.get_json('/services/%s' % self.service.uuid) + self.assertEqual('service1', response['name']) + return_updated_at = timeutils.parse_isotime( + response['updated_at']).replace(tzinfo=None) + self.assertEqual(test_time, return_updated_at) + + @mock.patch('oslo_utils.timeutils.utcnow') + def test_replace_ok_by_name_not_found(self, mock_utcnow): + name = 'not_found' + test_time = datetime.datetime(2000, 1, 1, 0, 0) + mock_utcnow.return_value = test_time + + response = self.patch_json('/services/%s' % name, + [{'path': '/bay_uuid', + 'value': self.bay2.uuid, + 'op': 'replace'}], + expect_errors=True) + self.assertEqual('application/json', response.content_type) + self.assertEqual(404, response.status_code) + + @mock.patch('oslo_utils.timeutils.utcnow') + def test_replace_ok_by_name_multiple_service(self, mock_utcnow): + test_time = datetime.datetime(2000, 1, 1, 0, 0) + mock_utcnow.return_value = test_time + + obj_utils.create_test_service(self.context, name='test_service', + uuid=utils.generate_uuid()) + obj_utils.create_test_service(self.context, name='test_service', + uuid=utils.generate_uuid()) + + response = self.patch_json('/services/test_service', + [{'path': '/bay_uuid', + 'value': self.bay2.uuid, + 'op': 'replace'}], + expect_errors=True) + self.assertEqual('application/json', response.content_type) + self.assertEqual(409, response.status_code) + class TestPost(api_base.FunctionalTest):