class Terrafying::Components::Subnet

Attributes

az[R]
cidr[R]
id[R]
public[R]
route_table[R]

Public Class Methods

create_in(vpc, name, az, cidr, options = {}) click to toggle source
# File lib/terrafying/components/subnet.rb, line 14
def self.create_in(vpc, name, az, cidr, options = {})
  Subnet.new.create_in vpc, name, az, cidr, options
end
find(id) click to toggle source
# File lib/terrafying/components/subnet.rb, line 18
def self.find(id)
  Subnet.new.find(id)
end
new() click to toggle source
Calls superclass method
# File lib/terrafying/components/subnet.rb, line 22
def initialize
  super
end

Public Instance Methods

create_in(vpc, name, az, cidr, options = {}) click to toggle source
# File lib/terrafying/components/subnet.rb, line 46
def create_in(vpc, name, az, cidr, options = {})
  options = {
    tags: {}
  }.merge(options)

  ident = "#{tf_safe(vpc.name)}-#{name}-#{az}"

  @cidr = cidr
  @az = az
  @id = resource :aws_subnet, ident,
                 vpc_id: vpc.id,
                 cidr_block: cidr,
                 availability_zone: az,
                 tags: { Name: ident }.merge(options[:tags])

  @route_table = resource :aws_route_table, ident,
                          vpc_id: vpc.id,
                          tags: { Name: ident }.merge(options[:tags])

  if options[:nat_gateway]
    gateway = { nat_gateway_id: options[:nat_gateway] }
    @public = false
  elsif options[:gateway]
    gateway = { gateway_id: options[:gateway] }
    @public = true
  else
    gateway = nil
    @public = false
  end

  if gateway
    resource :aws_route, "#{ident}-default", {
      route_table_id: @route_table,
      destination_cidr_block: '0.0.0.0/0'
    }.merge(gateway)
  end

  resource :aws_route_table_association, ident,
           subnet_id: @id,
           route_table_id: @route_table

  self
end
find(id) click to toggle source
# File lib/terrafying/components/subnet.rb, line 26
def find(id)
  subnet = aws.subnet_by_id(id)

  @id = id
  @cidr = subnet.cidr_block
  @az = subnet.availability_zone

  begin
    route_table = aws.route_table_for_subnet(id)
  rescue StandardError
    # fallback to main route table if ones not explicitly associated
    route_table = aws.route_table_for_vpc(subnet.vpc_id)
  end

  @route_table = route_table.route_table_id
  @public = route_table.routes.select { |r| r.destination_cidr_block == '0.0.0.0/0' }.first.gateway_id != nil

  self
end
ip_addresses() click to toggle source
# File lib/terrafying/components/subnet.rb, line 90
def ip_addresses
  NetAddr::CIDR.create(@cidr).enumerate.drop(CIDR_PADDING)
end