Class: BjnInventory::SourceCommand::AwsElb

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

#_retrieve_classic_entries(current, client, chunk_result) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 91

def _retrieve_classic_entries(current, client, chunk_result)
    if chunk_result.load_balancer_descriptions.length > 0
        chunk = Hash[chunk_result.load_balancer_descriptions.map { |lb| [lb.load_balancer_name, lb.to_h] }]
        logger.debug "Describing load balancer tags"
        # Amazon only allows 20 names to be submitted
        chunk_tags = Hash[client.describe_tags(load_balancer_names: chunk.keys).tag_descriptions.map do |lbtags|
            [lbtags.load_balancer_name, { tags: lbtags.to_h[:tags] }]
        end]
        logger.debug "... described"
        chunk_list = chunk.map { |lbname, lb| lb.merge(chunk_tags[lbname]) }
    else
        chunk_list = [ ]
    end
    chunk_list = @filters.select(chunk_list)
    return chunk_list
end

#_retrieve_entries(current, client, marker = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 73

def _retrieve_entries(current, client, marker=nil)
    logger.debug "client: #{client.class.to_s}"
    logger.debug "Describing load balancers (page #{marker || 0})"
    chunk_result = client.describe_load_balancers(marker: marker, page_size: @page_size)
    logger.debug "... described"
    new_marker = chunk_result.next_marker
    if client.class.to_s.include?('Aws::ElasticLoadBalancing::Client')
        chunk_list = _retrieve_classic_entries(current, client, chunk_result)
    else
        chunk_list = _retrieve_v2_entries(current, client, chunk_result)
    end
    if new_marker
        return _retrieve_entries(current + chunk_list, client, new_marker)
    else
        return current + chunk_list
    end
end

#_retrieve_v2_entries(current, client, chunk_result) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 108

def _retrieve_v2_entries(current, client, chunk_result)
    if chunk_result.load_balancers.length > 0
        chunk = Hash[chunk_result.load_balancers.map { |lb| [lb.load_balancer_arn, lb.to_h] }]
        logger.debug "Describing load balancer tags"
        # Amazon only allows 20 names to be submitted
        chunk_tags = Hash[client.describe_tags(resource_arns: chunk.keys).tag_descriptions.map do |lbtags|
            [lbtags.resource_arn, { tags: lbtags.to_h[:tags] }]
        end]
        logger.debug "... described"
        logger.debug "Describing load balancer v2 listeners"
        chunk_listeners = {}
        chunk.keys.each do |lb_arn|
            listener_list = client.describe_listeners(load_balancer_arn: lb_arn).listeners.map do |lb_listener|
                lb_listener.to_h
            end
            chunk_listeners[lb_arn] = {
                listener_descriptions: listener_list.map do |lb_listener|
                    { listener: lb_listener.merge({load_balancer_port: lb_listener[:port]}) }
                end
            }
        end
        logger.debug "... described"
        chunk_list = chunk.map { |lbname, lb| lb.merge(chunk_tags[lbname]).merge(chunk_listeners[lbname]) }
    else
        chunk_list = [ ]
    end
    chunk_list = @filters.select(chunk_list)
    return chunk_list
end

#new_classic_client(region = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 42

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

#new_client(region = nil) ⇒ Object



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

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

#new_ec2_client(region = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 34

def new_ec2_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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 58

def retrieve_entries(override_client=nil)
    entries = @regions.map do |region|
        if override_client || @client
            client = override_client || @client
            logger.debug "client: #{client.class.to_s}"
            all_lbs = _retrieve_entries([], client)
        else
            logger.debug "region: #{region}"
            classic_lbs = _retrieve_entries([], new_classic_client(region))
            v2_lbs = _retrieve_entries([], new_client(region))
            all_lbs = classic_lbs + v2_lbs
        end
    end.flatten
end

#set_options(opt) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bjn_inventory/source_command/aws_elb.rb', line 11

def set_options(opt)
    # Amazon only allows 20 names in describe_tags, so this is one way of accomplishing that
    @page_size = opt[:page_size] || 20
    @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 = BjnInventory::Util::Filter::JsonAws.new(filters: opt[:filters] || '[ ]', logger: logger)

    @client = opt[:client]
    @ec2_client = opt[:ec2_client]
    @profile = opt[:profile]
    ec2_client = @ec2_client || new_ec2_client(region)
    if @regions.nil? or @regions.empty?
        logger.debug "Listing regions"
        @regions = ec2_client.describe_regions().regions.map { |region| region.region_name }
    end
end