Sha256: cfafe820165e8474fab179b8b874e8aa593c36507a5c9809bfeb68b92ff91157

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true
require "aws-sdk"

require "aws_ec2_dns_name/instance"

class AwsEc2DnsName
  attr_accessor :client

  # @param [String] region
  # @param [String] access_key_id
  # @param [String] secret_access_key
  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

  # @return [Array<AwsEc2DnsName:Instance>]
  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" }.value
      dns_name = dns_name(ec2_instance)

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

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

  private

  # @param [Aws::EC2::Types::Instance] ec2_instance
  # @return [String]
  def dns_name(ec2_instance)
    public_dns_name = ec2_instance.public_dns_name
    private_dns_name = ec2_instance.private_dns_name

    if public_dns_name.empty?
      private_dns_name
    else
      public_dns_name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aws_ec2_dns_name-0.1.4 lib/aws_ec2_dns_name.rb