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_base64, #fn_join

Constructor Details

#initializeTemplateBuilder

Returns a new instance of TemplateBuilder.



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

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



88
89
90
# File 'lib/cloud_shaped/template_builder.rb', line 88

def description
  @description
end

#metadataObject (readonly)

Returns the value of attribute metadata.



89
90
91
# File 'lib/cloud_shaped/template_builder.rb', line 89

def 
  @metadata
end

Instance Method Details

#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)


56
57
58
59
# File 'lib/cloud_shaped/template_builder.rb', line 56

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)



84
85
86
# File 'lib/cloud_shaped/template_builder.rb', line 84

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

#def_parameter(name, 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



44
45
46
# File 'lib/cloud_shaped/template_builder.rb', line 44

def def_parameter(name, options = {})
  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



67
68
69
70
71
72
73
74
# File 'lib/cloud_shaped/template_builder.rb', line 67

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



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

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["Resources"] = resources
    template["Outputs"] = outputs unless outputs.empty?
  end
end