Class: Cfhighlander::Cloudformation::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/cfhighlander.validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ Validator

Returns a new instance of Validator.



12
13
14
# File 'lib/cfhighlander.validator.rb', line 12

def initialize(component)
  @component = component
end

Instance Method Details

#validate(destination_template_locations, format) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cfhighlander.validator.rb', line 16

def validate(destination_template_locations, format)
  destination_template_locations.each do |file|

    # validate cloudformation template
    file_size_bytes = File.size(file)

    #:template_body (String) — Structure containing the template body with a minimum length of
    # 1 byte and a maximum length of 51,200 bytes. For more information,
    # go to Template Anatomy in the AWS CloudFormation User Guide.

    if file_size_bytes > 51200
      validate_s3 (file)
    else
      validate_local(file)
    end

  end

end

#validate_local(path) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/cfhighlander.validator.rb', line 36

def validate_local(path)
  puts "Validate template #{path} locally"
  template = File.read path
  awscfn = Aws::CloudFormation::Client.new
  response = awscfn.validate_template({
      template_body: template
  })
  puts 'SUCCESS'
end

#validate_s3(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cfhighlander.validator.rb', line 46

def validate_s3(path)
  template = File.read path
  bucket = @component.highlander_dsl.distribution_bucket
  if bucket.nil?
    bucket = "#{()}.#{aws_current_region()}.cfhighlander.templates"
    s3_create_bucket_if_not_exists(bucket)
  end
  prefix = @component.highlander_dsl.distribution_prefix
  md5 = Digest::MD5.hexdigest template
  s3_key = "#{prefix}/highlander/validate/#{md5}"
  s3 = Aws::S3::Client.new({region: s3_bucket_region(bucket)})

  puts "Upload #{path} to s3://#{bucket}/#{s3_key}"
  s3.put_object({ body: template, bucket: bucket, key: s3_key })
  awscfn = Aws::CloudFormation::Client.new

  puts "Validate s3://#{bucket}/#{s3_key}"
  response = awscfn.validate_template({
      template_url: "https://#{bucket}.s3.amazonaws.com/#{s3_key}"
  })
  puts "Delete s3://#{bucket}/#{s3_key}"
  s3.delete_object({ bucket: bucket, key: s3_key })
  puts 'SUCCESS'

end