Repository: ansible/awx
File: awx/main/analytics/core.py (Python)

Code before:
    if not os.path.exists(path):
        logger.error('Automation Analytics TAR {} not found'.format(path))
        return False
    if "Error:" in str(path):
        return False

    logger.debug('shipping analytics file: {}'.format(path))
    url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
    if not url:
        logger.error('AUTOMATION_ANALYTICS_URL is not set')
        return False

    rh_id = getattr(settings, 'REDHAT_USERNAME', None) or getattr(settings, 'SUBSCRIPTIONS_CLIENT_ID', None)
    rh_secret = getattr(settings, 'REDHAT_PASSWORD', None) or getattr(settings, 'SUBSCRIPTIONS_CLIENT_SECRET', None)

    if not rh_id:
        logger.error('Neither REDHAT_USERNAME nor SUBSCRIPTIONS_CLIENT_ID are set')
        return False

    if not rh_secret:
        logger.error('Neither REDHAT_PASSWORD nor SUBSCRIPTIONS_CLIENT_SECRET are set')
        return False

    with open(path, 'rb') as f:
        files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
        s = requests.Session()
        s.headers = get_awx_http_client_headers()
        s.headers.pop('Content-Type')
        with set_environ(**settings.AWX_TASK_ENV):
            try:
                client = OIDCClient(rh_id, rh_secret, DEFAULT_OIDC_TOKEN_ENDPOINT, ['api.console'])
                response = client.make_request("POST", url, headers=s.headers, files=files, verify=settings.INSIGHTS_CERT_PATH, timeout=(31, 31))
            except requests.RequestException:
                logger.error("Automation Analytics API request failed, trying base auth method")
                response = s.post(url, files=files, verify=settings.INSIGHTS_CERT_PATH, auth=(rh_id, rh_secret), headers=s.headers, timeout=(31, 31))

        # Accept 2XX status_codes
        if response.status_code >= 300:
            logger.error('Upload failed with status {}, {}'.format(response.status_code, response.text))
            return False

        return True


Review comment (bug):
Looks like these clauses are related to your current api-test failure. Probably need to use `override_settings` to set the new settings in the test.

Code after:
    if not os.path.exists(path):
        logger.error('Automation Analytics TAR {} not found'.format(path))
        return False
    if "Error:" in str(path):
        return False

    logger.debug('shipping analytics file: {}'.format(path))
    url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
    if not url:
        logger.error('AUTOMATION_ANALYTICS_URL is not set')
        return False

    rh_id = getattr(settings, 'REDHAT_USERNAME', None)
    rh_secret = getattr(settings, 'REDHAT_PASSWORD', None)

    if not (rh_id and rh_secret):
        rh_id = getattr(settings, 'SUBSCRIPTIONS_CLIENT_ID', None)
        rh_secret = getattr(settings, 'SUBSCRIPTIONS_CLIENT_SECRET', None)

    if not rh_id:
        logger.error('Neither REDHAT_USERNAME nor SUBSCRIPTIONS_CLIENT_ID are set')
        return False

    if not rh_secret:
        logger.error('Neither REDHAT_PASSWORD nor SUBSCRIPTIONS_CLIENT_SECRET are set')
        return False

    with open(path, 'rb') as f:
        files = {'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)}
        s = requests.Session()
        s.headers = get_awx_http_client_headers()
        s.headers.pop('Content-Type')
        with set_environ(**settings.AWX_TASK_ENV):
            try:
                client = OIDCClient(rh_id, rh_secret)
                response = client.make_request("POST", url, headers=s.headers, files=files, verify=settings.INSIGHTS_CERT_PATH, timeout=(31, 31))
            except requests.RequestException:
                logger.error("Automation Analytics API request failed, trying base auth method")
                response = s.post(url, files=files, verify=settings.INSIGHTS_CERT_PATH, auth=(rh_id, rh_secret), headers=s.headers, timeout=(31, 31))

        # Accept 2XX status_codes
        if response.status_code >= 300:
            logger.error('Upload failed with status {}, {}'.format(response.status_code, response.text))
            return False

        return True