class AwsInventory::Vpc

Public Instance Methods

data() click to toggle source
# File lib/aws_inventory/vpc.rb, line 6
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
header() click to toggle source
# File lib/aws_inventory/vpc.rb, line 2
def header
  ["Name", "Vpc ID", "CIDR", "Subnets", "Instances"]
end
instances_in(subnets) click to toggle source
# File lib/aws_inventory/vpc.rb, line 45
def instances_in(subnets)
  subnet_ids = subnets.map(&:subnet_id)
  instances.select do |i|
    subnet_ids.include?(i.subnet_id)
  end
end
subnets() click to toggle source
# File lib/aws_inventory/vpc.rb, line 52
def subnets
  @subnets ||= ec2.describe_subnets.subnets
end
subnets_for(vpc) click to toggle source
# File lib/aws_inventory/vpc.rb, line 33
def subnets_for(vpc)
  ec2.describe_subnets(
    filters: [
      {
        name: "vpc-id",
        values: [
          vpc.vpc_id,
        ],
      },
  ]).subnets
end
vpc_name(vpc_id) click to toggle source

Pretty vpc name Use vpc_id as argument so other classes can use this method also

# File lib/aws_inventory/vpc.rb, line 23
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
vpcs() click to toggle source
# File lib/aws_inventory/vpc.rb, line 29
def vpcs
  @vpcs ||= ec2.describe_vpcs.vpcs
end