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, aws_profile = nil, nopretty = false) ⇒ TemplateDSL

Returns a new instance of TemplateDSL.



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

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

Instance Attribute Details

#aws_profileObject (readonly)

Returns the value of attribute aws_profile.



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

def aws_profile
  @aws_profile
end

#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



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

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.



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

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



125
126
127
128
129
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 125

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

#exec!Object



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

def exec!()
  cfn(self)
end

#find_in_map(map, key, name) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 153

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

#get_tag_attribute(tags, attribute) ⇒ Object

Find tags where the specified attribute is true then remove this attribute.



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

def get_tag_attribute(tags, attribute)
  marked_tags = []
  tags.each do |tag, options|
    if options.delete(attribute.to_sym) or options.delete(attribute.to_s)
      marked_tags << tag
    end
  end
  marked_tags
end

#load_from_file(filename) ⇒ Object



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

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



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

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



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

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

#parameter(name, options) ⇒ Object



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

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

#resource(name, options) ⇒ Object



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

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

#tag(tag, *args) ⇒ Object



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

def tag(tag, *args)
  if (tag.is_a?(String) || tag.is_a?(Symbol)) && !args.empty?
    default(:Tags, {})[tag.to_s] = args[0]
  # For backward-compatibility, transform `tag_name=>value` format to `tag_name, :Value=>value, :Immutable=>true`
  # Tags declared this way remain immutable and won't be updated.
  elsif tag.is_a?(Hash) && tag.size == 1 && args.empty?
    $stderr.puts "WARNING: #{tag} tag declaration format is deprecated and will be removed in a future version. Please use resource-like style instead."
    tag.each do |name, value|
      default(:Tags, {})[name.to_s] = {:Value => value, :Immutable => true}
    end
  else
    $stderr.puts "Error: #{tag} tag validation error. Please verify tag's declaration format."
    exit(false)
  end
end