Class: CloudFormula::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudformula/template.rb

Overview

Defines a CloudFormula/CloudFormation template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = '', parameters = {}) ⇒ Template

Returns a new instance of Template

Parameters:

  • source (String) (defaults to: '')

    Path to source template

  • parameters (Hash) (defaults to: {})

    Parameter names and values to use in the template


10
11
12
13
# File 'lib/cloudformula/template.rb', line 10

def initialize(source = '', parameters = {})
  @source = source
  @parameters = parameters
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters


6
7
8
# File 'lib/cloudformula/template.rb', line 6

def parameters
  @parameters
end

Instance Method Details

#aws_options(override_options = {}, parameters = {}) ⇒ Object

Returns the merged options Hash for use by AWS::CloudFormation::StackCollection#create and AWS::CloudFormation::Stack#update

Parameters:

  • override_options (Hash) (defaults to: {})

    Optional. Stack options to override. See README.md “CloudFormation stack options” for details

  • parameters (Hash) (defaults to: {})

    Parameters to supply to the template. Required if the template contains any required parameters without a default value


65
66
67
# File 'lib/cloudformula/template.rb', line 65

def aws_options(override_options = {}, parameters = {})
  stack_options.merge(override_options.merge({ :parameters => parameters }))
end

#errorsObject

Similar to Active Record Validations errors[] object


71
72
73
# File 'lib/cloudformula/template.rb', line 71

def errors
  validate
end

#generateString

Evaluates and returns the @source template populated with values from @parameters

Returns:


17
18
19
20
21
# File 'lib/cloudformula/template.rb', line 17

def generate
  init_parameters @parameters
  raise "Parameter validation failed.\n\nErrors:\n  #{errors.join("\n")}" if invalid?
  JsonErb.new(File.read(@source)).result(binding)
end

#generate_hashHash

Evalutes and returns the @source template populated with values from @parameters

Returns:

  • (Hash)

25
26
27
# File 'lib/cloudformula/template.rb', line 25

def generate_hash
  JSON.parse(generate)
end

#invalid?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/cloudformula/template.rb', line 79

def invalid?
  !valid?
end

#raw(value) ⇒ String

Causes the given string value to be output without escape characters

Parameters:

Returns:


32
33
34
35
# File 'lib/cloudformula/template.rb', line 32

def raw(value)
  value.instance_variable_set :@cloudformula_raw, true
  value
end

#render(source, parameters = {}) ⇒ String

Returns The result of generating the template

Parameters:

  • source (String)

    Path to source template

  • parameters (Hash) (defaults to: {})

    Parameter names and values to use in the template

Returns:

  • (String)

    The result of generating the template


40
41
42
43
44
45
46
# File 'lib/cloudformula/template.rb', line 40

def render(source, parameters = {})
  raw CloudFormula::Template.new(source, parameters).generate
rescue Errno::ENOENT
  # retry the path, but relative to its parent template
  relative_to_parent = Pathname.new(@source).parent + source
  raw CloudFormula::Template.new(relative_to_parent, parameters).generate
end

#stack_optionsObject

Returns the CloudFormation stack options defined by the template.


49
50
51
52
# File 'lib/cloudformula/template.rb', line 49

def stack_options
  init_parameters @parameters
  JsonErb.new(File.read(@source), 0).stack_options(binding) rescue {}
end

#valid?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/cloudformula/template.rb', line 75

def valid?
  errors.length == 0
end

#validationsObject

Returns the validations defined by the template.


55
56
57
58
# File 'lib/cloudformula/template.rb', line 55

def validations
  init_parameters @parameters
  JsonErb.new(File.read(@source), 0).validations(binding) rescue {}
end