Fix inventory source name to exclude replacement text, fix inventory update name to be based only on inventory and group name. Fixes https://trello.com/c/zYrHMz9S

This commit is contained in:
Chris Church 2015-06-02 19:46:08 -04:00
parent bcf4688a55
commit 77ab83eb0d
2 changed files with 18 additions and 4 deletions

View File

@ -1113,7 +1113,7 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions):
# 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 not self.name or old_name_re.match(self.name) or '__replace_' in self.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:
@ -1130,10 +1130,10 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions):
update_fields.append('name')
# Do the actual save.
super(InventorySource, self).save(*args, **kwargs)
# Add the PK to the name if only attached to an inventory (no group).
if new_instance and self.inventory and replace_text in self.name:
# Add the PK to the name.
if replace_text in self.name:
self.name = self.name.replace(replace_text, str(self.pk))
self.save(update_fields=['name'])
super(InventorySource, self).save(update_fields=['name'])
if not getattr(_inventory_updates, 'is_updating', False):
self.inventory.update_computed_fields(update_groups=False, update_hosts=False)
@ -1230,6 +1230,14 @@ class InventoryUpdate(UnifiedJob, InventorySourceOptions):
self.license_error = True
if 'license_error' not in update_fields:
update_fields.append('license_error')
inventory_source = self.inventory_source
if self.active and inventory_source.inventory and self.name == inventory_source.name:
if inventory_source.group:
self.name = '%s (%s)' % (inventory_source.group.name, inventory_source.inventory.name)
else:
self.name = inventory_source.inventory.name
if 'name' not in update_fields:
update_fields.append('name')
super(InventoryUpdate, self).save(*args, **kwargs)
def get_absolute_url(self):

View File

@ -1778,6 +1778,12 @@ class InventoryUpdatesTest(BaseTransactionTest):
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))
# Replacement text should not be left in inventory source name.
self.assertFalse(InventorySource.objects.filter(name__icontains='__replace_').exists())
# Inventory update name should be based on inventory/group names and need not have the inventory source pk.
print InventoryUpdate.objects.values_list('name', 'inventory_source__name')
for inventory_update in InventoryUpdate.objects.all():
self.assertFalse(inventory_update.name.endswith(inventory_update.inventory_source.name), inventory_update.name)
return
# Print out group/host tree for debugging.
print