Class: TemplateDSL
- Inherits:
-
JsonObjectDSL
- Object
- JsonObjectDSL
- TemplateDSL
- 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
-
#aws_profile ⇒ Object
readonly
Returns the value of attribute aws_profile.
-
#aws_region ⇒ Object
readonly
Returns the value of attribute aws_region.
-
#nopretty ⇒ Object
readonly
Returns the value of attribute nopretty.
-
#parameter_cli ⇒ Object
readonly
Returns the value of attribute parameter_cli.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#stack_name ⇒ Object
readonly
Returns the value of attribute stack_name.
Instance Method Summary collapse
- #condition(name, options) ⇒ Object
-
#excise_parameter_attributes!(attributes) ⇒ Object
Find parameters where the specified attribute is true then remove the attribute from the cfn template.
- #excise_tags! ⇒ Object
- #exec! ⇒ Object
- #find_in_map(map, key, name) ⇒ Object
-
#get_tag_attribute(tags, attribute) ⇒ Object
Find tags where the specified attribute is true then remove this attribute.
-
#initialize(options) ⇒ TemplateDSL
constructor
A new instance of TemplateDSL.
- #load_from_file(filename) ⇒ Object
- #mapping(name, options) ⇒ Object
- #metadata(name, options) ⇒ Object
- #output(name, options) ⇒ Object
- #parameter(name, options) ⇒ Object
- #resource(name, options) ⇒ Object
- #tag(tag, *args) ⇒ Object
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() @parameters = .fetch(:parameters, {}) @interactive = .fetch(:interactive, false) @stack_name = [:stack_name] @aws_region = .fetch(:region, default_region) @aws_profile = [:profile] @nopretty = .fetch(:nopretty, false) super() end |
Instance Attribute Details
#aws_profile ⇒ Object (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_region ⇒ Object (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 |
#nopretty ⇒ Object (readonly)
Returns the value of attribute nopretty.
65 66 67 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65 def nopretty @nopretty end |
#parameter_cli ⇒ Object (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 |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
65 66 67 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65 def parameters @parameters end |
#stack_name ⇒ Object (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, ) default(:Conditions, {})[name] = 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, | attributes.each do |attribute| marked_parameters[attribute] ||= [] if .delete(attribute.to_sym) or .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 = @dict.fetch(:Tags, {}) @dict.delete(: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(, attribute) = [] .each do |tag, | if .delete(attribute.to_sym) or .delete(attribute.to_s) << tag end end 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, ) # if options is a string and a valid file then the script will process the external file. default(:Mappings, {})[name] = \ if .is_a?(Hash); elsif .is_a?(String); load_from_file()['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, ) default(:Metadata, {})[name] = end |
#output(name, options) ⇒ Object
177 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 177 def output(name, ) default(:Outputs, {})[name] = 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, ) default(:Parameters, {})[name] = if @interactive @parameters[name] ||= _get_parameter_from_cli(name, ) else @parameters[name] ||= Parameter.new('') @parameters[name].default = [:Default] @parameters[name].use_previous_value = [:UsePreviousValue] end end |
#resource(name, options) ⇒ Object
175 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 175 def resource(name, ) default(:Resources, {})[name] = 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 |