require 'aws-sdk' require 'bjn_inventory/source_command' require 'bjn_inventory/util/filter/json_aws' module BjnInventory class SourceCommand class AwsElb < BjnInventory::SourceCommand 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 def new_ec2_client(region=nil) opts = { region: (region || @regions.first) } opts.update({profile: @profile}) if @profile Aws::EC2::Client.new(opts) end def new_client(region=nil) opts = { region: (region || @regions.first) } opts.update({profile: @profile}) if @profile Aws::ElasticLoadBalancing::Client.new(opts) end def retrieve_entries(override_client=nil) entries = @regions.map do |region| client = override_client || @client || new_client(region) _retrieve_entries([], client) end.flatten end def _retrieve_entries(current, client, marker=nil) logger.debug "Describing load balancers (page #{marker})" chunk_result = client.describe_load_balancers(marker: marker, page_size: @page_size) logger.debug "... described" marker = chunk_result.next_marker 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) if marker return _retrieve_entries(current + chunk_list, client, marker) else return current + chunk_list end end end end end