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.



24
25
26
# File 'lib/terrafying/components/subnet.rb', line 24

def initialize()
  super
end

Instance Attribute Details

#azObject (readonly)

Returns the value of attribute az.



14
15
16
# File 'lib/terrafying/components/subnet.rb', line 14

def az
  @az
end

#cidrObject (readonly)

Returns the value of attribute cidr.



14
15
16
# File 'lib/terrafying/components/subnet.rb', line 14

def cidr
  @cidr
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/terrafying/components/subnet.rb', line 14

def id
  @id
end

#publicObject (readonly)

Returns the value of attribute public.



14
15
16
# File 'lib/terrafying/components/subnet.rb', line 14

def public
  @public
end

#route_tableObject (readonly)

Returns the value of attribute route_table.



14
15
16
# File 'lib/terrafying/components/subnet.rb', line 14

def route_table
  @route_table
end

Class Method Details

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



16
17
18
# File 'lib/terrafying/components/subnet.rb', line 16

def self.create_in(vpc, name, az, cidr, options={})
  Subnet.new.create_in vpc, name, az, cidr, options
end

.find(id) ⇒ Object



20
21
22
# File 'lib/terrafying/components/subnet.rb', line 20

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

Instance Method Details

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



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
89
90
91
92
93
94
95
# File 'lib/terrafying/components/subnet.rb', line 48

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/terrafying/components/subnet.rb', line 28

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
    # 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



97
98
99
# File 'lib/terrafying/components/subnet.rb', line 97

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