Class: CloudShaped::TemplateBuilder

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/cloud_shaped/template_builder.rb

Overview

A builder for CloudFormation templates.

Constant Summary

Constants included from Interpolation

Interpolation::DEFAULT_DELIMITERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SnsMethods

#sns_topic

Methods included from CoreMethods

#output, #parameter, #ref, #resource, #tag, #tags

Methods included from Interpolation

#interpolate

Methods included from FunctionMethods

#fn_and, #fn_base64, #fn_equals, #fn_if, #fn_join, #fn_not, #fn_or

Constructor Details

#initializeTemplateBuilder

Returns a new instance of TemplateBuilder.



9
10
11
12
13
14
15
16
# File 'lib/cloud_shaped/template_builder.rb', line 9

def initialize
   = {}
  @parameters = {}
  @mappings = {}
  @conditions = {}
  @resources = {}
  @outputs = {}
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



103
104
105
# File 'lib/cloud_shaped/template_builder.rb', line 103

def description
  @description
end

#metadataObject (readonly)

Returns the value of attribute metadata.



104
105
106
# File 'lib/cloud_shaped/template_builder.rb', line 104

def 
  
end

Instance Method Details

#def_condition(name, body) ⇒ Object

Declares a Condition.

Examples:

def_mapping "inProd", fn_equals(ref("Env"), "prod")

Parameters:

  • name (String)

    the condition name

  • condition (Hash)

    the condition body



72
73
74
# File 'lib/cloud_shaped/template_builder.rb', line 72

def def_condition(name, body)
  conditions[name] = body
end

#def_mapping(name, mapping = {}) {|mapping| ... } ⇒ Object

Declares a Mappping.

Examples:

def_mapping "regionMap", "us-east-1" => { "32" => "ami-6411e20d" }

Parameters:

  • name (String)

    the mapping name

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

    the mapping

Yields:

  • (mapping)


59
60
61
62
# File 'lib/cloud_shaped/template_builder.rb', line 59

def def_mapping(name, mapping = {})
  yield mapping if block_given?
  mappings[name] = mapping
end

#def_output(name, value) ⇒ Object

Declares an Output.

Examples:

def_output "loadBalancerName", ref("loadBalancer")

Parameters:

  • name (String)

    the output name

  • value

    the output value (usually a reference to a resource)



99
100
101
# File 'lib/cloud_shaped/template_builder.rb', line 99

def def_output(name, value)
  outputs[name] = output(value)
end

#def_parameter(name, options = {}) {|options| ... } ⇒ Object

Declares a Parameter.

Examples:

def_parameter "appName"
def_parameter "minInstances", :type => "Number"

Parameters:

  • name (String)

    the parameter name

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

    a customizable set of options

Options Hash (options):

  • :type (String) — default: "String"

    the parameter type

  • :description (String)

    parameter description

  • :default (String)

    a default value

Yields:

  • (options)


46
47
48
49
# File 'lib/cloud_shaped/template_builder.rb', line 46

def def_parameter(name, options = {})
  yield options if block_given?
  parameters[name] = parameter(options)
end

#def_resource(name, type, *args, &block) ⇒ Object

Declares a Resource.

Parameters:

  • name (String)

    the resource name

  • type (String, Symbol)

    the resource type

  • args (Hash)

    resource properties



82
83
84
85
86
87
88
89
# File 'lib/cloud_shaped/template_builder.rb', line 82

def def_resource(name, type, *args, &block)
  definition = if type.is_a?(Symbol)
                 send(type, *args, &block)
               else
                 resource(type, *args, &block)
               end
  resources[name] = definition if definition
end

#templateHash

Returns a CloudFormation template as Ruby data.

Returns:

  • (Hash)

    a CloudFormation template as Ruby data



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloud_shaped/template_builder.rb', line 20

def template
  {}.tap do |template|
    template["AWSTemplateFormatVersion"] = "2010-09-09"
    template["Description"] = description if description
    template["Metadata"] =  unless .empty?
    template["Parameters"] = parameters unless parameters.empty?
    template["Mappings"] = mappings unless mappings.empty?
    template["Conditions"] = conditions unless conditions.empty?
    template["Resources"] = resources
    template["Outputs"] = outputs unless outputs.empty?
  end
end