Class: Stacker::Stack::Template

Inherits:
Component show all
Extended by:
Memoist
Defined in:
lib/stacker/stack/template.rb

Defined Under Namespace

Classes: JSONFormatter

Constant Summary collapse

FORMAT_VERSION =
'2010-09-09'

Instance Attribute Summary

Attributes inherited from Component

#stack

Instance Method Summary collapse

Methods inherited from Component

#initialize

Constructor Details

This class inherits a constructor from Stacker::Stack::Component

Instance Method Details

#diff(*args) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/stacker/stack/template.rb', line 58

def diff *args
  if json?
    Differ.json_diff local, remote, *args
  else
    Differ.diff local_raw, remote_raw, *args
  end
end

#dumpObject



71
72
73
# File 'lib/stacker/stack/template.rb', line 71

def dump
  write remote
end

#exists?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/stacker/stack/template.rb', line 17

def exists?
  File.exists? path
end

#localObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stacker/stack/template.rb', line 26

def local
  raise TemplateDoesNotExistError.new name unless exists?
  @local ||= begin
    template = if json?
      JSON.parse local_raw
    else
      YAML.load local_raw
    end
    template['AWSTemplateFormatVersion'] ||= FORMAT_VERSION
    template
  end
rescue JSON::ParserError, Psych::SyntaxError
  raise TemplateSyntaxError.new path
end

#local_rawObject



21
22
23
# File 'lib/stacker/stack/template.rb', line 21

def local_raw
  File.read path
end

#remoteObject



48
49
50
51
52
53
54
55
56
# File 'lib/stacker/stack/template.rb', line 48

def remote
  @remote ||= json? ? JSON.parse(remote_raw) : YAML.parse(remote_raw)
rescue Aws::CloudFormation::Errors::ValidationError => err
  if err.message =~ /does not exist/
    raise DoesNotExistError.new err.message
  else
    raise Error.new err.message
  end
end

#remote_rawObject



41
42
43
44
45
# File 'lib/stacker/stack/template.rb', line 41

def remote_raw
  stack.region.client.get_template(
    stack_name: stack.name
  ).template_body
end

#write(value = local) ⇒ Object



67
68
69
# File 'lib/stacker/stack/template.rb', line 67

def write value = local
  File.write path, JSONFormatter.format(value)
end