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.
-
#s3_bucket ⇒ Object
readonly
Returns the value of attribute s3_bucket.
-
#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
- #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.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 73 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) @s3_bucket = .fetch(:s3_bucket, nil) 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 |
#s3_bucket ⇒ Object (readonly)
Returns the value of attribute s3_bucket.
65 66 67 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 65 def s3_bucket @s3_bucket 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
177 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 177 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.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 103 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
153 154 155 156 157 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 153 def = @dict.fetch(:Tags, {}) @dict.delete(:Tags) end |
#exec ⇒ Object
722 723 724 |
# File 'lib/cloudformation-ruby-dsl/cfntemplate.rb', line 722 def exec cfn(self) end |
#exec! ⇒ Object
84 85 86 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 84 def exec!() cfn(self) end |
#find_in_map(map, key, name) ⇒ Object
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 183 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.
143 144 145 146 147 148 149 150 151 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 143 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 125 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
116 117 118 119 120 121 122 123 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 116 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
175 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 175 def (name, ) default(:Metadata, {})[name] = end |
#output(name, options) ⇒ Object
181 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 181 def output(name, ) default(:Outputs, {})[name] = end |
#parameter(name, options) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 88 def parameter(name, ) default(:Parameters, {})[name] = if @interactive @parameters[name] ||= Parameter.new(_get_parameter_from_cli(name, )) else @parameters[name] ||= Parameter.new('') end # set various param options @parameters[name].default = [:Default] @parameters[name].use_previous_value = [:UsePreviousValue] end |
#resource(name, options) ⇒ Object
179 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 179 def resource(name, ) default(:Resources, {})[name] = end |
#tag(tag, *args) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cloudformation-ruby-dsl/dsl.rb', line 159 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 |