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
This commit is contained in:
Madhuri Kumari 2015-04-21 11:02:27 +00:00
parent 77ce8b5b7c
commit 27c30e541b
2 changed files with 54 additions and 5 deletions

View File

@ -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))

View File

@ -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):