lib/aws/cfn/compiler.rb in aws-cfn-compiler-0.1.0 vs lib/aws/cfn/compiler.rb in aws-cfn-compiler-0.2.1

- old
+ new

@@ -40,17 +40,18 @@ end vers = '2010-09-09' if @spec and @spec['AWSTemplateFormatVersion'] vers = @spec['AWSTemplateFormatVersion'] end + # noinspection RubyStringKeysInHashInspection compiled = { - AWSTemplateFormatVersion: (@opts[:formatversion].nil? ? vers : @opts[:formatversion]), - Description: (@opts[:description].nil? ? desc : @opts[:description]), - Parameters: @items['params'], - Mappings: @items['mappings'], - Resources: @items['resources'], - Outputs: @items['outputs'], + 'AWSTemplateFormatVersion' => (@opts[:formatversion].nil? ? vers : @opts[:formatversion]), + 'Description' => (@opts[:description].nil? ? desc : @opts[:description]), + 'Parameters' => @items['Parameters'], + 'Mappings' => @items['Mappings'], + 'Resources' => @items['Resources'], + 'Outputs' => @items['Outputs'], } puts puts 'Validating compiled file...' @@ -64,29 +65,49 @@ puts puts '*** Compiled Successfully ***' end def validate(compiled) - raise 'No resources!?' unless compiled[:Resources] - raise 'No parameters!?' unless compiled[:Parameters] - names = compiled[:Resources].keys + compiled[:Parameters].keys + raise 'No Resources!?' unless compiled['Resources'] + #raise 'No Parameters!?' unless compiled['Parameters'] + names = compiled['Resources'].keys + (compiled['Parameters'].nil? ? [] : compiled['Parameters'].keys) refs = find_refs(compiled).select { |a| !(a =~ /^AWS::/) } unless (refs-names).empty? puts '!!! Unknown references !!!' (refs-names).each do |name| puts " #{name}" end abort! end puts ' References validated' + names = compiled['Resources'].keys + (compiled['Mappings'].nil? ? [] : compiled['Mappings'].keys) + maps = find_maps(compiled) #.select { |a| !(a =~ /^AWS::/) } + + unless (maps-names).empty? + puts '!!! Unknown mappings !!!' + (maps-names).each do |name| + puts " #{name}" + end + abort! + end + puts ' References validated' end def save(compiled, output_file) begin + hash = {} + compiled.each do |item,value| + unless value.nil? + if (not value.is_a?(Hash)) or (value.count > 0) + hash[item] = value + end + end + end + File.open output_file, 'w' do |f| - f.write JSON.pretty_generate(compiled) + f.write JSON.pretty_generate(hash) end puts ' Compiled file written.' rescue puts "!!! Could not write compiled file: #{$!}" abort! @@ -109,23 +130,23 @@ puts "Loading specification #{abs}..." spec = File.read(abs) case File.extname(File.basename(abs)).downcase when /json/ - spec = JSON.parse(spec) + @spec = JSON.parse(spec) when /yaml/ - spec = YAML.load(spec) + @spec = YAML.load(spec) else raise "Unsupported file type for specification: #{spec}" end - @spec = spec + # @spec = spec else raise "Unable to open specification: #{abs}" end end - %w{Params Mappings Resources Outputs}.each do |dir| - load_dir(dir,spec) + %w( Mappings Parameters Resources Outputs ).each do |dir| + load_dir(dir,@spec) end end protected @@ -149,10 +170,25 @@ elsif hash.is_a? Array hash.collect{|a| find_refs(a)}.flatten.compact.uniq end end + def find_maps(hash) + if hash.is_a? Hash + tr = [] + hash.keys.collect do |key| + if 'Fn::FindInMap' == key + hash[key].first + else + find_maps(hash[key]) + end + end.flatten.compact.uniq + elsif hash.is_a? Array + hash.collect{|a| find_maps(a)}.flatten.compact.uniq + end + end + def load_dir(dir,spec=nil) puts "Loading #{dir}..." raise "No such directory: #{@opts[:directory]}" unless File.directory?(@opts[:directory]) set = [] if File.directory?(File.join(@opts[:directory], dir)) @@ -164,12 +200,13 @@ set = get_file_set(dir.downcase) end end set.collect do |filename| next unless filename =~ /\.(json|ya?ml)\z/i - if spec and spec[dir] + if spec and spec.has_key?(dir) base = File.basename(filename).gsub(%r/\.(rb|yaml)/, '') + next if spec[dir].nil? # Edge case ... explicitly want NONE of these! next unless spec[dir].include?(base) puts "\tUsing #{dir}/#{base}" end begin puts " reading #{filename}" @@ -188,13 +225,16 @@ rescue puts " !! error: #{$!}" abort! end end + if spec and spec[dir] + raise "Suspect that a #{dir} item was missed! \nRequested: #{spec[dir]}\n Found: #{@items[dir].keys}" unless (@items[dir].keys.count == spec[dir].count) + end end def get_file_set(dir) - Dir[File.join(@opts[:directory], "#{dir}.*")] | Dir[File.join(@opts[:directory], dir, "**", "*")] + Dir[File.join(@opts[:directory], "#{dir}.*")] | Dir[File.join(@opts[:directory], dir.to_s, "**", "*")] end end end end