Class: Terrafying::Components::Subnet

Inherits:
Terrafying::Context
  • Object
show all
Defined in:
lib/terrafying/components/subnet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSubnet

Returns a new instance of Subnet.



22
23
24
# File 'lib/terrafying/components/subnet.rb', line 22

def initialize
  super
end

Instance Attribute Details

#azObject (readonly)

Returns the value of attribute az.



12
13
14
# File 'lib/terrafying/components/subnet.rb', line 12

def az
  @az
end

#cidrObject (readonly)

Returns the value of attribute cidr.



12
13
14
# File 'lib/terrafying/components/subnet.rb', line 12

def cidr
  @cidr
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/terrafying/components/subnet.rb', line 12

def id
  @id
end

#publicObject (readonly)

Returns the value of attribute public.



12
13
14
# File 'lib/terrafying/components/subnet.rb', line 12

def public
  @public
end

#route_tableObject (readonly)

Returns the value of attribute route_table.



12
13
14
# File 'lib/terrafying/components/subnet.rb', line 12

def route_table
  @route_table
end

Class Method Details

.create_in(vpc, name, az, cidr, options = {}) ⇒ Object



14
15
16
# 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) ⇒ Object



18
19
20
# File 'lib/terrafying/components/subnet.rb', line 18

def self.find(id)
  Subnet.new.find(id)
end

Instance Method Details

#create_in(vpc, name, az, cidr, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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_addressesObject



90
91
92
# File 'lib/terrafying/components/subnet.rb', line 90

def ip_addresses
  NetAddr::CIDR.create(@cidr).enumerate.drop(CIDR_PADDING)
end