diff --git a/awx/api/serializers.py b/awx/api/serializers.py index f5a27baefc..7045f5c037 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1205,6 +1205,7 @@ class HostSerializer(BaseSerializerWithVariables): 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}), 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: res['inventory'] = self.reverse('api:inventory_detail', kwargs={'pk': obj.inventory.pk}) diff --git a/awx/api/urls.py b/awx/api/urls.py index 80f5eb7248..f764ff3ffd 100644 --- a/awx/api/urls.py +++ b/awx/api/urls.py @@ -120,6 +120,7 @@ host_urls = patterns('awx.api.views', #url(r'^(?P[0-9]+)/single_fact/$', 'host_single_fact_view'), url(r'^(?P[0-9]+)/fact_versions/$', 'host_fact_versions_list'), url(r'^(?P[0-9]+)/fact_view/$', 'host_fact_compare_view'), + url(r'^(?P[0-9]+)/insights/$', 'host_insights'), ) group_urls = patterns('awx.api.views', diff --git a/awx/api/views.py b/awx/api/views.py index 8116f35bbb..2e3c25da0b 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -13,6 +13,7 @@ import socket import subprocess import sys import logging +import requests from base64 import b64encode 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.utils import * # noqa 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 @@ -2067,6 +2069,44 @@ class HostFactCompareView(SystemTrackingEnforcementMixin, SubDetailAPIView): 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): model = Group