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(options) ⇒ TemplateDSL

Returns a new instance of TemplateDSL.



72
73
74
75
76
77
78
79
80
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 72

def initialize(options)
  @parameters  = options.fetch(:parameters, {})
  @interactive = options.fetch(:interactive, false)
  @stack_name  = options[:stack_name]
  @aws_region  = options.fetch(:region, default_region)
  @aws_profile = options[:profile]
  @nopretty    = options.fetch(:nopretty, false)
  super()
end

Instance Attribute Details

#aws_profileObject (readonly)

Returns the value of attribute aws_profile.



65
66
67
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65

def aws_profile
  @aws_profile
end

#aws_regionObject (readonly)

Returns the value of attribute aws_region.



65
66
67
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65

def aws_region
  @aws_region
end

#noprettyObject (readonly)

Returns the value of attribute nopretty.



65
66
67
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65

def nopretty
  @nopretty
end

#parameter_cliObject (readonly)

Returns the value of attribute parameter_cli.



65
66
67
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65

def parameter_cli
  @parameter_cli
end

#parametersObject (readonly)

Returns the value of attribute parameters.



65
66
67
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65

def parameters
  @parameters
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



65
66
67
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65

def stack_name
  @stack_name
end

Instance Method Details

#condition(name, options) ⇒ Object



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

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

#excise_parameter_attributes!(attributes) ⇒ Object

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



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

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

#excise_tags!Object



149
150
151
152
153
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 149

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

#exec!Object



82
83
84
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 82

def exec!()
  cfn(self)
end

#find_in_map(map, key, name) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 179

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.



139
140
141
142
143
144
145
146
147
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 139

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 121

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



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

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

#metadata(name, options) ⇒ Object



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

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

#output(name, options) ⇒ Object



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

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

#parameter(name, options) ⇒ Object



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

def parameter(name, options)
  default(:Parameters, {})[name] = options

  if @interactive
    @parameters[name] ||= _get_parameter_from_cli(name, options)
  else
    @parameters[name] ||= Parameter.new('')
    @parameters[name].default = options[:Default]
    @parameters[name].use_previous_value = options[:UsePreviousValue]
  end
end

#resource(name, options) ⇒ Object



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

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

#tag(tag, *args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 155

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