insights proxy

* Issue request to Red Hat Insights API from Tower
/hosts/<id>/insights endpoint. User the first found Satellite 6
Credential as Basic Auth requests to Insights API.
This commit is contained in:
Chris Meyers
2017-05-18 17:22:29 -04:00
parent 631f82b574
commit fb53ca8455
3 changed files with 43 additions and 1 deletions

View File

@@ -1205,6 +1205,7 @@ class HostSerializer(BaseSerializerWithVariables):
ad_hoc_commands = self.reverse('api:host_ad_hoc_commands_list', kwargs={'pk': obj.pk}), ad_hoc_commands = self.reverse('api:host_ad_hoc_commands_list', kwargs={'pk': obj.pk}),
ad_hoc_command_events = self.reverse('api:host_ad_hoc_command_events_list', kwargs={'pk': obj.pk}), ad_hoc_command_events = self.reverse('api:host_ad_hoc_command_events_list', kwargs={'pk': obj.pk}),
fact_versions = self.reverse('api:host_fact_versions_list', kwargs={'pk': obj.pk}), fact_versions = self.reverse('api:host_fact_versions_list', kwargs={'pk': obj.pk}),
insights = self.reverse('api:host_insights', kwargs={'pk': obj.pk}),
)) ))
if obj.inventory: if obj.inventory:
res['inventory'] = self.reverse('api:inventory_detail', kwargs={'pk': obj.inventory.pk}) res['inventory'] = self.reverse('api:inventory_detail', kwargs={'pk': obj.inventory.pk})

View File

@@ -120,6 +120,7 @@ host_urls = patterns('awx.api.views',
#url(r'^(?P<pk>[0-9]+)/single_fact/$', 'host_single_fact_view'), #url(r'^(?P<pk>[0-9]+)/single_fact/$', 'host_single_fact_view'),
url(r'^(?P<pk>[0-9]+)/fact_versions/$', 'host_fact_versions_list'), url(r'^(?P<pk>[0-9]+)/fact_versions/$', 'host_fact_versions_list'),
url(r'^(?P<pk>[0-9]+)/fact_view/$', 'host_fact_compare_view'), url(r'^(?P<pk>[0-9]+)/fact_view/$', 'host_fact_compare_view'),
url(r'^(?P<pk>[0-9]+)/insights/$', 'host_insights'),
) )
group_urls = patterns('awx.api.views', group_urls = patterns('awx.api.views',

View File

@@ -13,6 +13,7 @@ import socket
import subprocess import subprocess
import sys import sys
import logging import logging
import requests
from base64 import b64encode from base64 import b64encode
from collections import OrderedDict from collections import OrderedDict
@@ -70,7 +71,8 @@ from awx.conf.license import get_license, feature_enabled, feature_exists, Licen
from awx.main.models import * # noqa from awx.main.models import * # noqa
from awx.main.utils import * # noqa from awx.main.utils import * # noqa
from awx.main.utils import ( from awx.main.utils import (
callback_filter_out_ansible_extra_vars callback_filter_out_ansible_extra_vars,
decrypt_field,
) )
from awx.main.utils.filters import SmartFilter from awx.main.utils.filters import SmartFilter
@@ -2067,6 +2069,44 @@ class HostFactCompareView(SystemTrackingEnforcementMixin, SubDetailAPIView):
return Response(self.serializer_class(instance=fact_entry).data) return Response(self.serializer_class(instance=fact_entry).data)
class HostInsights(GenericAPIView):
model = Host
serializer_class = EmptySerializer
def get(self, request, *args, **kwargs):
host = self.get_object()
cred = None
if host.insights_system_id is None:
return Response(status=status.HTTP_204_NO_CONTENT)
creds = Credential.objects.filter(credential_type__name='Red Hat Satellite 6', credential_type__kind='cloud', credential_type__managed_by_tower=True)
if creds.count() > 0:
cred = creds[0]
else:
'''
TODO: Different ERROR code? .. definately add more information feedback in 'errors' key
'''
return Response(status=status.HTTP_204_NO_CONTENT)
username = cred.inputs['username']
password = decrypt_field(cred, 'password')
session = requests.Session()
session.auth = requests.auth.HTTPBasicAuth(username, password)
headers = {'Content-Type': 'application/json'}
res = session.get('https://access.redhat.com/r/insights/v3/systems/{}/reports/'.format(host.insights_system_id), headers=headers)
if res.status_code != 200:
return Response(status=status.HTTP_204_NO_CONTENT)
try:
return Response(res.json())
except ValueError:
return Response(status=status.HTTP_204_NO_CONTENT)
class GroupList(ListCreateAPIView): class GroupList(ListCreateAPIView):
model = Group model = Group