Update how we repopulate the /host/:id/smart_inventories data

This commit is contained in:
Wayne Witzel III 2017-07-13 14:04:33 -04:00
parent e93f1d3e79
commit 6f1ef7fe0b
3 changed files with 32 additions and 10 deletions

View File

@ -58,7 +58,7 @@ import ansiconv
from social.backends.utils import load_backends
# AWX
from awx.main.tasks import send_notifications, update_host_smart_inventory_memberships
from awx.main.tasks import send_notifications
from awx.main.access import get_user_queryset
from awx.main.ha import is_ha_environment
from awx.api.authentication import TaskAuthentication, TokenGetAuthentication
@ -2005,14 +2005,6 @@ class HostSmartInventoriesList(SubListAPIView):
relationship = 'smart_inventories'
new_in_320 = True
def list(self, *args, **kwargs):
try:
if settings.AWX_REBUILD_SMART_MEMBERSHIP:
update_host_smart_inventory_memberships.delay()
return super(HostSmartInventoriesList, self).list(*args, **kwargs)
except Exception as e:
return Response(dict(error=_(unicode(e))), status=status.HTTP_400_BAD_REQUEST)
class HostActivityStreamList(ActivityStreamEnforcementMixin, SubListAPIView):

View File

@ -384,6 +384,21 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin):
self.websocket_emit_status('pending_deletion')
delete_inventory.delay(self.pk)
def _update_host_smart_inventory_memeberships(self):
if self.kind == 'smart' and settings.AWX_REBUILD_SMART_MEMBERSHIP:
def on_commit():
from awx.main.tasks import update_host_smart_inventory_memberships
update_host_smart_inventory_memberships.delay()
connection.on_commit(on_commit)
def save(self, *args, **kwargs):
self._update_host_smart_inventory_memeberships()
super(Inventory, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
self._update_host_smart_inventory_memeberships()
super(Inventory, self).delete(*args, **kwargs)
class SmartInventoryMembership(BaseModel):
'''
@ -568,6 +583,21 @@ class Host(CommonModelNameNotUnique):
host_name = self.variables_dict['ansible_host']
return host_name
def _update_host_smart_inventory_memeberships(self):
if settings.AWX_REBUILD_SMART_MEMBERSHIP:
def on_commit():
from awx.main.tasks import update_host_smart_inventory_memberships
update_host_smart_inventory_memberships.delay()
connection.on_commit(on_commit)
def save(self, *args, **kwargs):
self._update_host_smart_inventory_memeberships()
super(Host, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
self._update_host_smart_inventory_memeberships()
super(Host, self).delete(*args, **kwargs)
class Group(CommonModelNameNotUnique):
'''

View File

@ -347,7 +347,7 @@ def update_inventory_computed_fields(inventory_id, should_update_hosts=True):
def update_host_smart_inventory_memberships():
try:
with transaction.atomic():
smart_inventories = Inventory.objects.filter(kind='smart', host_filter__isnull=False)
smart_inventories = Inventory.objects.filter(kind='smart', host_filter__isnull=False, pending_deletion=False)
SmartInventoryMembership.objects.all().delete()
memberships = []
for smart_inventory in smart_inventories: