class Formatron::CloudFormation::Template::VPC

generates CloudFormation VPC resources rubocop:disable Metrics/ClassLength

Constants

HOSTED_ZONE_PREFIX
INTERNET_GATEWAY_PREFIX
ROUTE_PREFIX
ROUTE_TABLE_PREFIX
VPC_GATEWAY_ATTACHMENT_PREFIX
VPC_PREFIX

Public Class Methods

new( vpc:, external:, hosted_zone_name:, key_pair:, administrator_name:, administrator_password:, kms_key:, hosted_zone_id:, bucket:, name:, target: ) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/ParameterLists

# File lib/formatron/cloud_formation/template/vpc.rb, line 21
def initialize(
  vpc:,
  external:,
  hosted_zone_name:,
  key_pair:,
  administrator_name:,
  administrator_password:,
  kms_key:,
  hosted_zone_id:,
  bucket:,
  name:,
  target:
)
  @vpc = vpc
  @external = external
  @hosted_zone_name = hosted_zone_name
  @key_pair = key_pair
  @administrator_name = administrator_name
  @administrator_password = administrator_password
  @kms_key = kms_key
  @hosted_zone_id = hosted_zone_id
  @bucket = bucket
  @name = name
  @target = target
end

Public Instance Methods

merge(resources:, outputs:) click to toggle source

rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/MethodLength

# File lib/formatron/cloud_formation/template/vpc.rb, line 49
def merge(resources:, outputs:)
  @guid = @vpc.guid
  if @guid.nil?
    @guid = @external.guid
    _merge_external resources: resources, outputs: outputs
  else
    _merge_local resources: resources, outputs: outputs
  end
end

Private Instance Methods

_add_internet_gateway(resources) click to toggle source
# File lib/formatron/cloud_formation/template/vpc.rb, line 134
def _add_internet_gateway(resources)
  resources[@internet_gateway_id] = Resources::EC2.internet_gateway
end
_add_private_hosted_zone(resources, outputs) click to toggle source
# File lib/formatron/cloud_formation/template/vpc.rb, line 165
def _add_private_hosted_zone(resources, outputs)
  resources[@private_hosted_zone_id] = Resources::Route53.hosted_zone(
    name: @hosted_zone_name,
    vpc: @logical_id
  )
  outputs[@private_hosted_zone_id] = Template.output(
    Template.ref(@private_hosted_zone_id)
  )
end
_add_route(resources) click to toggle source
# File lib/formatron/cloud_formation/template/vpc.rb, line 155
def _add_route(resources)
  resources[
    @route_id
  ] = Resources::EC2.route(
    vpc_gateway_attachment: @vpc_gateway_attachment_id,
    internet_gateway: @internet_gateway_id,
    route_table: @route_table_id
  )
end
_add_route_table(resources) click to toggle source
# File lib/formatron/cloud_formation/template/vpc.rb, line 147
def _add_route_table(resources)
  resources[
    @route_table_id
  ] = Resources::EC2.route_table(
    vpc: @logical_id
  )
end
_add_vpc(resources, outputs) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/formatron/cloud_formation/template/vpc.rb, line 129
def _add_vpc(resources, outputs)
  resources[@logical_id] = Resources::EC2.vpc cidr: @cidr
  outputs[@logical_id] = Template.output Template.ref(@logical_id)
end
_add_vpc_gateway_attachment(resources) click to toggle source
# File lib/formatron/cloud_formation/template/vpc.rb, line 138
def _add_vpc_gateway_attachment(resources)
  resources[
    @vpc_gateway_attachment_id
  ] = Resources::EC2.vpc_gateway_attachment(
    vpc: @logical_id,
    gateway: @internet_gateway_id
  )
end
_merge_external(resources:, outputs:) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/template/vpc.rb, line 102
def _merge_external(resources:, outputs:)
  @cidr = @external.cidr
  @private_hosted_zone_id =
    "#{HOSTED_ZONE_PREFIX}#{@guid}"
  @vpc.subnet.each do |key, subnet|
    template_subnet = Subnet.new(
      subnet: subnet,
      external: @external.subnet[key],
      vpc_guid: @guid,
      vpc_cidr: @cidr,
      key_pair: @key_pair,
      administrator_name: @administrator_name,
      administrator_password: @administrator_password,
      hosted_zone_name: @hosted_zone_name,
      kms_key: @kms_key,
      nats: Util::VPC.instances(:nat, @external, @vpc),
      private_hosted_zone_id: @private_hosted_zone_id,
      public_hosted_zone_id: @hosted_zone_id,
      bucket: @bucket,
      name: @name,
      target: @target
    )
    template_subnet.merge resources: resources, outputs: outputs
  end
end
_merge_local(resources:, outputs:) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/formatron/cloud_formation/template/vpc.rb, line 60
def _merge_local(resources:, outputs:)
  @cidr = @vpc.cidr
  @logical_id = "#{VPC_PREFIX}#{@guid}"
  @internet_gateway_id = "#{INTERNET_GATEWAY_PREFIX}#{@guid}"
  @vpc_gateway_attachment_id =
    "#{VPC_GATEWAY_ATTACHMENT_PREFIX}#{@guid}"
  @route_table_id =
    "#{ROUTE_TABLE_PREFIX}#{@guid}"
  @route_id =
    "#{ROUTE_PREFIX}#{@guid}"
  @private_hosted_zone_id =
    "#{HOSTED_ZONE_PREFIX}#{@guid}"
  @vpc.subnet.each do |_, subnet|
    template_subnet = Subnet.new(
      subnet: subnet,
      external: nil,
      vpc_guid: @guid,
      vpc_cidr: @cidr,
      key_pair: @key_pair,
      administrator_name: @administrator_name,
      administrator_password: @administrator_password,
      hosted_zone_name: @hosted_zone_name,
      kms_key: @kms_key,
      nats: Util::VPC.instances(:nat, @vpc),
      private_hosted_zone_id: @private_hosted_zone_id,
      public_hosted_zone_id: @hosted_zone_id,
      bucket: @bucket,
      name: @name,
      target: @target
    )
    template_subnet.merge resources: resources, outputs: outputs
  end
  _add_vpc resources, outputs
  _add_internet_gateway resources
  _add_vpc_gateway_attachment resources
  _add_route_table resources
  _add_route resources
  _add_private_hosted_zone resources, outputs
end