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::DEFAULT_PROVIDER_NAME, Sfn::CommandModule::Template::MAX_PARAMETER_ATTEMPTS, Sfn::CommandModule::Template::TEMPLATE_IGNORE_DIRECTORIES

Constants inherited from Sfn::Command

CONFIG_BASE_NAME, VALID_CONFIG_EXTENSIONS

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



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

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

  raw_template = _format_json(parameter_scrub!(template_content(file)))

  if config[:print_only]
    ui.puts raw_template
  else
    validate_stack(
      file.respond_to?(:dump) ? file.dump : file,
      if config[:processing]
        sparkle_collection.get(:template, config[:file])[:name]
      else
        config[:file]
      end
    )
  end
end

#validate_stack(template, name) ⇒ TrueClass

Validate template with remote API and unpack nested templates if required



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

def validate_stack(template, name)
  resources = template.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)}"
    if config[:upload_root_template]
      upload_result = store_template("validation-stack", template, Smash.new)
      stack = provider.connection.stacks.build(
        :name => "validation-stack",
        :template_url => upload_result[:url],
      )
    else
      stack = provider.connection.stacks.build(
        :name => "validation-stack",
        :template => parameter_scrub!(template),
      )
    end
    result = api_action!(:api_stack => stack) do
      stack.validate
    end
    ui.info ui.color("  -> VALID", :bold, :green)
    true
  rescue => e
    ui.info ui.color("  -> INVALID", :bold, :red)
    ui.fatal e.message
    raise e
  end
end