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

Instance Method Summary collapse

Instance Method Details

#identify_template_format(template_body) ⇒ Object



8
9
10
11
# File 'lib/stack_master/template_utils.rb', line 8

def identify_template_format(template_body)
  return :json if template_body =~ /^{/x # ignore leading whitespaces
  :yaml
end

#maybe_compressed_template_body(template_body) ⇒ Object



24
25
26
27
28
29
# File 'lib/stack_master/template_utils.rb', line 24

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



13
14
15
16
17
18
19
20
21
22
# File 'lib/stack_master/template_utils.rb', line 13

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