Class: CloudCompose::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_compose/template.rb

Defined Under Namespace

Classes: Error, Partial

Constant Summary collapse

IMPORT_MERGE_KEYS =
%w[
  Metadata
  Parameters
  Mappings
  Conditions
  Transform
  Resources
  Outputs
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, pwd) ⇒ Template

Returns a new instance of Template.



31
32
33
34
35
36
37
38
39
40
# File 'lib/cloud_compose/template.rb', line 31

def initialize(path, pwd)
  @pwd = pwd
  @file_name = File.basename(path)
  @root = Pathname.new(File.dirname(path))
  content = File.read(path)
  @checksum = Digest::SHA256.hexdigest(content)
  parts = content.split(/^---/, 3)
  @config = CloudCompose::Config.new(parts[1], @root)
  @content = parts[2] || ''
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/cloud_compose/template.rb', line 29

def config
  @config
end

#contentObject (readonly)

Returns the value of attribute content.



29
30
31
# File 'lib/cloud_compose/template.rb', line 29

def content
  @content
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



29
30
31
# File 'lib/cloud_compose/template.rb', line 29

def file_name
  @file_name
end

#rootObject (readonly)

Returns the value of attribute root.



29
30
31
# File 'lib/cloud_compose/template.rb', line 29

def root
  @root
end

Instance Method Details

#checksumsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cloud_compose/template.rb', line 96

def checksums
  return @checksums if @checksums

  checksums = {}
  checksums["#{working_path}/#{file_name}"] = @checksum
  imported.each do |imp|
    imp.template.checksums.each do |key, value|
      checksums[key] = value
    end
  end
  @checksums = checksums
  @checksums
end

#description(depth) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/cloud_compose/template.rb', line 85

def description(depth)
  callouts = depth.zero? ? '├─' : ''
  leading = depth.zero? ? '' : '|'
  padding = ' ' * depth
  joined = imported.map { |i| i.template.description(depth + 2) }
  [
    "# #{leading}#{padding}#{callouts} #{working_path}/#{file_name}",
    *joined
  ].join("\n")
end

#importedObject



72
73
74
75
# File 'lib/cloud_compose/template.rb', line 72

def imported
  preload_imports!
  @imported
end

#parametersObject



77
78
79
# File 'lib/cloud_compose/template.rb', line 77

def parameters
  @config.parameters
end

#required_parametersObject



81
82
83
# File 'lib/cloud_compose/template.rb', line 81

def required_parameters
  @config.required_parameters
end

#to_hObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloud_compose/template.rb', line 42

def to_h
  template_body = create_hash(parameters)
  imported.each do |imp|
    formatted_imp = imp.template.send(:create_hash, imp.context)
    merge_imported(template_body, formatted_imp, imp.name)
  end

  template_body.keys.each do |key|
    template_body.delete(key) if template_body[key].empty?
  end

  template_body
end

#to_sObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cloud_compose/template.rb', line 56

def to_s
  yaml = CloudCompose::Parser.dump_yaml(to_h)

  <<~EOF
    # This Template was generated on #{Time.now.utc.iso8601} by CloudCompose #{CloudCompose::VERSION}
    #
    # Structure
    #{description(0)}
    #
    # Integrity
    #{checksum_print}
    #
    #{yaml}
  EOF
end