Merge "Remove '_apply_instance_name_template'"
This commit is contained in:
+12
-15
@@ -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
|
||||
|
||||
|
||||
@@ -4978,28 +4978,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 = [
|
||||
|
||||
Reference in New Issue
Block a user