Class: CloudFormationDSL::Template

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cloudformation-dsl/template.rb

Instance Method Summary collapse

Methods included from Helpers

#aws_account_id, #aws_no_value, #aws_notification_arns, #aws_stack_id, #aws_stack_name, #base64, #equal, #file, #find_in_map, #fn_and, #fn_if, #fn_not, #fn_or, #get_att, #get_azs, #interpolate, #join, #join_interpolate, #join_list, #load_from_file, #no_value, #not_equal, #ref, #select

Constructor Details

#initialize(filename = nil, &block) ⇒ Template

You can supply:

- filename
- block

The file located at the filename is evaluated The block will also be evaluated

If both filename and block are passed, then the filename is evaluated first before the block is evaluated.



14
15
16
17
18
# File 'lib/cloudformation-dsl/template.rb', line 14

def initialize(filename = nil, &block)
  @template_file = filename
  @template_block = block
  @dict = {}
end

Instance Method Details

#aws_regionObject



20
21
22
# File 'lib/cloudformation-dsl/template.rb', line 20

def aws_region
  ENV['EC2_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
end

#condition(name, options) ⇒ Object



56
# File 'lib/cloudformation-dsl/template.rb', line 56

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

#default(key, value) ⇒ Object



38
39
40
# File 'lib/cloudformation-dsl/template.rb', line 38

def default(key, value)
  @dict[key] ||= value
end

#evaluated_dataObject



24
25
26
27
28
29
30
31
32
# File 'lib/cloudformation-dsl/template.rb', line 24

def evaluated_data
  # Once evaluated, treat this as immutable. This allows us to lazy eval the template.
  return @dict unless @dict.empty?

  # See: http://stackoverflow.com/questions/4667158/ruby-instance-eval-a-file-while-maintaining-fileline-in-stacktrace
  instance_eval(File.read(@template_file), @template_file) if @template_file
  instance_eval(@template_block)                           if @template_block
  return @dict
end

#excise_parameter_attribute!(attribute) ⇒ Object

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



82
83
84
85
86
87
88
89
90
# File 'lib/cloudformation-dsl/template.rb', line 82

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



92
93
94
95
96
# File 'lib/cloudformation-dsl/template.rb', line 92

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

#mapping(name, options) ⇒ Object



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

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



60
# File 'lib/cloudformation-dsl/template.rb', line 60

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

#parameter(name, options) ⇒ Object

DSL for major sections



52
53
54
# File 'lib/cloudformation-dsl/template.rb', line 52

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


46
47
48
# File 'lib/cloudformation-dsl/template.rb', line 46

def print()
  puts JSON.pretty_generate(self)
end

#resource(name, options) ⇒ Object



58
# File 'lib/cloudformation-dsl/template.rb', line 58

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

#tag(tag) ⇒ Object



73
74
75
76
77
# File 'lib/cloudformation-dsl/template.rb', line 73

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

#to_json(*args) ⇒ Object



42
43
44
# File 'lib/cloudformation-dsl/template.rb', line 42

def to_json(*args)
  evaluated_data.to_json(*args)
end

#value(values) ⇒ Object



34
35
36
# File 'lib/cloudformation-dsl/template.rb', line 34

def value(values)
  @dict.update(values)
end