Class: Akashi::Vpc::Subnet::Base
- Inherits:
-
Base
show all
- Defined in:
- lib/akashi/vpc/subnet/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Ec2::Base
#name, #name=, service_class
Methods inherited from Base
base_class, collection, find, find_by, #initialize, where
Constructor Details
This class inherits a constructor from Akashi::Base
Class Method Details
.all ⇒ Object
24
25
26
|
# File 'lib/akashi/vpc/subnet/base.rb', line 24
def all
super.select { |subnet| base_cidr_block.include?(subnet.cidr_block) }
end
|
.cidr ⇒ Object
79
80
81
|
# File 'lib/akashi/vpc/subnet/base.rb', line 79
def cidr
@cidr ||= 24
end
|
.create(vpc:, availability_zone:) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/akashi/vpc/subnet/base.rb', line 28
def create(vpc:, availability_zone:)
response = Akashi::Aws.ec2.client.create_subnet(
vpc_id: vpc.id,
availability_zone: availability_zone,
cidr_block: next_cidr_block(vpc: vpc).to_s + "/#{cidr}",
)
new(response[:subnet][:subnet_id]).tap do |instance|
instance.name = name(vpc: vpc)
puts "Created a Subnet (\#{instance.id}) whose role is \"\#{role}\". Availability zone is \"\#{instance.availability_zone_name}\".\n EOS\n end\nend\n"
|
.current_number(vpc:) ⇒ Object
43
44
45
|
# File 'lib/akashi/vpc/subnet/base.rb', line 43
def current_number(vpc:)
"%02d" % where(vpc_id: vpc.id).count
end
|
.latest_cidr_block(vpc:) ⇒ Object
56
57
58
|
# File 'lib/akashi/vpc/subnet/base.rb', line 56
def latest_cidr_block(vpc:)
where(vpc_id: vpc.id).map(&:cidr_block).max
end
|
.name(vpc:) ⇒ Object
83
84
85
|
# File 'lib/akashi/vpc/subnet/base.rb', line 83
def name(vpc:)
"#{Akashi.name}-#{role}-#{current_number(vpc: vpc)}"
end
|
.netmask ⇒ Object
netmask_binary #=> “11111111111111111111111100000000” netmask #=> IPAddr.new(4294967040, Socket::AF_INET) netmask.to_s #=> “255.255.255.0”
69
70
71
|
# File 'lib/akashi/vpc/subnet/base.rb', line 69
def netmask
@netmask ||= IPAddr.new("0b#{netmask_binary}".oct, Socket::AF_INET)
end
|
.netmask_binary ⇒ Object
cidr #=> 24 netmask_binary #=> “11111111111111111111111100000000”
75
76
77
|
# File 'lib/akashi/vpc/subnet/base.rb', line 75
def netmask_binary
@netmask_binary ||= ("1" * cidr).ljust(32, "0")
end
|
.next_cidr_block(vpc:) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/akashi/vpc/subnet/base.rb', line 47
def next_cidr_block(vpc:)
_latest_cidr_block = latest_cidr_block(vpc: vpc)
IPAddr.new(
_latest_cidr_block ? _latest_cidr_block.to_i + step : base_cidr_block.to_s,
Socket::AF_INET,
)
end
|
.object_class ⇒ Object
91
92
93
|
# File 'lib/akashi/vpc/subnet/base.rb', line 91
def object_class
@object_class ||= "Subnet"
end
|
.role ⇒ Object
87
88
89
|
# File 'lib/akashi/vpc/subnet/base.rb', line 87
def role
self.to_s.demodulize.underscore.dasherize
end
|
.step ⇒ Object
netmask #=> IPAddr.new(4294967040, Socket::AF_INET) step #=> 256
62
63
64
|
# File 'lib/akashi/vpc/subnet/base.rb', line 62
def step
@step ||= (~netmask).succ.to_i
end
|
Instance Method Details
#cidr_block ⇒ Object
10
11
12
|
# File 'lib/akashi/vpc/subnet/base.rb', line 10
def cidr_block
IPAddr.new(@object.cidr_block)
end
|
#number ⇒ Object
14
15
16
|
# File 'lib/akashi/vpc/subnet/base.rb', line 14
def number
name.slice(/(?<=-)\d{2}\Z/)
end
|
#route_table=(route_table) ⇒ Object
18
19
20
21
|
# File 'lib/akashi/vpc/subnet/base.rb', line 18
def route_table=(route_table)
@object.route_table = route_table.id
puts "A Subnet (#{id}) associated with a RouteTable (#{route_table.id})."
end
|