mirror of
https://github.com/ansible/awx.git
synced 2026-03-02 17:28:51 -03:30
Guard against the case where Insights fails to find the system ID
This commit is contained in:
@@ -31,7 +31,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
|
|
||||||
|
|
||||||
# Django REST Framework
|
# Django REST Framework
|
||||||
from rest_framework.exceptions import APIException, PermissionDenied, ParseError
|
from rest_framework.exceptions import APIException, PermissionDenied, ParseError, NotFound
|
||||||
from rest_framework.parsers import FormParser
|
from rest_framework.parsers import FormParser
|
||||||
from rest_framework.permissions import AllowAny, IsAuthenticated
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||||
from rest_framework.renderers import JSONRenderer, StaticHTMLRenderer
|
from rest_framework.renderers import JSONRenderer, StaticHTMLRenderer
|
||||||
@@ -1680,7 +1680,10 @@ class HostInsights(GenericAPIView):
|
|||||||
url = '{}/api/inventory/v1/hosts?insights_id={}'.format(
|
url = '{}/api/inventory/v1/hosts?insights_id={}'.format(
|
||||||
settings.INSIGHTS_URL_BASE, host.insights_system_id)
|
settings.INSIGHTS_URL_BASE, host.insights_system_id)
|
||||||
res = self._call_insights_api(url, session, headers)
|
res = self._call_insights_api(url, session, headers)
|
||||||
platform_id = res['results'][0]['id']
|
try:
|
||||||
|
platform_id = res['results'][0]['id']
|
||||||
|
except (IndexError, KeyError):
|
||||||
|
raise NotFound(_('This host is not recognized as an Insights host.'))
|
||||||
|
|
||||||
return platform_id
|
return platform_id
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,28 @@ class TestHostInsights:
|
|||||||
assert response.data['error'] == 'This host is not recognized as an Insights host.'
|
assert response.data['error'] == 'This host is not recognized as an Insights host.'
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
|
def test_insights_host_missing_from_insights(self, get, hosts, insights_credential, user, mocker):
|
||||||
|
class Response:
|
||||||
|
status_code = 200
|
||||||
|
content = "{'results': []}"
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
return {'results': []}
|
||||||
|
|
||||||
|
mocker.patch.object(requests.Session, 'get', return_value=Response())
|
||||||
|
|
||||||
|
host = hosts(host_count=1)[0]
|
||||||
|
host.insights_system_id = '123e4567-e89b-12d3-a456-426655440000'
|
||||||
|
host.inventory.insights_credential = insights_credential
|
||||||
|
host.inventory.save()
|
||||||
|
host.save()
|
||||||
|
|
||||||
|
url = reverse('api:host_insights', kwargs={'pk': host.pk})
|
||||||
|
response = get(url, user('admin', True))
|
||||||
|
|
||||||
|
assert response.data['error'] == 'This host is not recognized as an Insights host.'
|
||||||
|
assert response.status_code == 404
|
||||||
|
|
||||||
def test_insights_no_credential(self, get, hosts, user, mocker):
|
def test_insights_no_credential(self, get, hosts, user, mocker):
|
||||||
mocker.patch.object(requests.Session, 'get')
|
mocker.patch.object(requests.Session, 'get')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user