module AwsCftTools::Template::Metadata

Simple derived information about templates.

Constants

CONTENT_TYPES

Mapping of filename extensions to content types.

Public Instance Methods

name() click to toggle source

The name of the stack built by this template.

@return [String]

# File lib/aws_cft_tools/template/metadata.rb, line 41
def name
  @name ||= @options[:environment] + '-' +
            filename.to_s.sub(/\.(ya?ml|json|rb)$/, '').split(%r{/}).reverse.join('-')
end
parameters() click to toggle source

The parsed parameters for this template in the deployment environment.

@return [Hash]

# File lib/aws_cft_tools/template/metadata.rb, line 66
def parameters
  @parameters ||= parameters_for_filename_and_environment(parameters_source, @options[:environment])
end
template() click to toggle source

The parsed template as a Ruby data structure.

@return [Hash]

# File lib/aws_cft_tools/template/metadata.rb, line 50
def template
  @template ||= template_content_for_filename(template_file)
end
template?() click to toggle source

Queries if the template source looks like a CloudFormation template.

@return [Boolean]

# File lib/aws_cft_tools/template/metadata.rb, line 33
def template?
  template && template['AWSTemplateFormatVersion']
end
template_source_for_aws() click to toggle source

The JSON or YAML source that can be submitted to AWS to build the stack.

@return [String]

# File lib/aws_cft_tools/template/metadata.rb, line 58
def template_source_for_aws
  template_type == 'dsl' ? JSON.pretty_generate(template) : template_source
end
template_type() click to toggle source

The type of template content.

@return [String] One of dsl, json, or yaml.

# File lib/aws_cft_tools/template/metadata.rb, line 25
def template_type
  content_type(template_file)
end

Private Instance Methods

aws_env() click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 140
def aws_env
  region = @options[:region]
  {
    'AWS_REGION' => region,
    'EC2_REGION' => region,
    'AWS_PROFILE' => @options[:profile]
  }
end
content_type(file) click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 72
def content_type(file)
  CONTENT_TYPES[file.extname] if file
end
parameters_for_filename_and_environment(param_source, env) click to toggle source

Loads the contents of the full path and passes it through ERB before parsing as YAML. Returns a Ruby structure.

If no file exists, then a simple hash with the Environment key set.

# File lib/aws_cft_tools/template/metadata.rb, line 106
def parameters_for_filename_and_environment(param_source, env)
  parameters_for_filename_and_environment!(param_source, env)
rescue AwsCftTools::ToolingException
  raise
rescue => exception
  raise AwsCftTools::ParseException, "Error while reading and parsing #{parameter_file}: #{exception}"
end
parameters_for_filename_and_environment!(param_source, env) click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 114
def parameters_for_filename_and_environment!(param_source, env)
  return { Environment: env } unless param_source

  params_for_all = YAML.safe_load(process_erb_file(param_source), [], [], true)
  return params_for_all[env].update('Environment' => env) if params_for_all.key?(env)

  # now check for regex match on keys
  params_for_all.each do |env_name, params|
    return params.update('Environment' => env) if Regexp.compile("\\A#{env_name}\\Z").match?(env)
  end
  { 'Environment' => env }
end
process_erb_file(content) click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 127
def process_erb_file(content)
  with_environment { ERB.new(content).result }
end
template_content_for_dsl!() click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 96
def template_content_for_dsl!
  with_environment { JSON.parse(DSLContext.module_eval(template_source).to_json) }
end
template_content_for_filename(file) click to toggle source

Loads the contents of the full path and returns the content as a Ruby data structure

@return [String]

# File lib/aws_cft_tools/template/metadata.rb, line 80
def template_content_for_filename(file)
  type = content_type(file)
  return {} unless type
  send(:"template_content_for_#{type}!")
rescue => exception
  raise AwsCftTools::ParseException, "Error while parsing #{template_file}: #{exception}"
end
template_content_for_json!() click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 92
def template_content_for_json!
  JSON.parse(template_source) || {}
end
template_content_for_yaml!() click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 88
def template_content_for_yaml!
  YAML.safe_load(template_source, [Date], [], true) || {}
end
with_environment() { || ... } click to toggle source
# File lib/aws_cft_tools/template/metadata.rb, line 131
def with_environment
  return unless block_given?
  prior = ENV.to_h
  ENV.update(aws_env)
  yield
ensure
  ENV.update(prior)
end