lib/aws/cfn/compiler/base.rb in aws-cfn-compiler-0.9.19 vs lib/aws/cfn/compiler/base.rb in aws-cfn-compiler-0.9.20
- old
+ new
@@ -16,20 +16,33 @@
def initialize
super
@items = {}
@dynamic_items = {}
+ @dynamic_references = {}
+ @all_sections.each do |section|
+ @dynamic_items[section] ||= {}
+ @dynamic_references[section] ||= []
+ end
end
def dynamic_item(section,resource,hash)
abort! "Invalid section '#{section}'\nValid sections are: #{@all_sections.join(',')}" unless @all_sections.include?(section)
- unless @dynamic_items.has_key?(section)
- @dynamic_items[section] ||= {}
- end
@dynamic_items[section][resource] = hash
end
+ def dynamic_reference(section,resource)
+ abort! "Invalid section '#{section}'\nValid sections are: #{@all_sections.join(',')}" unless @all_sections.include?(section)
+ @dynamic_references[section] << resource
+ end
+
+ def _uniq_names(arr)
+ names = {}
+ arr.map { |n| names[n] = nil }
+ names.keys
+ end
+
def validate(compiled)
abort! 'No Resources!?' unless compiled['Resources']
logStep 'Validating template'
# --- Parameters ----------------------------------------------------------------------------------------------
@@ -66,15 +79,15 @@
@logger.info ' Functions validated'
# --- Mappings ----------------------------------------------------------------------------------------------
mappings = find_maps(compiled)
mpgs = compiled['Mappings'].nil? ? [] : compiled['Mappings'].keys
- names = mpgs # rscs+
+ names = _uniq_names(mpgs+@dynamic_items['Mappings'].keys)
maps = {}
mappings.each{|m| maps[m[:mapping]] = m[:source] }
- mapnames = maps.keys
+ mapnames = maps.keys+@dynamic_references['Mappings']
net = (mapnames-names)
unless net.empty?
@logger.error '!!! Unknown mappings !!!'
net.each do |name|
@logger.error " #{name} (Location in template: #{maps[name].join('/')})"
@@ -89,13 +102,13 @@
end
end
@logger.info ' Mappings validated'
# --- Conditions ----------------------------------------------------------------------------------------------
- cond = find_conditions(compiled)
+ cond = find_conditions(compiled)+@dynamic_references['Conditions']
cnds = compiled['Conditions'].keys rescue []
- names = cnds
+ names = _uniq_names(cnds+@dynamic_items['Conditions'].keys)
net = (cond-names)
unless net.empty?
@logger.error '!!! Unknown conditions !!!'
net.each do |name|
@@ -114,30 +127,30 @@
# --- References ----------------------------------------------------------------------------------------------
# Parameters => Resources => Outputs
refs = find_refs(compiled).select { |a,_| !(a =~ /^AWS::/) }
rscs = compiled['Resources'].keys
- names = rscs+prms+cnds
+ names = _uniq_names((mpgs+rscs+prms+cnds)+@dynamic_items['Mappings'].keys+@dynamic_items['Conditions'].keys+@dynamic_items['Parameters'].keys+@dynamic_items['Resources'].keys)
- net = (refs.keys-names)
+ net = (_uniq_names(refs.keys+@dynamic_references['Mappings']+@dynamic_references['Parameters']+@dynamic_references['Resources'])-names)
unless net.empty?
@logger.error '!!! Unknown references !!!'
net.each do |name|
@logger.error " #{name} from #{refs[name][0]}:#{refs[name][1]}"
end
abort!
end
- net = (prms-refs.keys)
+ net = (prms+@dynamic_items['Parameters'].keys-refs.keys-@dynamic_references['Parameters'])
unless net.empty?
@logger.warn '!!! Unused Parameters !!!'
- net.each do |name|
+ net.sort.each do |name|
@logger.warn " #{name}"
end
end
- net = (rscs.sort-refs.keys.sort)
+ net = (rscs+@dynamic_items['Resources'].keys-refs.keys-@dynamic_references['Resources'])
unless net.empty?
@logger.info '!!! Unreferenced Resources !!!'
- net.each do |name|
+ net.sort.each do |name|
@logger.info " #{name}"
end
end
@logger.info ' References validated'
end