class Covalence::S3::Client

Public Class Methods

new(region: REGION) click to toggle source
# File lib/covalence/core/state_stores/s3.rb, line 20
def initialize(region: REGION)
  @s3 = Aws::S3::Client.new(region: region)
  self.reset_cache
end

Public Instance Methods

get_cache() click to toggle source
# File lib/covalence/core/state_stores/s3.rb, line 29
def get_cache
  @cache.to_s
end
get_doc(bucket, document) click to toggle source
# File lib/covalence/core/state_stores/s3.rb, line 33
def get_doc(bucket, document)
  @cache[bucket][document] ||= begin
    @s3.get_object(bucket: bucket, key: document).body.read
  rescue Aws::S3::Errors::ServiceError => err
    raise err, "Unable to retrieve document '#{document}' from bucket '#{bucket}'"
  end
end
get_key(bucket, document, name) click to toggle source
# File lib/covalence/core/state_stores/s3.rb, line 41
def get_key(bucket, document, name)
  begin
    doc = self.get_doc(bucket, document)
  rescue Aws::S3::Errors::ServiceError => err
    fail err.message + " and the key is: #{name}. Check your data files."
  end

  # Parse JSON response
  begin
    parsed = JSON.parse(doc)
  rescue JSON::ParserError => err
    fail "No results or unable to parse document '#{document}': " + err.message
  end

  # Determine whether the document is a Terraform state file
  tf_state = true if parsed.has_key?('terraform_version')

  # Return ID for the key specified
  if tf_state
    tf_vers = Gem::Version.new(parsed.fetch('terraform_version'))

    if tf_vers >= Gem::Version.new('0.12.0')
      root = parsed
    else
      root = parsed.fetch('modules')[0]
    end

    outputs = root.fetch('outputs')
    return outputs.fetch(name)
  end
  return parsed.fetch(name) if parsed.has_key?(name)
  fail "Requested key '#{name}' not found"
end
reset_cache() click to toggle source
# File lib/covalence/core/state_stores/s3.rb, line 25
def reset_cache
  @cache = Hash.new{|h,k| h[k] = Hash.new}
end