Merge "Enable name property update for os.nova.server profile"

This commit is contained in:
Jenkins 2015-12-23 03:23:59 +00:00 committed by Gerrit Code Review
commit 62b1847cea
2 changed files with 66 additions and 0 deletions

View File

@ -180,6 +180,7 @@ class ServerProfile(base.Profile):
),
NAME: schema.String(
_('Name of the server.'),
updatable=True,
),
NETWORKS: schema.List(
_('List of networks for the server.'),
@ -377,6 +378,18 @@ class ServerProfile(base.Profile):
# TODO(anyone): Validate the new profile
# TODO(Yanyan Hu): Update all server properties changed in new profile
# Update server name
name = self.properties[self.NAME]
new_name = new_profile.properties[self.NAME]
if new_name != name:
attrs = {'name': new_name if new_name else obj.name}
try:
self.nova(obj).server_update(self.server_id, **attrs)
except Exception as ex:
LOG.exception(_('Failed in updating server name: %s'),
six.text_type(ex))
return False
# Update server flavor
flavor = self.properties[self.FLAVOR]
new_flavor = new_profile.properties[self.FLAVOR]

View File

@ -508,6 +508,59 @@ class TestNovaServerProfile(base.SenlinTestCase):
self.assertFalse(res)
nc.server_delete.assert_called_once_with('FAKE_ID')
def test_do_update_name_to_given_string_succeeded(self):
obj = mock.Mock()
obj.physical_id = 'FAKE_ID'
novaclient = mock.Mock()
profile = server.ServerProfile('t', self.spec)
profile._novaclient = novaclient
new_spec = copy.deepcopy(self.spec)
new_spec['properties']['name'] = 'TEST_SERVER'
new_profile = server.ServerProfile('t', new_spec)
res = profile.do_update(obj, new_profile)
self.assertTrue(res)
novaclient.server_update.assert_called_once_with('FAKE_ID',
name='TEST_SERVER')
def test_do_update_name_to_none_succeeded(self):
obj = mock.Mock()
obj.physical_id = 'FAKE_ID'
obj.name = 'FAKE_OBJ_NAME'
novaclient = mock.Mock()
profile = server.ServerProfile('t', self.spec)
profile._novaclient = novaclient
new_spec = copy.deepcopy(self.spec)
del new_spec['properties']['name']
# name property is removed
new_profile = server.ServerProfile('t', new_spec)
res = profile.do_update(obj, new_profile)
self.assertTrue(res)
novaclient.server_update.assert_called_once_with('FAKE_ID',
name='FAKE_OBJ_NAME')
def test_do_update_name_failed(self):
ex = exception.InternalError(code=500,
message='Internal server error')
novaclient = mock.Mock()
novaclient.server_update.side_effect = ex
obj = mock.Mock()
obj.physical_id = 'FAKE_ID'
profile = server.ServerProfile('t', self.spec)
profile._novaclient = novaclient
new_spec = copy.deepcopy(self.spec)
new_spec['properties']['name'] = 'TEST_SERVER'
new_profile = server.ServerProfile('t', new_spec)
res = profile.do_update(obj, new_profile)
self.assertFalse(res)
novaclient.server_update.assert_called_once_with('FAKE_ID',
name='TEST_SERVER')
@mock.patch.object(server.ServerProfile, '_update_network')
def test_do_update_network_successful_no_definition_overlap(
self, mock_update_network):