Module: StackMaster::TemplateUtils

Extended by:
TemplateUtils
Included in:
TemplateUtils
Defined in:
lib/stack_master/template_utils.rb

Constant Summary collapse

MAX_TEMPLATE_SIZE =
51200
MAX_S3_TEMPLATE_SIZE =
460800
JSON_IDENTIFICATION_PATTERN =

Matches if the first non-whitespace character is a ‘{’, handling cases with leading whitespace and extra (whitespace-only) lines.

Regexp.new('\A\s*{', Regexp::MULTILINE)

Instance Method Summary collapse

Instance Method Details

#identify_template_format(template_body) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/stack_master/template_utils.rb', line 11

def identify_template_format(template_body)
  if template_body =~ JSON_IDENTIFICATION_PATTERN
    :json
  else
    :yaml
  end
end

#maybe_compressed_template_body(template_body) ⇒ Object



30
31
32
33
34
35
# File 'lib/stack_master/template_utils.rb', line 30

def maybe_compressed_template_body(template_body)
  # Do not compress the template if it's not JSON because parsing YAML as a hash ignores
  # CloudFormation-specific tags such as !Ref
  return template_body if template_body.size <= MAX_TEMPLATE_SIZE || identify_template_format(template_body) != :json
  JSON.dump(template_hash(template_body))
end

#template_hash(template_body = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/stack_master/template_utils.rb', line 19

def template_hash(template_body=nil)
  return unless template_body
  template_format = identify_template_format(template_body)
  case template_format
  when :json
    JSON.parse(template_body)
  when :yaml
    YAML.load(template_body)
  end
end