Beispiel #1
0
    def __getattr__(cls, key):
        """Make the config values accessible.

        This allows all config values to be available via calls like:
        Config.user
        """
        if key not in cls.config_data:
            # These are called during nose setup before logging is turned off
            # during testing. Not the best, but tests look better with these
            # supressed.
            if key not in ['__test__', 'address', 'im_class', '__self__']:
                Utils.error("Tried to access config value '" +
                            str(key) + "', which doesn't exist.")
            raise AttributeError
        return cls.config_data[key]
Beispiel #2
0
    def _check_response(self, resp, attempts=1):
        """Return true if the rseponse a good/reasonable one.

        If the HTTP status code is in the 200s, return True,
        otherwise try to determine what happened. If we're asked to retry,
        politely wait the appropraite amount of time and retry, otherwise,
        wait the retry_wait amount of time.

        Fail (return False) if we've exceeded our retry amount.
        """
        if resp is None:
            raise ValueError('A response wasn\'t received')

        if 200 <= resp.status_code < 300:
            return True

        # If we made it this far, we need to handle an exception
        if attempts >= Config.max_http_attempts or (resp.status_code != 429 and
                                                    resp.status_code != 423):
            Utils.error('Error recieved in API return. Response code: ' +
                        str(resp.status_code) + '. Reponse text: ' + resp.text)
            #            print(resp.headers)
            error_response = json.loads(resp.text)
            error_response['status_code'] = resp.status_code
            raise Exception(error_response)

        if resp.status_code == 423:  # "Busy"
            if 'Retry-After' in resp.headers:
                Utils.info('Received HTTP 423. Retry-After set to ' +
                           resp.headers['Retry-After'] +
                           ' sec. Waiting to retry.')  # noqa
                time.sleep(int(resp.headers['Retry-After']) + 1)
            else:
                Utils.info(
                    'Received HTTP 429. Too many requests. Waiting to retry.'
                )  # noqa
                time.sleep(Config.retry_wait)
            return False

        # Assume we're going to retry with exponential backoff
        # Should only get here on a 429 "too many requests" but it's
        # not clear from Skytap what their limits are on when we should retry.
        time.sleep(2**(attempts - 1))

        return False
Beispiel #3
0
    def _check_response(self, resp, attempts=1):
        """Return true if the rseponse a good/reasonable one.

        If the HTTP status code is in the 200s, return True,
        otherwise try to determine what happened. If we're asked to retry,
        politely wait the appropraite amount of time and retry, otherwise,
        wait the retry_wait amount of time.

        Fail (return False) if we've exceeded our retry amount.
        """
        if resp is None:
            raise ValueError('A response wasn\'t received')

        if 200 <= resp.status_code < 300:
            return True

        # If we made it this far, we need to handle an exception
        if attempts >= Config.max_http_attempts or (resp.status_code != 429 and
                                                    resp.status_code != 423):
            Utils.error('Error recieved in API return. Response code: ' + str(resp.status_code) + '. Reponse text: ' + resp.text)
#            print(resp.headers)
            error_response = json.loads(resp.text)
            error_response['status_code'] = resp.status_code
            raise Exception(error_response)

        if resp.status_code == 423:  # "Busy"
            if 'Retry-After' in resp.headers:
                Utils.info('Received HTTP 423. Retry-After set to ' +
                              resp.headers['Retry-After'] + ' sec. Waiting to retry.')  # noqa
                time.sleep(int(resp.headers['Retry-After']) + 1)
            else:
                Utils.info('Received HTTP 429. Too many requests. Waiting to retry.')  # noqa
                time.sleep(Config.retry_wait)
            return False

        # Assume we're going to retry with exponential backoff
        # Should only get here on a 429 "too many requests" but it's
        # not clear from Skytap what their limits are on when we should retry.
        time.sleep(2 ** (attempts - 1))

        return False
Beispiel #4
0
    # for most things you'd want to do.
    config_data = INITIAL_CONFIG

# Load config values and set up the class.

for key in Config:
    env_val = "SKYTAP_" + key.upper()
    if env_val in os.environ:
        Config.config_data[key] = os.environ[env_val]
        try:
            Config.config_data[key] = int(Config.config_data[key])
        except ValueError:
            pass

if os.environ.get('READTHEDOCS', None) != 'True':
    if Config.base_url != 'https://cloud.skytap.com':
        Utils.warning('Base URL is not Skytap\'s recommended value. ' +
                      'This very likely will break things.')

    if len(Config.token) == 0:
        Utils.error('No environment variable SKYTAP_TOKEN found. ' +
                    'Set this variable and try again.')
        raise ValueError

    if len(Config.user) == 0:
        Utils.error('No environment variable SKYTAP_USER found. ' +
                    'Set this variable and try again.')
        raise ValueError

Utils.log_level(Config.log_level)