From 8a599d9754190e0cbee87f4317611150286ae167 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Fri, 28 Apr 2017 11:48:24 -0400 Subject: [PATCH] Add Inventory.kind field --- awx/main/managers.py | 25 ++++++++++++------------ awx/main/migrations/0038_v320_release.py | 5 +++++ awx/main/models/inventory.py | 12 ++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/awx/main/managers.py b/awx/main/managers.py index 436a6f3918..57d518d270 100644 --- a/awx/main/managers.py +++ b/awx/main/managers.py @@ -23,21 +23,22 @@ class HostManager(models.Manager): return len(set(self.values_list('name', flat=True))) def get_queryset(self): - """When the Inventory this host belongs to has a `host_filter` set - generate the QuerySet using that filter. Otherwise just return the default filter. + """When the parent instance of the host query set has a `kind` of dynamic and a `host_filter` + set. Use the `host_filter` to generate the queryset for the hosts. """ qs = super(HostManager, self).get_queryset() if self.instance is not None: - if hasattr(self.instance, 'host_filter') and self.instance.host_filter is not None: - q = DynamicFilter.query_from_string(self.instance.host_filter) - # If we are using host_filters, disable the core_filters, this allows - # us to access all of the available Host entries, not just the ones associated - # with a specific FK/relation. - # - # If we don't disable this, a filter of {'inventory': self.instance} gets automatically - # injected by the related object mapper. - self.core_filters = {} - return qs.filter(q) + if hasattr(self.instance, 'kind') and self.instance.kind == 'dynamic': + if hasattr(self.instance, 'host_filter') and self.instance.host_filter is not None: + q = DynamicFilter.query_from_string(self.instance.host_filter) + # If we are using host_filters, disable the core_filters, this allows + # us to access all of the available Host entries, not just the ones associated + # with a specific FK/relation. + # + # If we don't disable this, a filter of {'inventory': self.instance} gets automatically + # injected by the related object mapper. + self.core_filters = {} + return qs.filter(q) return qs diff --git a/awx/main/migrations/0038_v320_release.py b/awx/main/migrations/0038_v320_release.py index 10297a4c61..fb0b6bcb8c 100644 --- a/awx/main/migrations/0038_v320_release.py +++ b/awx/main/migrations/0038_v320_release.py @@ -41,6 +41,11 @@ class Migration(migrations.Migration): name='host_filter', field=awx.main.fields.DynamicFilterField(default=None, help_text='Filter that will be applied to the hosts of this inventory.', null=True, blank=True), ), + migrations.AddField( + model_name='inventory', + name='kind', + field=models.CharField(default=b'standard', help_text='Kind of inventory being represented.', max_length=32, choices=[(b'standard', 'Hosts have a direct link to this inventory.'), (b'dynamic', 'Hosts for inventory generated using the host_filter property.')]), + ), # Facts migrations.AlterField( diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 28d079c61c..1ddbc36801 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -46,6 +46,11 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin): an inventory source contains lists and hosts. ''' + KIND_CHOICES = [ + ('standard', _('Hosts have a direct link to this inventory.')), + ('dynamic', _('Hosts for inventory generated using the host_filter property.')), + ] + class Meta: app_label = 'main' verbose_name_plural = _('inventories') @@ -103,6 +108,13 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin): editable=False, help_text=_('Number of external inventory sources in this inventory with failures.'), ) + kind = models.CharField( + max_length=32, + choices=KIND_CHOICES, + blank=False, + default='standard', + help_text=_('Kind of inventory being represented.'), + ) host_filter = DynamicFilterField( blank=True, null=True,