Collect information on inventory sources

Also remove one minor query from smart inventory collection that will
never return anything.
This commit is contained in:
Bill Nottingham
2020-04-08 18:07:01 -04:00
parent 2855be9d26
commit f99a43ffa6
2 changed files with 92 additions and 18 deletions

View File

@@ -122,22 +122,27 @@ def cred_type_counts(since):
return counts return counts
@register('inventory_counts', '1.1') @register('inventory_counts', '1.2')
def inventory_counts(since): def inventory_counts(since):
counts = {} counts = {}
for inv in models.Inventory.objects.filter(kind='').annotate(num_sources=Count('inventory_sources', distinct=True), for inv in models.Inventory.objects.filter(kind='').annotate(num_sources=Count('inventory_sources', distinct=True),
num_hosts=Count('hosts', distinct=True)).only('id', 'name', 'kind'): num_hosts=Count('hosts', distinct=True)).only('id', 'name', 'kind'):
source_list = []
for source in inv.inventory_sources.filter().annotate(num_hosts=Count('hosts', distinct=True)).values('name','source', 'num_hosts'):
source_list.append(source)
counts[inv.id] = {'name': inv.name, counts[inv.id] = {'name': inv.name,
'kind': inv.kind, 'kind': inv.kind,
'hosts': inv.num_hosts, 'hosts': inv.num_hosts,
'sources': inv.num_sources 'sources': inv.num_sources,
'source_list': source_list
} }
for smart_inv in models.Inventory.objects.filter(kind='smart'): for smart_inv in models.Inventory.objects.filter(kind='smart'):
counts[smart_inv.id] = {'name': smart_inv.name, counts[smart_inv.id] = {'name': smart_inv.name,
'kind': smart_inv.kind, 'kind': smart_inv.kind,
'hosts': smart_inv.hosts.count(), 'hosts': smart_inv.hosts.count(),
'sources': smart_inv.inventory_sources.count() 'sources': 0,
'source_list': []
} }
return counts return counts

View File

@@ -13,10 +13,10 @@ def test_empty():
"active_host_count": 0, "active_host_count": 0,
"credential": 0, "credential": 0,
"custom_inventory_script": 0, "custom_inventory_script": 0,
"custom_virtualenvs": 0, # dev env ansible3 "custom_virtualenvs": 0, # dev env ansible3
"host": 0, "host": 0,
"inventory": 0, "inventory": 0,
"inventories": {'normal': 0, 'smart': 0}, "inventories": {"normal": 0, "smart": 0},
"job_template": 0, "job_template": 0,
"notification_template": 0, "notification_template": 0,
"organization": 0, "organization": 0,
@@ -27,28 +27,97 @@ def test_empty():
"user": 0, "user": 0,
"workflow_job_template": 0, "workflow_job_template": 0,
"unified_job": 0, "unified_job": 0,
"pending_jobs": 0 "pending_jobs": 0,
} }
@pytest.mark.django_db @pytest.mark.django_db
def test_database_counts(organization_factory, job_template_factory, def test_database_counts(
workflow_job_template_factory): organization_factory, job_template_factory, workflow_job_template_factory
objs = organization_factory('org', superusers=['admin']) ):
jt = job_template_factory('test', organization=objs.organization, objs = organization_factory("org", superusers=["admin"])
inventory='test_inv', project='test_project', jt = job_template_factory(
credential='test_cred') "test",
workflow_job_template_factory('test') organization=objs.organization,
inventory="test_inv",
project="test_project",
credential="test_cred",
)
workflow_job_template_factory("test")
models.Team(organization=objs.organization).save() models.Team(organization=objs.organization).save()
models.Host(inventory=jt.inventory).save() models.Host(inventory=jt.inventory).save()
models.Schedule( models.Schedule(
rrule='DTSTART;TZID=America/New_York:20300504T150000', rrule="DTSTART;TZID=America/New_York:20300504T150000",
unified_job_template=jt.job_template unified_job_template=jt.job_template,
).save() ).save()
models.CustomInventoryScript(organization=objs.organization).save() models.CustomInventoryScript(organization=objs.organization).save()
counts = collectors.counts(None) counts = collectors.counts(None)
for key in ('organization', 'team', 'user', 'inventory', 'credential', for key in (
'project', 'job_template', 'workflow_job_template', 'host', "organization",
'schedule', 'custom_inventory_script'): "team",
"user",
"inventory",
"credential",
"project",
"job_template",
"workflow_job_template",
"host",
"schedule",
"custom_inventory_script",
):
assert counts[key] == 1 assert counts[key] == 1
@pytest.mark.django_db
def test_inventory_counts(organization_factory, inventory_factory):
(inv1, inv2, inv3) = [inventory_factory(f"inv-{i}") for i in range(3)]
s1 = inv1.inventory_sources.create(name="src1", source="ec2")
s2 = inv1.inventory_sources.create(name="src2", source="file")
s3 = inv1.inventory_sources.create(name="src3", source="gce")
s1.hosts.create(name="host1", inventory=inv1)
s1.hosts.create(name="host2", inventory=inv1)
s1.hosts.create(name="host3", inventory=inv1)
s2.hosts.create(name="host4", inventory=inv1)
s2.hosts.create(name="host5", inventory=inv1)
s3.hosts.create(name="host6", inventory=inv1)
s1 = inv2.inventory_sources.create(name="src1", source="ec2")
s1.hosts.create(name="host1", inventory=inv2)
s1.hosts.create(name="host2", inventory=inv2)
s1.hosts.create(name="host3", inventory=inv2)
inv_counts = collectors.inventory_counts(None)
assert {
inv1.id: {
"name": "inv-0",
"kind": "",
"hosts": 6,
"sources": 3,
"source_list": [
{"name": "src1", "source": "ec2", "num_hosts": 3},
{"name": "src2", "source": "file", "num_hosts": 2},
{"name": "src3", "source": "gce", "num_hosts": 1},
],
},
inv2.id: {
"name": "inv-1",
"kind": "",
"hosts": 3,
"sources": 1,
"source_list": [{"name": "src1", "source": "ec2", "num_hosts": 3}],
},
inv3.id: {
"name": "inv-2",
"kind": "",
"hosts": 0,
"sources": 0,
"source_list": [],
},
} == inv_counts