Class: CfnFlow::Template

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

Defined Under Namespace

Modules: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_path) ⇒ Template

Returns a new instance of Template.



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

def initialize(local_path)
  @local_path = local_path
end

Instance Attribute Details

#local_pathObject (readonly)

Returns the value of attribute local_path.



7
8
9
# File 'lib/cfn_flow/template.rb', line 7

def local_path
  @local_path
end

Instance Method Details

#bucketObject



65
66
67
# File 'lib/cfn_flow/template.rb', line 65

def bucket
  CfnFlow.template_s3_bucket
end

#is_cfn_template?Boolean

Determine if this file is a CFN template

Returns:

  • (Boolean)


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

def is_cfn_template?
  local_data.is_a?(Hash) && local_data.key?('Resources')
end

#json?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/cfn_flow/template.rb', line 16

def json?
  ! yaml?
end

#key(release) ⇒ Object

S3 methods



33
34
35
36
37
# File 'lib/cfn_flow/template.rb', line 33

def key(release)
  # Replace leading './' in local_path
  clean_path = local_path.sub(/\A\.\//, '')
  File.join(*[s3_prefix, release, clean_path].compact)
end

#local_dataObject



51
52
53
54
55
56
57
58
59
# File 'lib/cfn_flow/template.rb', line 51

def local_data
  # We *could* load JSON as YAML, but that would generate confusing errors
  # in the case of a JSON syntax error.
  @local_data ||= yaml? ? YAML.load_file(local_path) : MultiJson.load(File.read(local_path))
rescue Exception => error
  # Tag & re-raise any error
  error.extend(CfnFlow::Template::Error)
  raise error
end

#s3_object(release) ⇒ Object



39
40
41
# File 'lib/cfn_flow/template.rb', line 39

def s3_object(release)
  Aws::S3::Object.new(bucket, key(release))
end

#s3_prefixObject



69
70
71
# File 'lib/cfn_flow/template.rb', line 69

def s3_prefix
  CfnFlow.template_s3_prefix
end

#to_jsonObject



61
62
63
# File 'lib/cfn_flow/template.rb', line 61

def to_json
  @to_json ||= MultiJson.dump(local_data, pretty: true)
end

#upload(release) ⇒ Object



47
48
49
# File 'lib/cfn_flow/template.rb', line 47

def upload(release)
  s3_object(release).put(body: to_json)
end

#url(release) ⇒ Object



43
44
45
# File 'lib/cfn_flow/template.rb', line 43

def url(release)
  s3_object(release).public_url
end

#validate!Object

Returns a response object if valid, or raises an Aws::CloudFormation::Errors::ValidationError with an error message



27
28
29
# File 'lib/cfn_flow/template.rb', line 27

def validate!
  cfn.validate_template(template_body: to_json)
end

#yaml?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/cfn_flow/template.rb', line 12

def yaml?
  local_path.end_with?('.yml')
end