Class: EC2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2cli/ec2-client.rb

Constant Summary collapse

API_VERSION =
'2013-02-01'
SIGNATURE_VERSION =
2
SIGNATURE_ALGORITHM =
:SHA256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessKeyId, secretAccessKey, endpoint_or_region) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
# File 'lib/ec2cli/ec2-client.rb', line 23

def initialize(accessKeyId, secretAccessKey, endpoint_or_region)
  @accessKeyId = accessKeyId
  @secretAccessKey = secretAccessKey
  set_endpoint_and_region(endpoint_or_region)
  @debug = false
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



21
22
23
# File 'lib/ec2cli/ec2-client.rb', line 21

def debug
  @debug
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



19
20
21
# File 'lib/ec2cli/ec2-client.rb', line 19

def endpoint
  @endpoint
end

#regionObject (readonly)

Returns the value of attribute region.



20
21
22
# File 'lib/ec2cli/ec2-client.rb', line 20

def region
  @region
end

Instance Method Details

#aws_sign(params) ⇒ Object



100
101
102
103
104
105
# File 'lib/ec2cli/ec2-client.rb', line 100

def aws_sign(params)
  params = params.sort_by {|a, b| a.to_s }.map {|k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
  string_to_sign = "POST\n#{@endpoint}\n/\n#{params}"
  digest = OpenSSL::HMAC.digest(OpenSSL::Digest.const_get(SIGNATURE_ALGORITHM).new, @secretAccessKey, string_to_sign)
  Base64.encode64(digest).gsub("\n", '')
end

#query(action, params = {}) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ec2cli/ec2-client.rb', line 34

def query(action, params = {})
  params = {
    :Action           => action,
    :Version          => API_VERSION,
    :Timestamp        => Time.now.getutc.strftime('%Y-%m-%dT%H:%M:%SZ'),
    :SignatureVersion => SIGNATURE_VERSION,
    :SignatureMethod  => "Hmac#{SIGNATURE_ALGORITHM}",
    :AWSAccessKeyId   => @accessKeyId,
  }.merge(params)

  signature = aws_sign(params)
  params[:Signature] = signature

 if @debug
    $stderr.puts("---request begin---\n\#{params.pretty_inspect}\n---request end---\n")
  end

  https = Net::HTTP.new(@endpoint, 443)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE

  source = nil

  https.start do |w|
    req = Net::HTTP::Post.new('/',
      'Host' => @endpoint,
      'Content-Type' => 'application/x-www-form-urlencoded'
    )

    req.set_form_data(params)
    res = w.request(req)

    unless res.kind_of?(Net::HTTPOK)
      raise EC2::Error, "#{res.code} #{res.message}"
    end

    source = res.body
  end

  if /<Response>\s*<Errors>\s*<Error>/ =~ source
    errors = []

    REXML::Document.new(source).each_element('//Errors/Error') do |element|
      code = element.text('Code')
      message = element.text('Message')
      errors << "#{code}:#{message}"
    end

    rraise EC2::Error, errors.join(', ') unless errors.empty?
  end

  if @debug
    $stderr.puts("---response begin---\n\#{source}\n---response end---\n")
  end

  return source
end

#set_endpoint_and_region(endpoint_or_region) ⇒ Object



30
31
32
# File 'lib/ec2cli/ec2-client.rb', line 30

def set_endpoint_and_region(endpoint_or_region)
  @endpoint, @region = EC2::Endpoint.endpoint_and_region(endpoint_or_region)
end