From 72da961550a1536f119ebe8e4a6ce3673d06a6fb Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Mon, 15 Apr 2019 15:18:19 -0400 Subject: [PATCH] Conform to the new output of the Insights system reports endpoint --- awx/api/views/__init__.py | 13 ++++++++----- awx/main/utils/insights.py | 39 +++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index a9b47ab92c..fbacad4112 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -1677,7 +1677,7 @@ class HostInsights(GenericAPIView): return headers - def _get_platform_id(self, host, session, headers): + def _get_platform_info(self, host, session, headers): url = '{}/api/inventory/v1/hosts?insights_id={}'.format( settings.INSIGHTS_URL_BASE, host.insights_system_id) res = self._call_insights_api(url, session, headers) @@ -1688,7 +1688,7 @@ class HostInsights(GenericAPIView): _('Could not translate Insights system ID {}' ' into an Insights platform ID.').format(host.insights_system_id)) - return platform_id + return res['results'][0] def _get_reports(self, platform_id, session, headers): url = '{}/api/insights/v1/system/{}/reports/'.format( @@ -1711,11 +1711,15 @@ class HostInsights(GenericAPIView): return remediations - def _get_insights(self, platform_id, session, headers): + def _get_insights(self, session, headers): + platform_info = self._get_platform_info(host, session, headers) + platform_id = platform_info['id'] reports = self._get_reports(platform_id, session, headers) remediations = self._get_remediations(platform_id, session, headers) - return {'insights_content': filter_insights_api_response(reports, remediations)} + return { + 'insights_content': filter_insights_api_response(platform_info, reports, remediations) + } def get(self, request, *args, **kwargs): host = self.get_object() @@ -1739,7 +1743,6 @@ class HostInsights(GenericAPIView): password = cred.get_input('password', default='') session = self._get_session(username, password) headers = self._get_headers() - platform_id = self._get_platform_id(host, session, headers) data = self._get_insights(platform_id, session, headers) return Response(data, status=status.HTTP_200_OK) diff --git a/awx/main/utils/insights.py b/awx/main/utils/insights.py index d4b4cf0d25..a21720794c 100644 --- a/awx/main/utils/insights.py +++ b/awx/main/utils/insights.py @@ -15,7 +15,7 @@ # by a different Insights endpoint -def filter_insights_api_response(reports, remediations): +def filter_insights_api_response(platform_info, reports, remediations): severity_mapping = { 1: 'INFO', 2: 'WARN', @@ -23,25 +23,24 @@ def filter_insights_api_response(reports, remediations): 4: 'CRITICAL' } - new_json = {} - if 'checked_on' in reports: - new_json['last_check_in'] = reports['checked_on'] - if 'active_reports' in reports: - new_json['reports'] = [] - for rep in reports['active_reports']: - new_report = { - 'rule': {}, - 'maintenance_actions': remediations - } - rule = rep.get('rule') or {} - for k in ['description', 'summary']: - if k in rule: - new_report['rule'][k] = rule[k] - if 'category' in rule: - new_report['rule']['category'] = rule['category']['name'] - if rule.get('total_risk') in severity_mapping: - new_report['rule']['severity'] = severity_mapping[rule['total_risk']] + new_json = { + 'last_check_in': platform_info.get('updated'), + 'reports': [], + } + for rep in reports: + new_report = { + 'rule': {}, + 'maintenance_actions': remediations + } + rule = rep.get('rule') or {} + for k in ['description', 'summary']: + if k in rule: + new_report['rule'][k] = rule[k] + if 'category' in rule: + new_report['rule']['category'] = rule['category']['name'] + if rule.get('total_risk') in severity_mapping: + new_report['rule']['severity'] = severity_mapping[rule['total_risk']] - new_json['reports'].append(new_report) + new_json['reports'].append(new_report) return new_json