Class: TemplateDSL

Inherits:
JsonObjectDSL show all
Defined in:
lib/cloudformation-ruby-dsl/dsl.rb,
lib/cloudformation-ruby-dsl/cfntemplate.rb

Overview

Additional dsl logic Core interpreter for the DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from JsonObjectDSL

#default, #print, #to_json, #value

Constructor Details

#initialize(parameters = {}, stack_name = nil, aws_region = default_region, nopretty = false) ⇒ TemplateDSL

Returns a new instance of TemplateDSL.



59
60
61
62
63
64
65
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 59

def initialize(parameters = {}, stack_name = nil, aws_region = default_region, nopretty = false)
  @parameters = parameters
  @stack_name = stack_name
  @aws_region = aws_region
  @nopretty = nopretty
  super()
end

Instance Attribute Details

#aws_regionObject (readonly)

Returns the value of attribute aws_region.



57
58
59
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 57

def aws_region
  @aws_region
end

#noprettyObject (readonly)

Returns the value of attribute nopretty.



57
58
59
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 57

def nopretty
  @nopretty
end

#parametersObject (readonly)

Returns the value of attribute parameters.



57
58
59
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 57

def parameters
  @parameters
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



57
58
59
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 57

def stack_name
  @stack_name
end

Instance Method Details

#condition(name, options) ⇒ Object



125
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 125

def condition(name, options) default(:Conditions, {})[name] = options end

#excise_parameter_attribute!(attribute) ⇒ Object

Find parameters where the specified attribute is true then remove the attribute from the cfn template.



77
78
79
80
81
82
83
84
85
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 77

def excise_parameter_attribute!(attribute)
  marked_parameters = []
  @dict.fetch(:Parameters, {}).each do |param, options|
    if options.delete(attribute.to_sym) or options.delete(attribute.to_s)
      marked_parameters << param
    end
  end
  marked_parameters
end

#excise_tags!Object



113
114
115
116
117
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 113

def excise_tags!
  tags = @dict.fetch(:Tags, {})
  @dict.delete(:Tags)
  tags
end

#exec!Object



67
68
69
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 67

def exec!()
  cfn(self)
end

#find_in_map(map, key, name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 131

def find_in_map(map, key, name)
  # Eagerly evaluate mappings when all keys are known at template expansion time
  if map.is_a?(String) && key.is_a?(String) && name.is_a?(String)
    # We don't know whether the map was built with string keys or symbol keys.  Try both.
    def get(map, key) map[key] || map.fetch(key.to_sym) end
    get(get(@dict.fetch(:Mappings).fetch(map), key), name)
  else
    { :'Fn::FindInMap' => [ map, key, name ] }
  end
end

#load_from_file(filename) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 96

def load_from_file(filename)
  file = File.open(filename)

  begin
    # Figure out what the file extension is and process accordingly.
    contents = case File.extname(filename)
      when ".rb"; eval(file.read, nil, filename)
      when ".json"; JSON.load(file)
      when ".yaml"; YAML::load(file)
      else; raise("Do not recognize extension of #{filename}.")
    end
  ensure
    file.close
  end
  contents
end

#mapping(name, options) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 87

def mapping(name, options)
  # if options is a string and a valid file then the script will process the external file.
  default(:Mappings, {})[name] = \
    if options.is_a?(Hash); options
    elsif options.is_a?(String); load_from_file(options)['Mappings'][name]
    else; raise("Options for mapping #{name} is neither a string or a hash.  Error!")
  end
end

#output(name, options) ⇒ Object



129
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 129

def output(name, options) default(:Outputs, {})[name] = options end

#parameter(name, options) ⇒ Object



71
72
73
74
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 71

def parameter(name, options)
  default(:Parameters, {})[name] = options
  @parameters[name] ||= options[:Default]
end

#resource(name, options) ⇒ Object



127
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 127

def resource(name, options) default(:Resources, {})[name] = options end

#tag(tag) ⇒ Object



119
120
121
122
123
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 119

def tag(tag)
  tag.each do | name, value |
    default(:Tags, {})[name] = value
  end
end