Class: Skywriter::Template

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

Overview

CloudFormation Template

Represents a CloudFormation template.

Examples:

parameters = {"foo" => "bar"}
webserver = Skywriter::Resource::EC2::Instance.new('MyWebserver')
load_balancer = Skywriter::Resource::ElasticLoadBalancing::LoadBalancer.new('MyLoadBalancer')

template = Skywriter::Template.new(
  description: "My Template",
  parameters: parameters,
  resources: [webserver, load_balancer]
)

AWS::CloudFormation.new.stacks.create('my-stack', template.to_json)

Defined Under Namespace

Classes: MergeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Template

Constructor

See the AWS Documentation for details on the meaning of these parameters: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/concept-template.html

Parameters:

  • format_version (String)

    The AWS CloudFormation template version

  • description (String)

    The stack description

  • parameters (Hash)

    A hash of parameters

  • mappings (Hash)

    A hash of mappings

  • conditions (Hash)

    A hash of condition

  • resources (Hash, Array<Skywriter::Resource>)

    Either a hash of resources or an array of objects whose classwhich include Skywriter::Resource, for example Skywriter::Resource::EC2::Instance.

  • outputs (Hash)

    A hash of outputs



39
40
41
42
43
44
45
46
47
48
# File 'lib/skywriter/template.rb', line 39

def initialize(options = {})
  @format_version   = (options[:format_version] || '2010-09-09').freeze
  @description      = options[:description].freeze

  @parameters       = (access_liberally(options, :parameters) || {}).freeze
  @mappings         = (access_liberally(options, :mappings) || {}).freeze
  @conditions       = (access_liberally(options, :conditions) || {}).freeze
  @resources        = resources_as_json(access_liberally(options, :resources)).freeze
  @outputs          = (access_liberally(options, :outputs) || {}).freeze
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



23
24
25
# File 'lib/skywriter/template.rb', line 23

def conditions
  @conditions
end

#descriptionObject (readonly)

Returns the value of attribute description.



22
23
24
# File 'lib/skywriter/template.rb', line 22

def description
  @description
end

#format_versionObject (readonly)

Returns the value of attribute format_version.



22
23
24
# File 'lib/skywriter/template.rb', line 22

def format_version
  @format_version
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



23
24
25
# File 'lib/skywriter/template.rb', line 23

def mappings
  @mappings
end

#outputsObject (readonly)

Returns the value of attribute outputs.



23
24
25
# File 'lib/skywriter/template.rb', line 23

def outputs
  @outputs
end

#parametersObject (readonly)

Returns the value of attribute parameters.



23
24
25
# File 'lib/skywriter/template.rb', line 23

def parameters
  @parameters
end

#resourcesObject (readonly)

Returns the value of attribute resources.



23
24
25
# File 'lib/skywriter/template.rb', line 23

def resources
  @resources
end

Instance Method Details

#as_jsonHash

Returns a hash representing the Template

Returns:

  • (Hash)

    A JSON-able hash



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/skywriter/template.rb', line 62

def as_json(*)
  {
    'FormatVersion' => format_version,
    'Description'   => description,
    'Parameters'    => parameters,
    'Mappings'      => mappings,
    'Conditions'    => conditions,
    'Resources'     => resources,
    'Outputs'       => outputs,
  }.reject { |key, value| value.nil? }
end

#merge(other) ⇒ Object



74
75
76
# File 'lib/skywriter/template.rb', line 74

def merge(other)
  dup.merge!(other)
end

#to_jsonString

Returns a JSON string representing the Template

Returns:

  • (String)

    A JSON string



54
55
56
# File 'lib/skywriter/template.rb', line 54

def to_json(*)
  JSON.pretty_generate(as_json)
end