Class: Dcmgr::Models::IpLease
- Defined in:
- lib/dcmgr/models/ip_lease.rb
Overview
IP address lease information
Constant Summary collapse
- TYPE_AUTO =
0
- TYPE_RESERVED =
1
- TYPE_MANUAL =
2
Constants inherited from BaseNew
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from BaseNew
Proxy, dataset, install_data, install_data_hooks, lock!, unlock!
Class Method Details
.lease(instance_nic, network) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dcmgr/models/ip_lease.rb', line 39 def self.lease(instance_nic, network) raise TypeError unless instance_nic.is_a?(InstanceNic) raise TypeError unless network.is_a?(Network) gwaddr = network.ipaddress reserved = [gwaddr] reserved = reserved.map {|i| i.to_u32 } # use SELECT FOR UPDATE to lock rows within same network. addrs = (gwaddr.network.first.to_u32 .. gwaddr.network.last.to_u32).to_a - reserved - network.ip_lease_dataset.for_update.all.map {|i| IPAddress::IPv4.new(i.ipv4).to_u32 } raise "Out of free IP address in this network segment: #{gwaddr.network.to_s}/#{gwaddr.prefix}" if addrs.empty? leaseaddr = IPAddress::IPv4.parse_u32(addrs[rand(addrs.size).to_i]) create(:ipv4=>leaseaddr.to_s, :network_id=>network.id, :instance_nic_id=>instance_nic.id) end |
Instance Method Details
#validate ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dcmgr/models/ip_lease.rb', line 26 def validate # validate ipv4 syntax begin addr = IPAddress::IPv4.new("#{self.ipv4}") # validate if ipv4 is in the range of network_id. unless network.ipaddress.network.include?(addr) errors.add(:ipv4, "IP address #{addr} is out of range: #{network.canonical_uuid})") end rescue => e errors.add(:ipv4, "Invalid IP address syntax: #{self.ipv4} (#{e})") end end |