class Formatron::AWS

shared AWS clients rubocop:disable Metrics/ClassLength

Constants

CAPABILITIES
REGIONS
STACK_READY_STATES

Attributes

region[R]

Public Class Methods

new(credentials:) click to toggle source
# File lib/formatron/aws.rb, line 62
def initialize(credentials:)
  @credentials = JSON.parse(File.read(credentials))
  @region = @credentials['region']
  _create_aws_credentials
  _create_s3_client
  _create_cloudformation_client
  _create_route53_client
end

Public Instance Methods

delete_file(bucket:, key:) click to toggle source
# File lib/formatron/aws.rb, line 88
def delete_file(bucket:, key:)
  @s3_client.delete_object(
    bucket: bucket,
    key: key
  )
end
delete_stack(stack_name:) click to toggle source
# File lib/formatron/aws.rb, line 143
def delete_stack(stack_name:)
  cloud_formation_stack = Formatron::AWS::CloudFormationStack.new(
    stack_name: stack_name,
    client: @cloudformation_client
  )
  cloud_formation_stack.delete if cloud_formation_stack.exists?
end
deploy_stack(stack_name:, template_url:, parameters:) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/aws.rb, line 111
def deploy_stack(stack_name:, template_url:, parameters:)
  aws_parameters = parameters.map do |key, value|
    {
      parameter_key: key,
      parameter_value: value,
      use_previous_value: false
    }
  end
  cloud_formation_stack = Formatron::AWS::CloudFormationStack.new(
    stack_name: stack_name,
    client: @cloudformation_client
  )
  if !cloud_formation_stack.exists?
    cloud_formation_stack.create(
      template_url: template_url,
      parameters: aws_parameters
    )
  else
    cloud_formation_stack.update(
      template_url: template_url,
      parameters: aws_parameters
    )
  end
end
download_file(bucket:, key:, path:) click to toggle source
# File lib/formatron/aws.rb, line 95
def download_file(bucket:, key:, path:)
  @s3_client.get_object(
    bucket: bucket,
    key: key,
    response_target: path
  )
end
file_exists?(bucket:, key:) click to toggle source
# File lib/formatron/aws.rb, line 81
def file_exists?(bucket:, key:)
  @s3_client.list_objects(
    bucket: bucket,
    prefix: key
  ).contents.length > 0
end
get_file(bucket:, key:) click to toggle source
# File lib/formatron/aws.rb, line 103
def get_file(bucket:, key:)
  @s3_client.get_object(
    bucket: bucket,
    key: key
  ).body.read
end
hosted_zone_name(hosted_zone_id) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/formatron/aws.rb, line 137
def hosted_zone_name(hosted_zone_id)
  @route53_client.get_hosted_zone(
    id: hosted_zone_id
  ).hosted_zone.name.chomp '.'
end
stack_outputs(stack_name:) click to toggle source
# File lib/formatron/aws.rb, line 151
def stack_outputs(stack_name:)
  description = @cloudformation_client.describe_stacks(
    stack_name: stack_name
  ).stacks[0]
  status = description.stack_status
  fail "CloudFormation stack, #{stack_name}, " \
       "is not ready: #{status}" unless STACK_READY_STATES.include? status
  description.outputs.each_with_object({}) do |output, outputs|
    outputs[output.output_key] = output.output_value
  end
end
stack_ready!(stack_name:) click to toggle source
# File lib/formatron/aws.rb, line 163
def stack_ready!(stack_name:)
  status = @cloudformation_client.describe_stacks(
    stack_name: stack_name
  ).stacks[0].stack_status
  fail "CloudFormation stack, #{stack_name}, " \
       "is not ready: #{status}" unless STACK_READY_STATES.include? status
end
upload_file(kms_key:, bucket:, key:, content:) click to toggle source
# File lib/formatron/aws.rb, line 71
def upload_file(kms_key:, bucket:, key:, content:)
  @s3_client.put_object(
    bucket: bucket,
    key: key,
    body: content,
    server_side_encryption: 'aws:kms',
    ssekms_key_id: kms_key
  )
end

Private Instance Methods

_create_aws_credentials() click to toggle source
# File lib/formatron/aws.rb, line 171
def _create_aws_credentials
  @aws_credentials = Aws::Credentials.new(
    @credentials['access_key_id'],
    @credentials['secret_access_key']
  )
end
_create_cloudformation_client() click to toggle source
# File lib/formatron/aws.rb, line 186
def _create_cloudformation_client
  @cloudformation_client = ::Aws::CloudFormation::Client.new(
    region: @region,
    credentials: @aws_credentials
  )
end
_create_route53_client() click to toggle source
# File lib/formatron/aws.rb, line 193
def _create_route53_client
  @route53_client = ::Aws::Route53::Client.new(
    region: @region,
    credentials: @aws_credentials
  )
end
_create_s3_client() click to toggle source
# File lib/formatron/aws.rb, line 178
def _create_s3_client
  @s3_client = ::Aws::S3::Client.new(
    region: @region,
    signature_version: 'v4',
    credentials: @aws_credentials
  )
end