insights proxy robust error condition handling

This commit is contained in:
Chris Meyers
2017-05-22 15:02:16 -04:00
parent 9ba0ba07ec
commit 3793f1ec09

View File

@@ -2074,39 +2074,48 @@ class HostInsights(GenericAPIView):
model = Host model = Host
serializer_class = EmptySerializer serializer_class = EmptySerializer
def _extract_insights_creds(self, credential):
return (credential.inputs['username'], decrypt_field(credential, 'password'))
def _get_insights(self, url, username, password):
session = requests.Session()
session.auth = requests.auth.HTTPBasicAuth(username, password)
headers = {'Content-Type': 'application/json'}
return session.get(url, headers=headers, timeout=120)
def get_insights(self, url, username, password):
try:
res = self._get_insights(url, username, password)
except requests.exceptions.SSLError:
return (dict(error='SSLError while trying to connect to https://access.redhat.com/'), status.HTTP_500_INTERNAL_SERVER_ERROR)
except requests.exceptions.Timeout:
return (dict(error='Request to {} timed out'.format(url)), status.HTTP_504_GATEWAY_TIMEOUT)
if res.status_code != 200:
return (dict(error='Failed to gather reports and maintenance plans from Insights API. Server responded with {} status code and message {}'.format(res.status_code, res.content)), status.HTTP_500_INTERNAL_SERVER_ERROR)
try:
return (dict(insights_content=res.json()), status.HTTP_200_OK)
except ValueError:
return (None, status.HTTP_204_NO_CONTENT)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
host = self.get_object() host = self.get_object()
cred = None cred = None
ret = {}
if host.insights_system_id is None: if host.insights_system_id is None:
return Response(status=status.HTTP_204_NO_CONTENT) return Response(dict(error='This host is not recognized as an Insights host.'), status=status.HTTP_404_NOT_FOUND)
creds = Credential.objects.filter(credential_type__name='Red Hat Satellite 6', credential_type__kind='cloud', credential_type__managed_by_tower=True) 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: if creds.count() > 0:
cred = creds[0] cred = creds[0]
else: else:
''' return Response(dict(error='No Insights Credential found for the Inventory, "{}", that this host belongs to.'.format(host.inventory.name)), status=status.HTTP_404_NOT_FOUND)
TODO: Different ERROR code? .. definately add more information feedback in 'errors' key
'''
return Response(status=status.HTTP_204_NO_CONTENT)
username = cred.inputs['username'] url = 'https://access.redhat.com/r/insights/v3/systems/{}/reports/'.format(host.insights_system_id)
password = decrypt_field(cred, 'password') (username, password) = self._extract_insights_creds(cred)
(msg, status) = self.get_insights(url, username, password)
session = requests.Session() return Response(msg, status=status)
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:
ret['insights_content'] = res.json()
return Response(ret)
except ValueError:
return Response(status=status.HTTP_204_NO_CONTENT)
class GroupList(ListCreateAPIView): class GroupList(ListCreateAPIView):