Class: BjnInventory::SourceCommand::AwsRds

Inherits:
BjnInventory::SourceCommand show all
Defined in:
lib/bjn_inventory/source_command/aws_rds.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



49
50
51
52
53
54
55
# File 'lib/bjn_inventory/source_command/aws_rds.rb', line 49

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

#retrieve_entries(override_client = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bjn_inventory/source_command/aws_rds.rb', line 57

def retrieve_entries(override_client=nil)
    filter_map = {}
    @tag_filters.each do |tag_filter|
        filter_map[tag_filter['key']] = tag_filter['values']
    end
        
    @regions.map do |region|
        client = override_client || @client || new_client(region)
        client.describe_db_clusters(filters: @filters).db_clusters.collect do |cluster|
            tags = client.list_tags_for_resource(resource_name:cluster.db_cluster_arn)
            cluster_hash = cluster.to_hash
            tag_map = {}
            filter_map.keys.each do |key|
                tag_map[key] = false
            end
            cluster_hash['tags'] = tags.tag_list.map do |tag|
                if filter_map.keys.include?(tag.key) && \
                   filter_map[tag.key].include?(tag.value)
                    tag_map[tag.key] = true
                end
                tag.to_hash
            end
            if tag_map.values.include? false
                []
            else
                [ cluster_hash ]
            end
        end
    end.flatten
end

#set_options(opt) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bjn_inventory/source_command/aws_rds.rb', line 12

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] || '[ ]')

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

    @client = opt[:client]
    @profile = opt[:profile]
    client = @client || new_client(region)
    region_list = %w{
        us-east-2
        us-east-1
        us-west-1
        us-west-2
        ca-central-1
        ap-south-1
        ap-northeast-2
        ap-southeast-1
        ap-southeast-2
        ap-northeast-1
        eu-central-1
        eu-west-1
        eu-west-2
        sa-east-1
    }
    if @regions.nil? or @regions.empty?
        @regions = region_list
    end
end