Class: EC2ResponseObject

Inherits:
Object show all
Defined in:
lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb

Overview

Convenience class to convert standard amazon-ec2 responses from their camel cased style to a hash using underscore style. For example: instanceId to instance_id

Class Method Summary collapse

Class Method Details

.get_descriptions(resp) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 4

def self.get_descriptions(resp)      
  rs = get_instance_from_response(resp)
  group = get_group_from_response(resp)
  
  # puts rs.methods.sort - rs.ancestors.methods
  out = begin
    if rs.respond_to?(:instancesSet)
      [EC2ResponseObject.get_hash_from_response(rs.instancesSet.item, group)]
    else
      rs.collect {|r| 
        if r.instancesSet.item.class == Array
          r.instancesSet.item.map {|t| EC2ResponseObject.get_hash_from_response(t, group)}
        else
          [EC2ResponseObject.get_hash_from_response(r.instancesSet.item, group)]
        end            
      }.flatten.reject {|a| a.nil? }
    end
  rescue Exception => e
    # Really weird bug with amazon's ec2 gem
    rs.collect {|r| EC2ResponseObject.get_hash_from_response(r)}.reject {|a| a.nil? } rescue []
  end

  out
end

.get_group_from_response(resp) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 39

def self.get_group_from_response(resp)
  begin
    resp = resp.reservationSet.item.first if resp.reservationSet.item.is_a?(Array)
    group = resp.reservationSet.item.groupSet.item.groupId unless resp.reservationSet.nil?
    group ||= resp.groupSet.item[0].groupId rescue nil
    group ||= resp.DescribeInstancesResponse.reservationSet.item.groupSet.item.groupId
    #rs ||= rs.respond_to?(:instancesSet) ? rs.instancesSet : rs
    #rs.reject! {|a| a.nil? || a.empty? }
  rescue Exception => e
    resp
  end
  group
end

.get_hash_from_response(resp, group = 'default') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 52

def self.get_hash_from_response(resp, group = 'default')
    {
      :instance_id => resp.instanceId,
      :name => resp.instanceId, 
      :ip => resp.dnsName || "not-assigned",
      :status => resp.instanceState.name,
      :launching_time => resp.launchTime.parse_datetime,
      :internal_ip => resp.privateDnsName,
      :keypair => resp.keyName,
      :security_group => group
    }        
end

.get_instance_from_response(resp) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 28

def self.get_instance_from_response(resp)
  begin
    rs = resp.reservationSet.item unless resp.reservationSet.nil?
    rs ||= resp.DescribeInstancesResponse.reservationSet.item
    rs ||= rs.respond_to?(:instancesSet) ? rs.instancesSet : rs
    rs.reject! {|a| a.nil? || a.empty? }
  rescue Exception => e
    resp
  end
  rs
end