Include in the API the count of hosts used by an organization

This commit is contained in:
Jeff Bradberry
2019-02-06 13:26:13 -05:00
parent e44c73883e
commit 6399ec59c9
3 changed files with 36 additions and 2 deletions

View File

@@ -29,7 +29,7 @@ from awx.main.models.ha import (
)
from awx.main.models.organization import Team
from awx.main.models.projects import Project
from awx.main.models.inventory import Inventory
from awx.main.models.inventory import Inventory, Host
from awx.main.models.jobs import JobTemplate
from awx.conf.license import (
feature_enabled,
@@ -235,6 +235,8 @@ class OrganizationCountsMixin(object):
db_results['projects'] = project_qs\
.values('organization').annotate(Count('organization')).order_by('organization')
db_results['hosts'] = Host.objects.active_counts_by_org()
# Other members and admins of organization are always viewable
db_results['users'] = org_qs.annotate(
users=Count('member_role__members', distinct=True),
@@ -246,7 +248,7 @@ class OrganizationCountsMixin(object):
org_id = org['id']
count_context[org_id] = {
'inventories': 0, 'teams': 0, 'users': 0, 'job_templates': 0,
'admins': 0, 'projects': 0}
'admins': 0, 'projects': 0, 'hosts': 0}
for res, count_qs in db_results.items():
if res == 'job_templates_project':
@@ -255,6 +257,8 @@ class OrganizationCountsMixin(object):
org_reference = JT_inventory_reference
elif res == 'users':
org_reference = 'id'
elif res == 'hosts':
org_reference = 'inventory__organization'
else:
org_reference = 'organization'
for entry in count_qs:

View File

@@ -17,6 +17,7 @@ from awx.conf.license import (
from awx.main.models import (
ActivityStream,
Inventory,
Host,
Project,
JobTemplate,
WorkflowJobTemplate,
@@ -119,6 +120,7 @@ class OrganizationDetail(RelatedJobsPreventDeleteMixin, RetrieveUpdateDestroyAPI
organization__id=org_id).count()
org_counts['job_templates'] = JobTemplate.accessible_objects(**access_kwargs).filter(
project__organization__id=org_id).count()
org_counts['hosts'] = Host.objects.org_active_count(org_id)
full_context['related_field_counts'] = {}
full_context['related_field_counts'][org_id] = org_counts