1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Include pk in inventory source name to keep it unique even with group renames. Fixes https://trello.com/c/PnmDWCai

This commit is contained in:
Chris Church 2015-05-20 14:00:33 -04:00
parent a564f21e7c
commit 7f1c97b167
3 changed files with 23 additions and 6 deletions

View File

@ -882,9 +882,8 @@ class Command(NoArgsCommand):
continue
mem_group = self.all_group.all_groups[group_name]
group = self.inventory.groups.create(name=group_name, variables=json.dumps(mem_group.variables), description='imported')
# Access auto one-to-one attribute to create related object.
#group.inventory_source
InventorySource.objects.create(group=group, inventory=self.inventory, name=('%s (%s)' % (group_name, self.inventory.name)))
# Create related inventory source (name will be set by save() method on InventorySource).
InventorySource.objects.create(group=group, inventory=self.inventory)
self.logger.info('Group "%s" added', group.name)
if inv_src_group and group_name in root_group_names:
self._batch_add_m2m(inv_src_group.children, group)

View File

@ -1110,12 +1110,14 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions):
self.inventory = self.group.inventory
if 'inventory' not in update_fields:
update_fields.append('inventory')
# Set name automatically.
# Set name automatically. Include PK (or placeholder) to make sure the names are always unique.
replace_text = '__replace_%s__' % now()
old_name_re = re.compile(r'^inventory_source \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*?$')
if not self.name or old_name_re.match(self.name):
if self.inventory and self.group:
self.name = '%s (%s)' % (self.group.name, self.inventory.name)
if self.inventory and self.group and self.pk:
self.name = '%s (%s - %s)' % (self.group.name, self.inventory.name, self.pk)
elif self.inventory and self.group:
self.name = '%s (%s - %s)' % (self.group.name, self.inventory.name, replace_text)
elif self.inventory and self.pk:
self.name = '%s (%s)' % (self.inventory.name, self.pk)
elif self.inventory:

View File

@ -1762,6 +1762,22 @@ class InventoryUpdatesTest(BaseTransactionTest):
self.assertTrue(self.group.children.get(name='images').children.filter(active=True).count())
self.assertTrue('instances' in child_names)
self.assertTrue(self.group.children.get(name='instances').children.filter(active=True).count())
# Sync again with overwrite set to False after renaming a group that
# was created by the sync. With overwrite false, the renamed group and
# the original group (created again by the sync) will both exist.
region_group = self.group.children.get(name='regions').children.all()[0]
region_group_original_name = region_group.name
region_group.name = region_group.name + '-renamed'
region_group.save(update_fields=['name'])
cache_path3 = tempfile.mkdtemp(prefix='awx_ec2_')
self._temp_paths.append(cache_path3)
inventory_source.source_vars = '---\n\ncache_path: %s\n' % cache_path3
inventory_source.overwrite = False
inventory_source.save()
self.check_inventory_source(inventory_source, initial=False, instance_id_group_ok=True)
child_names = self.group.children.filter(active=True).values_list('name', flat=True)
self.assertTrue(region_group_original_name in self.group.children.get(name='regions').children.values_list('name', flat=True))
self.assertTrue(region_group.name in self.group.children.get(name='regions').children.values_list('name', flat=True))
return
# Print out group/host tree for debugging.
print