def _set_operation(service_name, operation_name): try: client = _get_client(service_name) except botocore.exceptions.UnknownEndpointError as e: raise NoRegionError(e) except botocore.exceptions.PartialCredentialsError as e: LOG.debug('Credentials incomplete') raise CredentialsError( 'Your credentials are not complete. Error: {0}'.format(e)) except botocore.exceptions.NoRegionError: raise NoRegionError() if not _verify_ssl: warnings.filterwarnings("ignore") return getattr(client, operation_name)
def make_api_call(service_name, operation_name, **operation_options): operation = _set_operation(service_name, operation_name) aggregated_error_message = [] max_attempts = 10 region = _region_name if not region: region = 'default' attempt = 0 while True: attempt += 1 if attempt > 1: LOG.debug('Retrying -- attempt #' + str(attempt)) delay = _get_delay(attempt) time.sleep(delay) try: LOG.debug('Making api call: (' + service_name + ', ' + operation_name + ') to region: ' + region + ' with args:' + str(operation_options)) response_data = operation(**operation_options) status = response_data['ResponseMetadata']['HTTPStatusCode'] LOG.debug('API call finished, status = ' + str(status)) if response_data: LOG.debug('Response: ' + str(response_data)) return response_data except botocore.exceptions.ClientError as e: _handle_response_code(e.response, attempt, aggregated_error_message) except botocore.parsers.ResponseParserError as e: LOG.debug('Botocore could not parse response received') if attempt > max_attempts: raise MaxRetriesError( 'Max retries exceeded for ResponseParserErrors' + os.linesep.join(aggregated_error_message)) aggregated_error_message.insert(attempt, str(e)) except botocore.exceptions.NoCredentialsError: LOG.debug('No credentials found') raise CredentialsError('Operation Denied. You appear to have no' ' credentials') except botocore.exceptions.PartialCredentialsError as e: LOG.debug('Credentials incomplete') raise CredentialsError(str(e)) except (botocore.exceptions.ValidationError, botocore.exceptions.ParamValidationError) as e: raise ValidationError(str(e)) except botocore.exceptions.BotoCoreError: LOG.error('Botocore Error') raise except IOError as error: if hasattr(error.args[0], 'reason') and str(error.args[0].reason) == \ '[Errno -2] Name or service not known': raise ConnectionError() LOG.error('Error while contacting Elastic Beanstalk Service') LOG.debug('error:' + str(error)) raise ServiceError(error)