Remove '_apply_instance_name_template'

Support for the 'multi_instance_display_name_template' option was
removed in commit 0e43002c9 and booting multiple instances now will
simply result in a simple numerical suffix (-1, -2, -3, ...) being added
to the instance's 'display_name' and 'hostname' attributes.

In that commit, it was noted that there was a lot more cleanup that
could be done in this area. The change gets that ball rolling by
squashing the '_apply_instance_name_template' function into
'_populate_instance_names'. This doesn't do much itself, but it allows
us to simplify a future change to remove much of the now-unnecessary
complexity of this feature.

Change-Id: I57ddfca39d9b76ac0cabf994d8a825a35b6d6e19
This commit is contained in:
Stephen Finucane 2018-05-09 14:07:57 +01:00
parent c1e0d2bf20
commit 6f4a4d5d89
2 changed files with 22 additions and 21 deletions

View File

@ -513,17 +513,6 @@ class API(base.Base):
'auto_disk_config': auto_disk_config
}
def _apply_instance_name_template(self, instance, index):
original_name = instance.display_name
new_name = '%s-%d' % (original_name, index + 1)
instance.display_name = new_name
if not instance.get('hostname', None):
if utils.sanitize_hostname(original_name) == "":
instance.hostname = self._default_host_name(instance.uuid)
else:
instance.hostname = utils.sanitize_hostname(new_name)
return instance
def _check_config_drive(self, config_drive):
if config_drive:
try:
@ -1409,7 +1398,7 @@ class API(base.Base):
self.volume_api.check_availability_zone(context, volume,
instance=instance)
def _populate_instance_names(self, instance, num_instances):
def _populate_instance_names(self, instance, num_instances, index):
"""Populate instance display_name and hostname."""
display_name = instance.get('display_name')
if instance.obj_attr_is_set('hostname'):
@ -1436,6 +1425,16 @@ class API(base.Base):
instance.hostname = utils.sanitize_hostname(hostname,
default_hostname)
if num_instances > 1 and self.cell_type != 'api':
original_name = instance.display_name
new_name = '%s-%d' % (original_name, index + 1)
instance.display_name = new_name
if not instance.get('hostname', None):
if utils.sanitize_hostname(original_name) == "":
instance.hostname = self._default_host_name(instance.uuid)
else:
instance.hostname = utils.sanitize_hostname(new_name)
def _default_display_name(self, instance_uuid):
return "Server %s" % instance_uuid
@ -1497,10 +1496,8 @@ class API(base.Base):
else:
instance.security_groups = security_groups
self._populate_instance_names(instance, num_instances)
self._populate_instance_names(instance, num_instances, index)
instance.shutdown_terminate = shutdown_terminate
if num_instances > 1 and self.cell_type != 'api':
instance = self._apply_instance_name_template(instance, index)
return instance

View File

@ -4979,28 +4979,32 @@ class _ComputeAPIUnitTestMixIn(object):
def test_populate_instance_names_host_name(self):
params = dict(display_name="vm1")
instance = self._create_instance_obj(params=params)
self.compute_api._populate_instance_names(instance, 1)
self.compute_api._populate_instance_names(instance, 1, 0)
self.assertEqual('vm1', instance.hostname)
def test_populate_instance_names_host_name_is_empty(self):
params = dict(display_name=u'\u865a\u62df\u673a\u662f\u4e2d\u6587')
instance = self._create_instance_obj(params=params)
self.compute_api._populate_instance_names(instance, 1)
self.compute_api._populate_instance_names(instance, 1, 0)
self.assertEqual('Server-%s' % instance.uuid, instance.hostname)
def test_populate_instance_names_host_name_multi(self):
params = dict(display_name="vm")
instance = self._create_instance_obj(params=params)
with mock.patch.object(instance, 'save'):
self.compute_api._apply_instance_name_template(instance, 1)
self.compute_api._populate_instance_names(instance, 2, 1)
if self.cell_type != 'api':
self.assertEqual('vm-2', instance.hostname)
else:
self.assertNotIn('hostname', instance)
def test_populate_instance_names_host_name_is_empty_multi(self):
params = dict(display_name=u'\u865a\u62df\u673a\u662f\u4e2d\u6587')
instance = self._create_instance_obj(params=params)
with mock.patch.object(instance, 'save'):
self.compute_api._apply_instance_name_template(instance, 1)
self.compute_api._populate_instance_names(instance, 2, 1)
if self.cell_type != 'api':
self.assertEqual('Server-%s' % instance.uuid, instance.hostname)
else:
self.assertNotIn('hostname', instance)
def test_host_statuses(self):
instances = [