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.
24 25 26 |
# File 'lib/terrafying/components/subnet.rb', line 24 def initialize() super end |
Instance Attribute Details
#az ⇒ Object (readonly)
Returns the value of attribute az.
14 15 16 |
# File 'lib/terrafying/components/subnet.rb', line 14 def az @az end |
#cidr ⇒ Object (readonly)
Returns the value of attribute cidr.
14 15 16 |
# File 'lib/terrafying/components/subnet.rb', line 14 def cidr @cidr end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/terrafying/components/subnet.rb', line 14 def id @id end |
#public ⇒ Object (readonly)
Returns the value of attribute public.
14 15 16 |
# File 'lib/terrafying/components/subnet.rb', line 14 def public @public end |
#route_table ⇒ Object (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
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, ={}) = { 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
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_addresses ⇒ Object
97 98 99 |
# File 'lib/terrafying/components/subnet.rb', line 97 def ip_addresses NetAddr::CIDR.create(@cidr).enumerate.drop(CIDR_PADDING) end |