Sha256: bfd9e8a96410302d6031199565bdb0ee6d9ddbc42204fb1f2fcd47c1ad430845

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

require 'aws-sdk'
require 'bjn_inventory/source_command'

module BjnInventory

    class SourceCommand

        class AwsEc2 < BjnInventory::SourceCommand

            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

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

            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

        end

    end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bjn_inventory-1.7.1 lib/bjn_inventory/source_command/aws_ec2.rb
bjn_inventory-1.7.0 lib/bjn_inventory/source_command/aws_ec2.rb
bjn_inventory-1.6.1 lib/bjn_inventory/source_command/aws_ec2.rb
bjn_inventory-1.5.1 lib/bjn_inventory/source_command/aws_ec2.rb
bjn_inventory-1.3.1 lib/bjn_inventory/source_command/aws_ec2.rb
bjn_inventory-1.3.0 lib/bjn_inventory/source_command/aws_ec2.rb