Module: Backends::Ec2::Helpers::AwsConnectHelper

Defined in:
lib/backends/ec2/helpers/aws_connect_helper.rb

Class Method Summary collapse

Class Method Details

.handle_service_error(error, logger) ⇒ Object

Converts EC2 error codes to errors understood by rOCCI-server. This method will ALWAYS raise an error. See docs.aws.amazon.com/AWSEC2/latest/APIReference/api-error-codes.html

Parameters:

  • error (Aws::EC2::Errors::ServiceError)

    EC2 error instance

  • logger (Logger)

    instance of a logging facility

Server-side Effects:

  • none: call answered from within the backend


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/backends/ec2/helpers/aws_connect_helper.rb', line 41

def self.handle_service_error(error, logger)
  error_code = error.class.to_s.split('::').last
  message = "#{error_code}: #{error.message}"

  case error_code
  when 'Unavailable'
    # service is not available, probably EC2's fault
    fail Backends::Errors::ServiceUnavailableError, message
  when 'AuthFailure', 'Blocked', 'SignatureDoesNotMatch'
    # something is wrong with our credentials
    fail Backends::Errors::AuthenticationError, message
  when 'CannotDelete', 'DependencyViolation', 'IncorrectState', 'IncorrectInstanceState'
    # action wasn't allowed in this state or context
    fail Backends::Errors::ResourceStateError, message
  when /^(.+)Format$/, /^(.+)ZoneMismatch$/, /^(.+)AlreadyExists$/, /^(.+)Duplicate$/
    # something was wrong with our request
    fail Backends::Errors::ResourceNotValidError, message
  when /^(.+)InUse$/, /^(.+)Conflict$/, /^(.+)NotSupported$/
    # again, wrong state to perform the given action
    fail Backends::Errors::ResourceStateError, message
  when /^Insufficient(.+)Capacity$/, /^(.+)LimitExceeded$/, 'DiskImageSizeTooLarge'
    # not enough resources or requesting too much for current limits
    fail Backends::Errors::ResourceCreationError, message
  when /^(.+)Malformed$/, 'InvalidParameterValue'
    # what we sent was malformed or didn't have the proper format
    fail Backends::Errors::IdentifierNotValidError, message
  when /^(.+)NotFound$/
    #
    fail Backends::Errors::ResourceNotFoundError, message
  else
    # 'InternalError', ...
    fail Backends::Errors::ResourceActionError, message
  end
end

.rescue_aws_service(logger) ⇒ Object

Wraps calls to EC2 and provides basic error handling. This method requires a block, if no block is given a Backends::Errors::StubError error is raised.

Examples:

Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
  instance_statuses = @ec2_client.describe_instance_status.instance_statuses
  instance_statuses.each { |istatus| id_list << istatus[:instance_id] } if instance_statuses
end

Parameters:

  • logger (Logger)

    instance of a logging facility

Server-side Effects:

  • none: call answered from within the backend


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/backends/ec2/helpers/aws_connect_helper.rb', line 18

def self.rescue_aws_service(logger)
  fail Backends::Errors::StubError, 'AWS service-wrapper was called without a block!' unless block_given?

  begin
    yield
  rescue ::Aws::EC2::Errors::DryRunOperation => e
    logger.warn "[Backends] [Ec2] DryRun: #{e.message}"
    fail Backends::Errors::MethodNotImplementedError, e.message
  rescue ::Aws::EC2::Errors::ServiceError => e
    handle_service_error(e, logger)
  rescue ::Seahorse::Client::NetworkingError => e
    logger.error "[Backends] [Ec2] HTTP Error: #{e.message}"
    fail Backends::Errors::ServiceUnavailableError, e.message
  end
end