Class: Murakumo::Util::EC2Client

Inherits:
Object
  • Object
show all
Defined in:
lib/util/murakumo_ec2_client.rb

Constant Summary collapse

API_VERSION =
'2011-12-15'
SIGNATURE_VERSION =
2
SIGNATURE_ALGORITHM =
:SHA256

Instance Method Summary collapse

Constructor Details

#initialize(accessKeyId, secretAccessKey, endpoint = nil) ⇒ EC2Client

Returns a new instance of EC2Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/util/murakumo_ec2_client.rb', line 18

def initialize(accessKeyId, secretAccessKey, endpoint = nil)
  unless endpoint
    local_hostname = Net::HTTP.get('169.254.169.254', '/latest/meta-data/local-hostname')

    if /\A[^.]+\.([^.]+)\.compute\.internal\Z/ =~ local_hostname
      endpoint = $1
    else
      endpoint = 'us-east-1'
    end
  end

  @accessKeyId = accessKeyId
  @secretAccessKey = secretAccessKey
  @endpoint = endpoint

  if /\A[^.]+\Z/ =~ @endpoint
    @endpoint = "ec2.#{@endpoint}.amazonaws.com"
  end
end

Instance Method Details

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



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
# File 'lib/util/murakumo_ec2_client.rb', line 38

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

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

  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)

    res.body
  end
end