Method: App::AWSReports.get_aws_data

Defined in:
lib/aws/aws_reports.rb

.get_aws_data(regions, resource, resource_title, silent: false) ⇒ Object

This runs the AWS GET and does various things depending on the the flags passed.

Returns:

  • void



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/aws/aws_reports.rb', line 119

def self.get_aws_data(regions, resource, resource_title, silent: false)
    response  = {}
    results   = []
    threads   = []
    semaphore = Mutex.new
    regions   = resource.has_key?(App::AWSReports::KEY_REGIONS) ? resource[App::AWSReports::KEY_REGIONS] : regions
    puts unless silent
    regions.each do |region|
        response[region] = {} if response[region].nil?
        sleep(0.01) unless silent
        threads << Thread.new {
            cmd = "aws #{resource[App::AWSReports::KEY_CLI][App::AWSReports::KEY_COMMAND]}#{region == App::AWSReports::CONST_GLOBAL ? '' : " --region #{region}"}#{App::AWS::get_profile_for_cli}"
            App::AWSOutputter::output_cli_command(cmd) unless silent
            json = `#{cmd}`
            begin
                semaphore.synchronize do
                    if json.strip == ''
                        response[region] = []
                    else
                        response[region] = JSON.parse(json)
                    end
                end
            rescue => e
                puts json.inspect
                raise RuntimeError, "JSON parsing (for: #{resource_title}) failed:\n\n#{e.message}"
            end
        }
    end
    sleep(0.1) unless silent
    puts unless silent

    # Display spinner while waiting for threads to finish.
    Blufin::Terminal::execute_proc("AWS \xe2\x80\x94 Fetching: #{Blufin::Terminal::format_highlight(resource_title, false)}", Proc.new {
        threads.each { |thread| thread.join }
    }, verbose: !silent)
    puts unless silent

    # Extract the regional response(s) (from multi-calls to different regions) and aggregate into a single Array.
    root_keys = resource[App::AWSReports::KEY_CLI]['root']

    if resource[App::AWSReports::KEY_REGIONS].length == 1 && resource[App::AWSReports::KEY_REGIONS][0] == App::AWSReports::CONST_GLOBAL
        results = recursively_get_results(App::AWSReports::CONST_GLOBAL, response[App::AWSReports::CONST_GLOBAL], root_keys)
    else
        response.each do |region, regional_response|
            recursively_get_results(region, regional_response, root_keys).each do |regional_result|
                results << regional_result
            end
        end
    end
    results
end