Class: Sfn::Command::Validate

Inherits:
Sfn::Command
  • Object
show all
Includes:
Sfn::CommandModule::Base, Sfn::CommandModule::Stack, Sfn::CommandModule::Template
Defined in:
lib/sfn/command/validate.rb

Overview

Validate command

Constant Summary

Constants included from Sfn::CommandModule::Template

Sfn::CommandModule::Template::MAX_PARAMETER_ATTEMPTS, Sfn::CommandModule::Template::TEMPLATE_IGNORE_DIRECTORIES

Instance Method Summary collapse

Methods included from Sfn::CommandModule::Stack

included

Methods included from Sfn::CommandModule::Template

included

Methods included from Sfn::CommandModule::Base

included

Methods inherited from Sfn::Command

#config, #initialize

Methods included from Sfn::CommandModule::Callbacks

#api_action!, #callbacks_for, #run_callbacks_for

Constructor Details

This class inherits a constructor from Sfn::Command

Instance Method Details

#execute!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sfn/command/validate.rb', line 13

def execute!
  print_only_original = config[:print_only]
  config[:print_only] = true
  file = load_template_file
  file.delete('sfn_nested_stack')
  ui.info "#{ui.color("Template Validation (#{provider.connection.provider}): ", :bold)} #{config[:file].sub(Dir.pwd, '').sub(%r{^/}, '')}"
  file = Sfn::Utils::StackParameterScrubber.scrub!(file)
  file = translate_template(file)
  config[:print_only] = print_only_original

  if(config[:print_only])
    ui.puts _format_json(file)
  else
    validate_stack(file, sparkle_collection.get(:template, config[:file])[:name])
  end
end

#validate_stack(stack, name) ⇒ Object



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
# File 'lib/sfn/command/validate.rb', line 30

def validate_stack(stack, name)
  resources = stack.fetch('Resources', {})
  nested_stacks = resources.find_all do |r_name, r_value|
    r_value.is_a?(Hash) &&
      provider.connection.data[:stack_types].include?(r_value['Type'])
  end
  nested_stacks.each do |n_name, n_resource|
    validate_stack(n_resource.fetch('Properties', {}).fetch('Stack', {}), "#{name} > #{n_name}")
    n_resource['Properties'].delete('Stack')
  end
  begin
    ui.info "Validating: #{ui.color(name, :bold)}"
    stack = provider.connection.stacks.build(
      :name => 'validation-stack',
      :template => Sfn::Utils::StackParameterScrubber.scrub!(stack)
    )
    result = api_action!(:api_stack => stack) do
      stack.validate
    end
    ui.info ui.color('  -> VALID', :bold, :green)
  rescue => e
    ui.info ui.color('  -> INVALID', :bold, :red)
    ui.fatal e.message
    raise e
  end
end