Conform to the new output of the Insights system reports endpoint

This commit is contained in:
Jeff Bradberry 2019-04-15 15:18:19 -04:00
parent 4c86c5065c
commit 72da961550
2 changed files with 27 additions and 25 deletions

View File

@ -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)

View File

@ -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