class OpenStax::Aws::Template

Attributes

absolute_file_path[R]

Public Class Methods

from_absolute_file_path(absolute_file_path) click to toggle source
# File lib/openstax/aws/template.rb, line 17
def self.from_absolute_file_path(absolute_file_path)
  new(absolute_file_path: absolute_file_path)
end
from_body(body) click to toggle source
# File lib/openstax/aws/template.rb, line 21
def self.from_body(body)
  new(body: body)
end
new(absolute_file_path: nil, body: nil) click to toggle source
# File lib/openstax/aws/template.rb, line 108
def initialize(absolute_file_path: nil, body: nil)
  raise "One of `absolute_file_path` or `body` must be set" if absolute_file_path.blank? && body.nil?
  @absolute_file_path = absolute_file_path
  @body = body.try(:dup)
end

Public Instance Methods

basename() click to toggle source
# File lib/openstax/aws/template.rb, line 25
def basename
  File.basename(absolute_file_path)
end
body() click to toggle source
# File lib/openstax/aws/template.rb, line 37
def body
  return @body unless @body.nil?

  @body = File.read(absolute_file_path)
  @body = ERB.new(@body).tap { |erb| erb.filename = absolute_file_path }.result if erb?
  @body
end
erb?() click to toggle source
# File lib/openstax/aws/template.rb, line 33
def erb?
  extname == '.erb'
end
extname() click to toggle source
# File lib/openstax/aws/template.rb, line 29
def extname
  File.extname(absolute_file_path)
end
hash() click to toggle source
# File lib/openstax/aws/template.rb, line 45
def hash
  json_hash || yml_hash || raise("Cannot read template #{absolute_file_path}")
end
is_sam?() click to toggle source
# File lib/openstax/aws/template.rb, line 102
def is_sam?
  body.match(/Transform: AWS::Serverless/).present?
end
parameter_names() click to toggle source
# File lib/openstax/aws/template.rb, line 59
def parameter_names
  hash["Parameters"].try(:keys) || []
end
required_capabilities() click to toggle source
# File lib/openstax/aws/template.rb, line 63
def required_capabilities
  has_an_iam_resource = hash["Resources"].any? do |resource_id, resource_body|
    resource_body["Type"].starts_with?("AWS::IAM::")
  end

  # A bit of a cop out to claim named_iam since we haven't checked
  # to see if any of the IAM resources have custom names, but it will
  # work
  has_an_iam_resource ? [:named_iam] : []
end
s3_folder() click to toggle source
# File lib/openstax/aws/template.rb, line 82
def s3_folder
  @unique_s3_folder ||=
    OpenStax::Aws.configuration.fixed_s3_template_folder ||
    Time.now.utc.strftime("%Y%m%d_%H%M%S_#{SecureRandom.hex(4)}")
end
s3_key() click to toggle source
# File lib/openstax/aws/template.rb, line 74
def s3_key
  [
    OpenStax::Aws.configuration.cfn_template_bucket_folder,
    s3_folder,
    basename
  ].compact.join("/")
end
s3_url() click to toggle source
# File lib/openstax/aws/template.rb, line 88
def s3_url
  upload_once
  "https://s3.amazonaws.com/#{OpenStax::Aws.configuration.cfn_template_bucket_name}/#{s3_key}"
end
upload_once() click to toggle source
# File lib/openstax/aws/template.rb, line 93
def upload_once
  return if @uploaded

  validate
  upload

  @uploaded = true
end
valid?() click to toggle source
# File lib/openstax/aws/template.rb, line 49
def valid?
  begin
    validate
  rescue TemplateInvalid
    return false
  end

  true
end

Protected Instance Methods

json_hash() click to toggle source
# File lib/openstax/aws/template.rb, line 132
def json_hash
  JSON.parse(body) rescue nil
end
upload() click to toggle source
# File lib/openstax/aws/template.rb, line 123
def upload
  client = Aws::S3::Client.new(region: OpenStax::Aws.configuration.cfn_template_bucket_region)
  client.put_object({
    body: body,
    bucket: OpenStax::Aws.configuration.cfn_template_bucket_name,
    key: s3_key
  })
end
validate() click to toggle source
# File lib/openstax/aws/template.rb, line 114
def validate
  begin
    # any region works here
    Aws::CloudFormation::Client.new(region: "us-west-2").validate_template(template_body: body)
  rescue Aws::CloudFormation::Errors::ValidationError => ee
    raise TemplateInvalid.new(basename + ": " + ee.message, absolute_file_path, body)
  end
end
yml_hash() click to toggle source
# File lib/openstax/aws/template.rb, line 136
def yml_hash
  YAML.load(body) rescue nil
end