Class: Inventory::Ec2

Inherits:
Base
  • Object
show all
Defined in:
lib/inventory/ec2.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

#cost(instance) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/inventory/ec2.rb', line 31

def cost(instance)
  cost_type = COST_MAP[instance.instance_type]
  if cost_type
    cost = cost_type[platform(instance)]
    cost.round(2)
  end
end

#dataObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/inventory/ec2.rb', line 6

def data
  instances.map do |i|
    name = name_from_tag(i)
    group_names = security_group_names(i)

    [
      name,
      i.instance_id,
      i.instance_type,
      platform(i), # windows or linux
      group_names,
    ]
  end
end

#headerObject



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

def header
  ["Name", "Instance Id", "Instance Type", "Platform", "Security Groups"]
end

#name_from_tag(instance) ⇒ Object



21
22
23
24
25
# File 'lib/inventory/ec2.rb', line 21

def name_from_tag(instance)
  tags = instance.tags
  name_tag = tags.find { |t| t.key == "Name" }
  name_tag ? name_tag.value : "(unnamed)"
end

#platform(instance) ⇒ Object



39
40
41
# File 'lib/inventory/ec2.rb', line 39

def platform(instance)
  instance.platform || "linux"
end

#security_group_names(instance) ⇒ Object



27
28
29
# File 'lib/inventory/ec2.rb', line 27

def security_group_names(instance)
  instance.security_groups.map {|sg| sg.group_name}.join(', ')
end