Class: Inventory::Vpc

Inherits:
Base
  • Object
show all
Defined in:
lib/inventory/vpc.rb

Instance Method Summary collapse

Methods inherited from Base

eager_load!, inherited, #initialize, #report, #show, #sort, subclasses, #test_mode

Methods included from AwsServices

#acm, #cfn, #cw, #eb, #ec2, #ecs, #elbv1, #elbv2, #iam, #pricing, #rds, #route53

Methods included from Shared

#instances, #security_groups

Constructor Details

This class inherits a constructor from Inventory::Base

Instance Method Details

#dataObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/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

#headerObject



2
3
4
# File 'lib/inventory/vpc.rb', line 2

def header
  ["Name", "Vpc ID", "CIDR", "Subnets", "Instances"]
end

#instances_in(subnets) ⇒ Object



45
46
47
48
49
50
# File 'lib/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

#subnetsObject



52
53
54
# File 'lib/inventory/vpc.rb', line 52

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

#subnets_for(vpc) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/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) ⇒ Object

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



23
24
25
26
27
# File 'lib/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

#vpcsObject



29
30
31
# File 'lib/inventory/vpc.rb', line 29

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