Class: RightScale::CloudApi::AWS::ApiManager

Inherits:
CloudApi::ApiManager
  • Object
show all
Includes:
Mixin::QueryApiPatterns
Defined in:
lib/cloud/aws/base/manager.rb

Overview

Thread un-safe parent class for almost all the AWS services.

Defined Under Namespace

Classes: Error

Constant Summary collapse

COMMON_QUERY_PARAMS =

A list of common AWS API params

[
  'Action',
  'AWSAccessKeyId',
  'Expires',
  'SecurityToken',
  'Signature',
  'SignatureMethod',
  'SignatureVersion',
  'Timestamp',
  'Version',
]

Instance Method Summary collapse

Constructor Details

#initialize(aws_access_key_id, aws_secret_access_key, endpoint, options = {}) ⇒ ApiManager

This method is abstract.

Constructor

Examples:

new(key_id, secret, 'https://ec2.awsamazon.com', :cache => true)

Parameters:



87
88
89
90
91
# File 'lib/cloud/aws/base/manager.rb', line 87

def initialize(aws_access_key_id, aws_secret_access_key, endpoint, options={})
  credentials = { :aws_access_key_id     => aws_access_key_id,
                  :aws_secret_access_key => aws_secret_access_key }
  super(credentials, endpoint, options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Provides an ability to call methods by their API action names

Examples:

# the calls below produce the same result:
# way #1
ec2.api('DescribeInstances', 'InstanceId' => ['i-00000001', 'i-00000002'])
# way #2
ec2.DescribeInstances('InstanceId' => ['i-00000001', 'i-00000002'])

Parameters:

  • method_name (String, Symbol)
  • args (Objects)

Returns:

  • (Object)


172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cloud/aws/base/manager.rb', line 172

def method_missing(method_name, *args, &block)
  begin
    invoke_query_api_pattern_method(method_name, *args, &block)
  rescue PatternNotFoundError
    if method_name.to_s[/\A[A-Z]/]
      api(method_name, *args, &block)
    else
      super
    end
  end
end

Instance Method Details

#api(action, params = {}, &block) ⇒ Object

Makes a raw API call to AWS compatible service by the method name

Examples:

ec2.api('DescribeImages')
ec2.api('DescribeInstances', 'InstanceId' => ['i-00000001', 'i-00000002'])

Parameters:

  • action (String)

    Depends on the selected service/endpoint. See /

  • params (Hash) (defaults to: {})

    A set or extra parameters.

Options Hash (params):

  • :body (Hash, String)

    The request body.

  • :headers (Hash)

    The request headers.

  • :options (Hash)

    The request options (RightScale::CloudApi::ApiManager.process_api_request).

  • :params (Hash)

    The extra set of URL params.

Returns:

  • (Object)

    Usually a Hash with data.



112
113
114
115
116
117
118
119
120
# File 'lib/cloud/aws/base/manager.rb', line 112

def api(action, params={}, &block)
  params['Action'] ||= action.to_s._snake_case._camelize
  opts = {}
  opts[:body]    = params.delete(:body)
  opts[:headers] = params.delete(:headers) || {}
  opts[:options] = params.delete(:options) || {}
  opts[:params]  = parametrize(params)
  process_api_request(:post, '', opts, &block)
end

#parametrize(*args) ⇒ Hash Also known as: p9e

Parametrize data to the format that Amazon EC2 and compatible services accept

See Utils::AWS.parametrize for more examples.

Examples:

parametrize( 'ParamA'             => 'a',
             'ParamB'             => ['b', 'c'],
             'ParamC.?.Something' => ['d', 'e'],
             'Filter'             => [ { 'Key' => 'A', 'Value' => ['aa','ab']},
                                       { 'Key' => 'B', 'Value' => ['ba','bb']}] ) #=>
  {
    "Filter.1.Key"       => "A",
    "Filter.1.Value.1"   => "aa",
    "Filter.1.Value.2"   => "ab",
    "Filter.2.Key"       => "B",
    "Filter.2.Value.1"   => "ba",
    "Filter.2.Value.2"   => "bb",
    "ParamA"             => "a",
    "ParamB.1"           => "b",
    "ParamB.2"           => "c",
    "ParamC.1.Something" => "d",
    "ParamC.2.Something" => "e"
  }

Returns:

  • (Hash)

    A hash of data in the format Amazon want to get.



149
150
151
# File 'lib/cloud/aws/base/manager.rb', line 149

def parametrize(*args)
  Utils::AWS.parametrize(*args)
end