Sha256: dbb2d541b21bc0ae8818aab08f74f8eaf0d2512440b282c3ffa5797db1d1abbc

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

class AwsInventory::Vpc < AwsInventory::Base
  def header
    ["Name", "Vpc ID", "CIDR", "Subnets", "Instances"]
  end

  def data
    vpcs.map do |vpc|
      subnets = subnets_for(vpc)
      instances = instances_in(subnets)

      [
        vpc_name(vpc.vpc_id),
        vpc.vpc_id,
        vpc.cidr_block,
        subnets.count,
        instances.count
      ]
    end
  end

  # Pretty vpc name
  # Use vpc_id as argument so other classes can use this method also
  def vpc_name(vpc_id)
    vpc = vpcs.find { |vpc| vpc.vpc_id == vpc_id }
    tag = vpc.tags.find {|t| t.key == "Name"}
    name = tag ? tag.value : "(unnamed)"
  end

  def vpcs
    @vpcs ||= ec2.describe_vpcs.vpcs
  end

  def subnets_for(vpc)
    ec2.describe_subnets(
      filters: [
        {
          name: "vpc-id",
          values: [
            vpc.vpc_id,
          ],
        },
    ]).subnets
  end

  def instances_in(subnets)
    subnet_ids = subnets.map(&:subnet_id)
    instances.select do |i|
      subnet_ids.include?(i.subnet_id)
    end
  end

  def subnets
    @subnets ||= ec2.describe_subnets.subnets
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aws-inventory-0.4.5 lib/aws_inventory/vpc.rb
aws-inventory-0.4.3 lib/aws_inventory/vpc.rb
aws-inventory-0.4.2 lib/aws_inventory/vpc.rb
aws-inventory-0.4.0 lib/aws_inventory/vpc.rb