Class: Ec2ClientWrapper

Inherits:
Object show all
Defined in:
lib/ec2_client_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, logger) ⇒ Ec2ClientWrapper

Returns a new instance of Ec2ClientWrapper.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ec2_client_wrapper.rb', line 11

def initialize(commands, logger)
  @commands = commands
  @logger = logger
  @options = commands.global_options

  @config = {
    :endpoint            => @options[:ec2_endpoint] || "https://ec2.amazonaws.com",
    :api_version         => '2010-11-15',
    :ca_file             => File.join(File.dirname(__FILE__), "cacert.pem"),
    :aws_access_key      => @options[:aws_access_id],
    :aws_secret_key      => @options[:aws_secret_key],
    :signature_algorithm => :V2,
    :verbose             => (@options[:verbose] != nil)
  }

  @client = Amazon::RetryDelegator.new(
    Amazon::Coral::Ec2Client.new_aws_query(@config),
    :retry_if => Proc.new { |*opts| self.is_retryable_error_response(*opts) }
  )
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



9
10
11
# File 'lib/ec2_client_wrapper.rb', line 9

def commands
  @commands
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/ec2_client_wrapper.rb', line 9

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/ec2_client_wrapper.rb', line 9

def options
  @options
end

Instance Method Details

#allocate_addressObject



56
57
58
59
60
61
# File 'lib/ec2_client_wrapper.rb', line 56

def allocate_address()
  logger.trace "AllocateAddress()"
  result = @client.AllocateAddress()
  logger.trace result.inspect
  return raise_on_error(result)
end

#associate_address(instance_id, public_ip) ⇒ Object



63
64
65
66
67
68
# File 'lib/ec2_client_wrapper.rb', line 63

def associate_address(instance_id, public_ip)
  logger.trace "AssociateAddress('InstanceId' => #{instance_id.inspect}, 'PublicIp' => #{public_ip.inspect})"
  result = @client.AssociateAddress('InstanceId' => instance_id, 'PublicIp' => public_ip)
  logger.trace result.inspect
  return raise_on_error(result)
end

#is_error_response(response) ⇒ Object



45
46
47
# File 'lib/ec2_client_wrapper.rb', line 45

def is_error_response(response)
  response != nil && response.key?('Error')
end

#is_retryable_error_response(response) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ec2_client_wrapper.rb', line 32

def is_retryable_error_response(response)
  if response == nil then
    false
  else
    ret = false
    if response['Error'] then 
      # note: 'Timeout' is not retryable because the operation might have completed just the connection timed out
      ret ||= ['Throttling', 'ServiceUnavailable'].include?(response['Error']['Code'])
    end
    ret 
  end
end

#raise_on_error(response) ⇒ Object



49
50
51
52
53
54
# File 'lib/ec2_client_wrapper.rb', line 49

def raise_on_error(response)
  if is_error_response(response) then
    raise RuntimeError, response["Error"].inspect
  end
  return response
end