module AwsCftTools::Template::Properties
Simple properties of templates.
Public Instance Methods
Returns the list of environments allowed for the Environment
parameter.
@return [Array<String>]
# File lib/aws_cft_tools/template/properties.rb, line 14 def allowed_environments template.dig('Parameters', 'Environment', 'AllowedValues') || [] end
# File lib/aws_cft_tools/template/properties.rb, line 18 def allowed_environments_regex source = template.dig('Parameters', 'Environment', 'AllowedPattern') Regexp.compile(source) if source end
Returns the list of regions in which the template is allowed.
@note The region in which a template is deployed is available as the AWS::Region
pseudo-parameter.
@return [Array<String>]
# File lib/aws_cft_tools/template/properties.rb, line 58 def allowed_regions template.dig('Metadata', 'Region') || [] end
Returns the parameter defaults for the template.
# File lib/aws_cft_tools/template/properties.rb, line 32 def default_parameters (template['Parameters'] || []).each_with_object({}) do |param, hash| hash[param.first] = param.last['Default'] end end
# File lib/aws_cft_tools/template/properties.rb, line 23 def environment?(value) return allowed_environments.include?(value) if allowed_environments.any? regex = allowed_environments_regex return regex.match?(value) if regex end
lists the imports expected by the template
Note that this does substitutions of any references to template parameters.
@return [Array<String>]
# File lib/aws_cft_tools/template/properties.rb, line 95 def inputs (template['Resources'] || {}) .values .flat_map { |resource| pull_imports(resource['Properties'] || {}) } .uniq .map(&method(:with_substitutions)) end
lists the exported values from the template
Note that this does substitutions of any references to template parameters.
@return [Array<String>]
# File lib/aws_cft_tools/template/properties.rb, line 82 def outputs (template['Outputs'] || []).map do |_, output| with_substitutions(output_name(output.dig('Export', 'Name'))) end end
# File lib/aws_cft_tools/template/properties.rb, line 62 def region?(region) !region || allowed_regions.empty? || allowed_regions.include?(region) end
Returns the role of the template as specified in the template metadata.
@return [String]
# File lib/aws_cft_tools/template/properties.rb, line 43 def role template.dig('Metadata', 'Role') end
# File lib/aws_cft_tools/template/properties.rb, line 47 def role?(value) !value || role == value end
Returns any templates on which this template has an explicit dependency.
These explicit dependencies are combined with any dependencies implied by imported values.
# File lib/aws_cft_tools/template/properties.rb, line 71 def template_dependencies template.dig('Metadata', 'DependsOn', 'Templates') || [] end
Private Instance Methods
# File lib/aws_cft_tools/template/properties.rb, line 105 def output_name(name) if name.is_a?(Hash) name['Sub'] || name['Fn::Sub'] else name end end
# File lib/aws_cft_tools/template/properties.rb, line 113 def substitutions @substitutions ||= begin defaults = Hash.new { |hash, key| hash[key] = "${#{key}}" } # need to get the default values of parameters from the template and populate those [default_parameters, parameters].flat_map(&:to_a).each do |key, value| defaults["${#{key}}"] = value end defaults end end
expands ${param} when ${param} is defined in the parameters but only works with “${param}” and not [ “${param}”, {param: value}] only substitutes when a value is provided as a parameter - otherwise, leaves it unsubsituted
# File lib/aws_cft_tools/template/properties.rb, line 129 def with_substitutions(string) return string if string.is_a?(Array) string.gsub(/(\${[^}]*})/, substitutions) end