Repository: ansible/awx
File: awx/playbooks/action_plugins/insights.py (Python)

Code before:
    def save_playbook(self, proj_path, remediation, content):
        name = remediation.get('name', None) or 'insights-remediation'
        name = re.sub(r'[^\w\s-]', '', name).strip().lower()
        name = re.sub(r'[-\s]+', '-', name)
        fname = '{}-{}.yml'.format(name, remediation['id'])
        file_path = os.path.join(proj_path, fname)
        with open(file_path, 'wb') as f:
            f.write(content)

    def is_stale(self, proj_path, etag):
        file_path = os.path.join(proj_path, '.version')
        try:
            with open(file_path, 'r') as f:
                version = f.read()
            return version != etag
        except IOError:
            return True

    def write_version(self, proj_path, etag):
        file_path = os.path.join(proj_path, '.version')
        with open(file_path, 'w') as f:
            f.write(etag)

    def _obtain_auth_token(self, oidc_endpoint, client_id, client_secret):
        main_url = urljoin(oidc_endpoint, '/.well-known/openid-configuration')
        response = requests.get(url=main_url, headers={'Accept': 'application/json'})
        data = {}
        if response.status_code != 200:
            data['failed'] = True
            data['msg'] = 'Expected {} to return a status code of 200 but returned status code "{}" instead with content "{}".'.format(
                main_url, response.status_code, response.content
            )
            return data

        auth_url = response.json().get('token_endpoint', None)
        data = {
            'grant_type': 'client_credentials',
            'scope': 'api.console',
            'client_id': client_id,
            'client_secret': client_secret,
        }
        response = requests.post(url=auth_url, data=data)

        if response.status_code != 200:
            data['failed'] = True
            data['msg'] = 'Expected {} to return a status code of 200 but returned status code "{}" instead with content "{}".'.format(
                auth_url, response.status_code, response.content
            )
        else:
            data['token'] = response.json().get('access_token', None)
            data['token_type'] = response.json().get('token_type', None)

Review comment (suggestion):
with the below line change, we no longer need this conditional, rstrip will take off the `/` if it is there, otherwise it does nothing

Code after:
class ActionModule(ActionBase):
    def save_playbook(self, proj_path, remediation, content):
        name = remediation.get('name', None) or 'insights-remediation'
        name = re.sub(r'[^\w\s-]', '', name).strip().lower()
        name = re.sub(r'[-\s]+', '-', name)
        fname = '{}-{}.yml'.format(name, remediation['id'])
        file_path = os.path.join(proj_path, fname)
        with open(file_path, 'wb') as f:
            f.write(content)

    def is_stale(self, proj_path, etag):
        file_path = os.path.join(proj_path, '.version')
        try:
            with open(file_path, 'r') as f:
                version = f.read()
            return version != etag
        except IOError:
            return True

    def write_version(self, proj_path, etag):
        file_path = os.path.join(proj_path, '.version')
        with open(file_path, 'w') as f:
            f.write(etag)

    def _obtain_auth_token(self, oidc_endpoint, client_id, client_secret):
        if oidc_endpoint.endswith('/'):
            oidc_endpoint = oidc_endpoint.rstrip('/')
        main_url = oidc_endpoint + '/.well-known/openid-configuration'
        response = requests.get(url=main_url, headers={'Accept': 'application/json'})
        data = {}
        if response.status_code != 200:
            data['failed'] = True
            data['msg'] = 'Expected {} to return a status code of 200 but returned status code "{}" instead with content "{}".'.format(
                main_url, response.status_code, response.content
            )
            return data

        auth_url = response.json().get('token_endpoint', None)
        data = {
            'grant_type': 'client_credentials',
            'scope': 'api.console',
            'client_id': client_id,
            'client_secret': client_secret,
        }
        response = requests.post(url=auth_url, data=data)

        if response.status_code != 200:
            data['failed'] = True
            data['msg'] = 'Expected {} to return a status code of 200 but returned status code "{}" instead with content "{}".'.format(
                auth_url, response.status_code, response.content
            )