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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sfn/command/validate.rb', line 12

def execute!
  if config[:all]
    validate_templates = sparkle_collection.templates[sparkle_collection.provider].keys
  elsif config[:group]
    validate_templates = sparkle_collection.templates[sparkle_collection.provider].keys.select do |template|
      template.split("__").first == config[:group]
    end
  else
    validate_templates = [config[:file]]
  end

  if validate_templates.empty?
    load_template_file
    validate_templates.push(config[:file])
  end

  validate_templates.each do |template|
    config[:file] = template
    print_only_original = config[:print_only]
    config[:print_only] = true
    ui.info "#{ui.color("Template Validation (#{provider.connection.provider}): ", :bold)} #{config[:file].sub(Dir.pwd, "").sub(%r{^/}, "")}"
    file = load_template_file
    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
end

#validate_stack(template, name) ⇒ TrueClass

Validate template with remote API and unpack nested templates if required

Parameters:

  • template (Hash)

    template data structure

  • name (String)

    name of template

Returns:

  • (TrueClass)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sfn/command/validate.rb', line 57

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
  # Clear Compile Time Parameters from Config
  config[:compile_parameters] = {}
end