Class: AwsEc2DnsName

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_ec2_dns_name.rb,
lib/aws_ec2_dns_name/instance.rb

Defined Under Namespace

Classes: Instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region: nil, access_key_id: nil, secret_access_key: nil) ⇒ AwsEc2DnsName

Returns a new instance of AwsEc2DnsName.

Parameters:

  • region (String) (defaults to: nil)
  • access_key_id (String) (defaults to: nil)
  • secret_access_key (String) (defaults to: nil)


13
14
15
16
17
# File 'lib/aws_ec2_dns_name.rb', line 13

def initialize(region: nil, access_key_id: nil, secret_access_key: nil)
  self.client = Aws::EC2::Client.new(region: region,
                                     access_key_id: access_key_id,
                                     secret_access_key: secret_access_key)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/aws_ec2_dns_name.rb', line 8

def client
  @client
end

Instance Method Details

#instancesArray<AwsEc2DnsName:Instance> Also known as: list

Returns:

  • (Array<AwsEc2DnsName:Instance>)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aws_ec2_dns_name.rb', line 20

def instances
  client.describe_instances.first.reservations.map do |reservation|
    ec2_instance = reservation.instances.first
    name_tag = ec2_instance.tags.find { |tag| tag.key == "Name" }
    next unless name_tag
    name_tag_value = name_tag.value
    dns_name = dns_name(ec2_instance)

    # dns_name of terminated instance is empty string
    next if dns_name == ""

    AwsEc2DnsName::Instance.new(name_tag_value, dns_name)
  end.compact.sort_by { |i| i[:name_tag] }
end