-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathec2_client_wrapper.rb
73 lines (60 loc) · 2.19 KB
/
ec2_client_wrapper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
#
# Copyright 2008-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
require 'credentials'
require 'amazon/retry_delegator'
require 'amazon/coral/ec2client'
class Ec2ClientWrapper
attr_accessor :commands, :logger, :options
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
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
def is_error_response(response)
response != nil && response.key?('Error')
end
def raise_on_error(response)
if is_error_response(response) then
raise RuntimeError, response["Error"].inspect
end
return response
end
def allocate_address()
logger.trace "AllocateAddress()"
result = @client.AllocateAddress()
logger.trace result.inspect
return raise_on_error(result)
end
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
#TODO: Add stubs for all other Ec2 WS operations here, see http://s3.amazonaws.com/ec2-downloads/2010-11-15.ec2.wsdl
end