lib/aws/cfn/compiler/base.rb in aws-cfn-compiler-0.9.9 vs lib/aws/cfn/compiler/base.rb in aws-cfn-compiler-0.9.12

- old
+ new

@@ -15,33 +15,41 @@ attr_accessor :spec def initialize super @items = {} + @dynamic_items = {} end + def dynamic_item(section,resource,hash) + @dynamic_items[section][resource] = hash + end + def validate(compiled) abort! 'No Resources!?' unless compiled['Resources'] logStep 'Validating template' + # --- Parameters ---------------------------------------------------------------------------------------------- prms = compiled['Parameters'].keys rescue [] - compiled['Parameters'].each do |name,hash| - abort! "Parameter #{name} has an invalid compiled block!\n#{hash.ai}" unless hash.is_a?(Hash) - if hash['Type'] - unless %w(String Number CommaDelimitedList).include?(hash['Type']) - abort! "Parameter #{name} has an invalid type: #{hash['Type']}" + if compiled['Parameters'] + compiled['Parameters'].each do |name,hash| + abort! "Parameter #{name} has an invalid compiled block!\n#{hash.ai}" unless hash.is_a?(Hash) + if hash['Type'] + unless %w(String Number CommaDelimitedList).include?(hash['Type']) + abort! "Parameter #{name} has an invalid type: #{hash['Type']}" + end end - end - if hash['Default'] - unless hash['Default'].is_a?(String) - abort! "Parameter #{name} has an invalid default (Must be string): #{hash['Default']}" + if hash['Default'] + unless hash['Default'].is_a?(String) + abort! "Parameter #{name} has an invalid default (Must be string): #{hash['Default']}" + end end end + @logger.info ' Parameters validated' end - @logger.info ' Parameters validated' - # Mappings => Resources + # --- functions ---------------------------------------------------------------------------------------------- funs = find_fns(compiled) #.select { |a| !(a =~ /^AWS::/) } bad = [] funs.each do |fn| unless @valid_functions.include?(fn) @@ -51,36 +59,61 @@ if bad.size > 0 abort! "Encountered unsupported function(s) ...\n#{bad.ai}\nSupported functions are:\n#{@valid_functions.ai}" end @logger.info ' Functions validated' - # Mappings => Resources - maps = find_maps(compiled) #.select { |a| !(a =~ /^AWS::/) } + # --- Mappings ---------------------------------------------------------------------------------------------- + mappings = find_maps(compiled) mpgs = compiled['Mappings'].nil? ? [] : compiled['Mappings'].keys names = mpgs # rscs+ - unless (maps-names).empty? + maps = {} + mappings.each{|m| maps[m[:mapping]] = m[:source] } + mapnames = maps.keys + net = (mapnames-names) + unless net.empty? @logger.error '!!! Unknown mappings !!!' - (maps-names).each do |name| - @logger.error " #{name}" + net.each do |name| + @logger.error " #{name} (Location in template: #{maps[name].join('/')})" end abort! end - net = (names-maps) + net = (names-mapnames) unless net.empty? @logger.warn '!!! Unused mappings !!!' net.each do |name| @logger.warn " #{name}" end end @logger.info ' Mappings validated' + # --- Conditions ---------------------------------------------------------------------------------------------- + cond = find_conditions(compiled) + cnds = compiled['Conditions'].keys rescue [] + names = cnds + + net = (cond-names) + unless net.empty? + @logger.error '!!! Unknown conditions !!!' + net.each do |name| + @logger.error " #{name}" + end + abort! + end + net = (names-cond) + unless net.empty? + @logger.warn '!!! Unused conditions !!!' + net.each do |name| + @logger.warn " #{name}" + end + end + @logger.info ' Conditions validated' + + # --- References ---------------------------------------------------------------------------------------------- # Parameters => Resources => Outputs refs = find_refs(compiled).select { |a,_| !(a =~ /^AWS::/) } rscs = compiled['Resources'].keys - cnds = compiled['Conditions'].keys rescue [] - # outs = compiled['Outputs'].keys rescue [] names = rscs+prms+cnds net = (refs.keys-names) unless net.empty? @logger.error '!!! Unknown references !!!' @@ -94,13 +127,13 @@ @logger.warn '!!! Unused Parameters !!!' net.each do |name| @logger.warn " #{name}" end end - net = (rscs-refs.keys) + net = (rscs.sort-refs.keys.sort) unless net.empty? - @logger.info '!!! Unreported Resources !!!' + @logger.info '!!! Unreferenced Resources !!!' net.each do |name| @logger.info " #{name}" end end @logger.info ' References validated' @@ -188,10 +221,15 @@ case File.extname(File.basename(abs)).downcase when /json|jts/ @spec = JSON.parse(spec) when /yaml|yts/ - @spec = YAML.load(spec) + begin + @spec = YAML.load(spec) + rescue Psych::SyntaxError => e + i = 0 + abort! "Error in the template specification: #{e.message}\n#{spec.split(/\n/).map{|l| "#{i+=1}: #{l}"}.join("\n")}" + end else abort! "Unsupported file type for specification: #{spec}" end else abort! 'Unable to open specification'+ (abs.nil? ? " or {.,#{@config[:brick_path_list].join(',')}/}#{spec} not found" : ": #{abs}")