Class: BjnInventory::SourceCommand::AwsEc2

Inherits:
BjnInventory::SourceCommand show all
Defined in:
lib/bjn_inventory/source_command/aws_ec2.rb

Instance Method Summary collapse

Methods inherited from BjnInventory::SourceCommand

#initialize, #logger, #run

Constructor Details

This class inherits a constructor from BjnInventory::SourceCommand

Instance Method Details

#new_client(region = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/bjn_inventory/source_command/aws_ec2.rb', line 29

def new_client(region=nil)
    opts = {
        region: (region || @regions.first)
    }
    opts.update({profile: @profile}) if @profile
    Aws::EC2::Client.new(opts)
end

#retrieve_entries(override_client = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bjn_inventory/source_command/aws_ec2.rb', line 37

def retrieve_entries(override_client=nil)
    @regions.map do |region|
        client = override_client || @client || new_client(region)
        reservations = case @filters
        when []
          client.describe_instances()
        else
          client.describe_instances(filters: @filters)
        end.reservations
        reservations.map do |reservation|
            reservation.instances.map do |instance|
                instance.to_hash
            end
        end
    end.flatten
end

#set_options(opt) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bjn_inventory/source_command/aws_ec2.rb', line 10

def set_options(opt)
    @regions = nil
    if opt[:region] and !opt[:region].empty?
        @regions = opt[:region].respond_to?(:to_ary) ? opt[:region] : [ opt[:region] ]
        region = @regions.first
    else
        region = 'us-west-2'
    end

    @filters = JSON.parse(opt[:filters] || '[ ]')

    @client = opt[:client]
    @profile = opt[:profile]
    client = @client || new_client(region)
    if @regions.nil? or @regions.empty?
        @regions = client.describe_regions().regions.map { |region| region.region_name }
    end
end