Sha256: 4731b13f61329e24eeb2d405a54c60ccfa535b1e7987634cce76db6460238ec6

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

module Elasticity

  class MissingKeyError < StandardError; end
  class MissingRegionError < StandardError; end
  class ThrottlingException < StandardError; end

  class AwsSession

    attr_reader :host
    attr_reader :region

    # Supported values for options:
    #  :region - AWS region (e.g. us-west-1)
    #  :secure - true or false, default true.
    def initialize(options={})
      # There is a cryptic error if this isn't set
      if options.has_key?(:region) && options[:region] == nil
        raise MissingRegionError, 'A valid :region is required to connect to EMR'
      end
      options[:region] = 'us-east-1' unless options[:region]
      @region = options[:region]

      @host = "elasticmapreduce.#@region.amazonaws.com"
    end

    def submit(ruby_service_hash)
      aws_request = AwsRequestV4.new(self, ruby_service_hash)
      begin
        RestClient.post(aws_request.url, aws_request.payload, aws_request.headers)
      rescue RestClient::BadRequest => e
        type, message = AwsSession.parse_error_response(e.http_body)
        raise ThrottlingException, message if type == 'ThrottlingException'
        raise ArgumentError, message
      end
    end

    def ==(other)
      return false unless other.is_a? AwsSession
      return false unless @host == other.host
      true
    end

    private

    # AWS error responses all follow the same form.  Extract the message from
    # the error document.
    def self.parse_error_response(error_json)
      error = JSON.parse(error_json)
      [
        error['__type'],
        "AWS EMR API Error (#{error['__type']}): #{error['message']}"
      ]
    end

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
elasticity-6.0.12 lib/elasticity/aws_session.rb
elasticity-6.0.11 lib/elasticity/aws_session.rb
elasticity-6.0.10 lib/elasticity/aws_session.rb
elasticity-6.0.9 lib/elasticity/aws_session.rb
elasticity-6.0.8 lib/elasticity/aws_session.rb
elasticity-6.0.7 lib/elasticity/aws_session.rb
elasticity-6.0.6 lib/elasticity/aws_session.rb
elasticity-6.0.5 lib/elasticity/aws_session.rb