Class: Awspec::Type::Ec2

Inherits:
Base
  • Object
show all
Defined in:
lib/awspec/type/ec2.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#ec2_client, #id

Instance Method Summary collapse

Methods inherited from Base

#find_network_acl, #find_route_table, #find_security_group, #find_vpc

Constructor Details

#initialize(id) ⇒ Ec2

Returns a new instance of Ec2.



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
# File 'lib/awspec/type/ec2.rb', line 5

def initialize(id)
  super
  @client = @ec2_client
  if id.is_a?(Array)
    # Aws::EC2::Client.describe_instances native filters format
    res = @client.describe_instances({
                                       filters: id
                                     })
  elsif id.is_a?(Hash)
    # syntax sugar
    filters = []
    id.each do |k, v|
      filters.push({ name: k, values: Array(v) })
    end
    res = @client.describe_instances({
                                       filters: filters
                                     })
  else
    # instance_id or tag:Name
    begin
      res = @client.describe_instances({
                                         instance_ids: [id]
                                       })
    rescue
      # Aws::EC2::Errors::InvalidInstanceIDMalformed
      # Aws::EC2::Errors::InvalidInstanceIDNotFound
      res = @client.describe_instances({
                                         filters: [{ name: 'tag:Name', values: [id] }]
                                       })
    end
  end
  @id = res[:reservations][0][:instances][0][:instance_id]
  @instance = res[:reservations][0][:instances][0]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/awspec/type/ec2.rb', line 51

def method_missing(name)
  describe = name.to_s
  if @instance.key?(describe)
    @instance[describe]
  else
    super
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/awspec/type/ec2.rb', line 3

def client
  @client
end

#instanceObject (readonly)

Returns the value of attribute instance.



3
4
5
# File 'lib/awspec/type/ec2.rb', line 3

def instance
  @instance
end

Instance Method Details

#has_eip?(ip_address = nil) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/awspec/type/ec2.rb', line 60

def has_eip?(ip_address = nil)
  option = {
    filters: [{ name: 'instance-id', values: [@id] }]
  }
  option[:public_ips] = [ip_address] if ip_address
  ret = @client.describe_addresses(option)
  ret[:addresses].count == 1
end

#has_security_group?(sg_id) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/awspec/type/ec2.rb', line 69

def has_security_group?(sg_id)
  sgs = @instance[:security_groups]
  ret = sgs.find do |sg|
    sg[:group_id] == sg_id || sg[:group_name] == sg_id
  end
  return true if ret
  sg2 = find_security_group(sg_id)
  sg2[:tags].find do |tag|
    tag[:key] == 'Name' && tag[:value] == sg_id
  end
end