<commit_before>from __future__ import print_function, absolute_import, unicode_literals, division

import requests
import logging

from alppaca.compat import OrderedDict


class NoRolesFoundException(Exception):
    pass


class NoCredentialsFoundException(Exception):
    pass


class IMSCredentialsProvider(object):

    def __init__(self, ims_url, ims_protocol="https", debug=False):
        self.ims_host = ims_url
        self.ims_protocol = ims_protocol
        self.logger = logging.getLogger(__name__)

    def get_roles(self):
        """ Obtain a role. """
        try:
            request_template = "{protocol}://{host}/latest/meta-data/iam/security-credentials/"
            request_url = request_template.format(
                protocol=self.ims_protocol,
                host=self.ims_host)
            self.logger.debug("Requesting Roles from '%s'", request_url)
            response = requests.get(request_url)

            if response.status_code == 200:
                if not response.text:
                    self.logger.debug("Request for roles successfull, but empty response")
                    raise NoRolesFoundException("Server response was empty; host has no roles?")

                roles_list = [line.strip() for line in response.text.split("\n")] if response.text else []
                self.logger.debug("Received roles: {0}".format(roles_list))
                return roles_list
            else:
                self.logger.error('Request to "%s" failed', request_url)
                response.raise_for_status()
        except Exception as e:
            self.logger.exception("Due to following cause:")
            raise NoRolesFoundException(str(e))

    def get_credentials(self, role):
        """" Obtain a set of temporary credentials given a role. """
        try:
            request_template = "{protocol}://{host}/latest/meta-data/iam/security-credentials/{role}"
            request_url = request_template.format(
                protocol=self.ims_protocol,
                host=self.ims_host,
                role=role)
            self.logger.debug("Requesting Roles from '%s'", request_url)
            response = requests.get(request_url)
            if response.status_code == 200:
                if not response.text:
                    self.logger.debug("Request for credentials successfull, but empty response")
                    raise NoCredentialsFoundException("Server response was empty; no credentials for role: {0}".format(role))
                self.logger.debug("Request for credentials successfull")
                return response.text
            else:
                self.logger.error('Request for credentials to "%s" failed', request_url)
                response.raise_for_status()
        except Exception as e:
            self.logger.exception("Due to following cause:")
            raise NoCredentialsFoundException(str(e))

    def get_credentials_for_all_roles(self):
        """ Obtain all credentials for all roles. """
        results = OrderedDict()

        try:
            for role in self.get_roles():
                try:
                    results[role] = self.get_credentials(role)
                except NoCredentialsFoundException:
                    self.logger.exception("Role %s didn't have any credentials.", role)
        except NoRolesFoundException:
            self.logger.exception("Could not find any roles")
        return results<commit_msg>CLOUD-257 #15 Better log messages<commit_after>from __future__ import print_function, absolute_import, unicode_literals, division

import requests
import logging

from alppaca.compat import OrderedDict


class NoRolesFoundException(Exception):
    pass


class NoCredentialsFoundException(Exception):
    pass


class IMSCredentialsProvider(object):

    def __init__(self, ims_url, ims_protocol="https", debug=False):
        self.ims_host = ims_url
        self.ims_protocol = ims_protocol
        self.logger = logging.getLogger(__name__)

    def get_roles(self):
        """ Obtain a role. """
        try:
            request_template = "{protocol}://{host}/latest/meta-data/iam/security-credentials/"
            request_url = request_template.format(
                protocol=self.ims_protocol,
                host=self.ims_host)
            self.logger.debug("Requesting Roles from '%s'", request_url)
            response = requests.get(request_url)

            if response.status_code == 200:
                if not response.text:
                    self.logger.debug("Request for roles successful, but empty response")
                    raise NoRolesFoundException("Server response was empty; host has no roles?")

                roles_list = [line.strip() for line in response.text.split("\n")] if response.text else []
                self.logger.debug("Received roles: {0}".format(roles_list))
                return roles_list
            else:
                self.logger.error('Request to "%s" failed', request_url)
                response.raise_for_status()
        except Exception as e:
            self.logger.exception("Error getting roles:")
            raise NoRolesFoundException(str(e))

    def get_credentials(self, role):
        """" Obtain a set of temporary credentials given a role. """
        try:
            request_template = "{protocol}://{host}/latest/meta-data/iam/security-credentials/{role}"
            request_url = request_template.format(
                protocol=self.ims_protocol,
                host=self.ims_host,
                role=role)
            self.logger.debug("Requesting Credentials from '%s'", request_url)
            response = requests.get(request_url)
            if response.status_code == 200:
                if not response.text:
                    self.logger.debug("Request for credentials successful, but empty response")
                    raise NoCredentialsFoundException("Server response was empty; no credentials for role: {0}".format(role))
                self.logger.debug("Request for credentials successful")
                return response.text
            else:
                self.logger.error('Request for credentials to "%s" failed', request_url)
                response.raise_for_status()
        except Exception as e:
            self.logger.exception("Error getting credentials:")
            raise NoCredentialsFoundException(str(e))

    def get_credentials_for_all_roles(self):
        """ Obtain all credentials for all roles. """
        results = OrderedDict()

        try:
            for role in self.get_roles():
                try:
                    results[role] = self.get_credentials(role)
                except NoCredentialsFoundException:
                    self.logger.exception("Role %s didn't have any credentials.", role)
        except NoRolesFoundException:
            self.logger.exception("Could not find any roles")
        return results
{'fraction_non_alphanumeric': 0.09149949487660557, 'fraction_numerical': 0.0030307403665752633, 'mean_word_length': 2.1818181818181817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3420327', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1712, 'n_words': 435}