module AwsCftTools::Template::Properties

Simple properties of templates.

Public Instance Methods

allowed_environments() click to toggle source

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
allowed_environments_regex() click to toggle source
# 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
allowed_regions() click to toggle source

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
default_parameters() click to toggle source

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
environment?(value) click to toggle source
# 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
inputs() click to toggle source

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
outputs() click to toggle source

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
region?(region) click to toggle source
# File lib/aws_cft_tools/template/properties.rb, line 62
def region?(region)
  !region || allowed_regions.empty? || allowed_regions.include?(region)
end
role() click to toggle source

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
role?(value) click to toggle source
# File lib/aws_cft_tools/template/properties.rb, line 47
def role?(value)
  !value || role == value
end
template_dependencies() click to toggle source

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

output_name(name) click to toggle source
# 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
substitutions() click to toggle source
# 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
with_substitutions(string) click to toggle source

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