Class: Awshark::CloudFormation::Template

Inherits:
Object
  • Object
show all
Includes:
Files
Defined in:
lib/awshark/cloud_formation/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Files

#parse_file, #parse_string

Constructor Details

#initialize(path, options = {}) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
16
17
18
19
# File 'lib/awshark/cloud_formation/template.rb', line 11

def initialize(path, options = {})
  @path = path

  @bucket_and_path = options[:bucket]
  @bucket = (options[:bucket] || '').split('/')[0]
  @name = options[:name]
  @stage = options[:stage]
  @format = options[:format] || 'yaml'
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



9
10
11
# File 'lib/awshark/cloud_formation/template.rb', line 9

def bucket
  @bucket
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/awshark/cloud_formation/template.rb', line 9

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/awshark/cloud_formation/template.rb', line 9

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/awshark/cloud_formation/template.rb', line 8

def path
  @path
end

#stageObject (readonly)

Returns the value of attribute stage.



9
10
11
# File 'lib/awshark/cloud_formation/template.rb', line 9

def stage
  @stage
end

Instance Method Details

#bodyObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/awshark/cloud_formation/template.rb', line 22

def body
  return nil if template_path.blank?

  content = File.read(template_path)
  content = ERB.new(content).result_with_hash(context)

  if format == 'json'
    json = parse_string(template_path, content)
    return JSON.pretty_generate(json)
  end

  content
end

#contextObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/awshark/cloud_formation/template.rb', line 37

def context
  @context ||=
    begin
      context = parse_file(context_path) || {}
      context = context[stage] if context.key?(stage)

      {
        context: RecursiveOpenStruct.new(context),
        aws_account_id: Awshark.config.sts.,
        stage: stage,
        ssm: ssm
      }
    end
end

#sizeObject



53
54
55
# File 'lib/awshark/cloud_formation/template.rb', line 53

def size
  body.size
end

#uploaded?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/awshark/cloud_formation/template.rb', line 58

def uploaded?
  @uploaded == true
end

#urlObject



63
64
65
66
67
# File 'lib/awshark/cloud_formation/template.rb', line 63

def url
  upload unless uploaded?

  "https://#{bucket}.s3.#{region}.amazonaws.com/#{s3_key}"
end