Method: App::AWSReports.parse_metadata

Defined in:
lib/aws/aws_reports.rb

.parse_metadata(regions) ⇒ Object

Parses the YML file and returns a bunch of Hashes. Throws straight-up runtime errors if something is wrong.

Returns:

  • void



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
48
49
50
51
52
53
54
55
56
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/aws/aws_reports.rb', line 19

def self.(regions)

    yml_file = "#{App::Opt::get_base_path}#{App::Opt::OPT_PATH}/awx/#{YML_FILE}"

    # Throw error if the aws-report.yml isn't found in the /opt folder.
    Blufin::Terminal::error("File not found: #{Blufin::Terminal::format_directory(yml_file)}", 'This file should be located in the /opt folder of the my (ruby-gem) source-code.', true) unless Blufin::Files::file_exists(yml_file)

    columns      = {}
    table_widths = {}
    export_map   = {}

    begin
        data = YAML.load_file(File.expand_path(yml_file))
    rescue => e
        Blufin::Terminal::error("Unable to parse #{Blufin::Terminal::format_action('YML')} file: #{Blufin::Terminal::format_directory(yml_file)}", e.message, true)
    end

    data.each do |resource, data|

        # Validates keys (generically).
        {
            KEY_REGIONS           => [],
            KEY_REGIONS_PREFERRED => [],
            KEY_CLI               => %w(command root)
        }.each do |required_key, required_nested_keys|
            raise RuntimeError, "Missing key: #{required_key} for \xe2\x86\x92 #{resource}" unless data.has_key?(required_key)
            required_nested_keys.each do |required_nested_key|
                raise RuntimeError, "Missing key: #{required_key}.#{required_nested_key} for \xe2\x86\x92 #{resource}" unless data[required_key].has_key?(required_nested_key)
            end
        end

        # Validate regions.
        validate_region_array(regions, data[KEY_REGIONS], resource) if data.has_key?(KEY_REGIONS)
        validate_region_array(regions, data[KEY_REGIONS_PREFERRED], resource) if data.has_key?(KEY_REGIONS_PREFERRED)

        # Validate (and parse) columns.
        if !data[KEY_COLUMNS].nil? && data[KEY_COLUMNS].any?
            if data.has_key?(KEY_COLUMNS) && data[KEY_COLUMNS].is_a?(Array)
                columns[resource]           = {} unless columns.has_key?(resource)
                width_wildcard_count        = 0
                width_total                 = App::AWSOutputter::REGION_WIDTH # Start with 15 to account for hard-coded region column.
                columns[resource]['Region'] = {
                    :key       => 'region',
                    :width     => App::AWSOutputter::REGION_WIDTH,
                    :formatter => 'region'
                }
                data[KEY_COLUMNS].each_with_index do |column, idx|
                    title     = column['title']
                    key       = column['key']
                    width     = column['width']
                    color     = column['color'].nil? ? 'default' : column['color']
                    formatter = column['formatter']
                    ['Region'].each do |reserved_column|
                        raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 #{reserved_column} is a reserved column." if title.downcase == reserved_column.downcase
                    end
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Missing: title" if title.nil?
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Missing: key" if key.nil?
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Missing: width" if width.nil?
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Title already exists: #{title}" if columns[resource].has_key?(title)
                    raise RuntimeError, "Expected width to be wildcard (*) or Integer greater than 0, instead got: (#{width.class}) #{width}" unless width == '*' || width.is_a?(Integer)
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Title length (#{title.length}) is longer that column-width (#{width}) : #{title}" if title.length > width.to_i && width.to_s.strip != '*'
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Formatter not recognized: #{formatter}, valid formatters are: #{App::AWSOutputter::get_formatter.join(', ')}" unless App::AWSOutputter::get_formatter.include?(formatter) || formatter.nil?
                    raise RuntimeError, "#{resource}.#{KEY_COLUMNS}[#{idx}] \xe2\x86\x92 Color not recognized: #{color}, valid colors are: #{App::AWSOutputter::get_color.join(', ')}" unless App::AWSOutputter::get_color.include?(color)
                    width_wildcard_count     = width_wildcard_count + 1 if width == '*'
                    width_total              = width_total + width unless width == '*'
                    columns[resource][title] = {
                        :key       => key,
                        :width     => width,
                        :color     => color,
                        :formatter => formatter
                    }
                end
                raise RuntimeError, "Wildcard width count must be exactly 1, got: #{width_wildcard_count}" unless width_wildcard_count == 1
                raise RuntimeError, "#{resource}.#{KEY_COLUMNS} \xe2\x86\x92 Total width of columns (#{width_total}) minus wildcard (*) & region exceeds 210." if width_total > 210
                table_widths[resource] = width_total
            end
        end
        # Validate Exports.
        unless data[KEY_EXPORT].nil?
            raise RuntimeError, "Expected #{resource}.export to be an Array, instead got: #{data[KEY_EXPORT].class}" unless data[KEY_EXPORT].is_a?(Array)
            raise RuntimeError, "Expected #{resource}.export to have at least 1 entry." unless data[KEY_EXPORT].any?
            data[KEY_EXPORT].each do |export|
                raise RuntimeError, "#{resource}.#{KEY_EXPORT} \xe2\x86\x92 Missing: id" unless export.has_key?('id')
                raise RuntimeError, "#{resource}.#{KEY_EXPORT} \xe2\x86\x92 Missing: value" unless export.has_key?('value')
                # Make sure formatter exists.
                App::AWSOutputter::get_formatter(export['valueFormatter']) if export.has_key?('valueFormatter')
                export.each { |key, val| raise RuntimeError, "Unexpected key: #{key} (#{val})" unless %w(id value valueFormatter description).include?(key) }
                export_map[resource] = [] unless export_map.has_key?(resource)
                export_map[resource] << export['id']
            end
        end
    end

    # TODO - Maybe add "custom" resources here for WorkMailEmails and SES Identities, etc. here...?? If so, these need custom logic.

    [columns, data, export_map, table_widths]
end