Class: Terrafying::Components::Subnet
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::Subnet
- Defined in:
- lib/terrafying/components/subnet.rb
Instance Attribute Summary collapse
-
#az ⇒ Object
readonly
Returns the value of attribute az.
-
#cidr ⇒ Object
readonly
Returns the value of attribute cidr.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#public ⇒ Object
readonly
Returns the value of attribute public.
-
#route_table ⇒ Object
readonly
Returns the value of attribute route_table.
Class Method Summary collapse
Instance Method Summary collapse
- #create_in(vpc, name, az, cidr, options = {}) ⇒ Object
- #find(id) ⇒ Object
-
#initialize ⇒ Subnet
constructor
A new instance of Subnet.
- #ip_addresses ⇒ Object
Constructor Details
#initialize ⇒ Subnet
Returns a new instance of Subnet.
22 23 24 |
# File 'lib/terrafying/components/subnet.rb', line 22 def initialize super end |
Instance Attribute Details
#az ⇒ Object (readonly)
Returns the value of attribute az.
12 13 14 |
# File 'lib/terrafying/components/subnet.rb', line 12 def az @az end |
#cidr ⇒ Object (readonly)
Returns the value of attribute cidr.
12 13 14 |
# File 'lib/terrafying/components/subnet.rb', line 12 def cidr @cidr end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
12 13 14 |
# File 'lib/terrafying/components/subnet.rb', line 12 def id @id end |
#public ⇒ Object (readonly)
Returns the value of attribute public.
12 13 14 |
# File 'lib/terrafying/components/subnet.rb', line 12 def public @public end |
#route_table ⇒ Object (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
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, = {}) = { tags: {} }.merge() 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([:tags]) @route_table = resource :aws_route_table, ident, vpc_id: vpc.id, tags: { Name: ident }.merge([:tags]) if [:nat_gateway] gateway = { nat_gateway_id: [:nat_gateway] } @public = false elsif [:gateway] gateway = { gateway_id: [: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_addresses ⇒ Object
90 91 92 |
# File 'lib/terrafying/components/subnet.rb', line 90 def ip_addresses NetAddr::CIDR.create(@cidr).enumerate.drop(CIDR_PADDING) end |