Class: Dcmgr::Models::IpLease

Inherits:
BaseNew
  • Object
show all
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
TYPE_MESSAGES =
{
  TYPE_AUTO=>'auto',
  TYPE_RESERVED=>'reserved',
  TYPE_MANUAL=>'manual'
}

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseNew

Proxy, dataset, default_row_lock_mode=, install_data, install_data_hooks, lock!, unlock!, #with_timestamps?

Class Method Details

.lease(instance_nic, network) ⇒ Object

Raises:

  • (TypeError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dcmgr/models/ip_lease.rb', line 75

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

#is_natted?TrueClass, FalseClass

check if the current lease is for NAT outside address lease.

Returns:

  • (TrueClass, FalseClass)

    return true if the lease is for NAT outside.



48
49
50
# File 'lib/dcmgr/models/ip_lease.rb', line 48

def is_natted?
  instance_nic.network_id != network_id
end

#nat_inside_leaseIpLease?

get the lease of NAT inside network.

Returns:

  • (IpLease, nil)

    IpLease (outside) will return inside IpLease.



67
68
69
70
71
72
73
# File 'lib/dcmgr/models/ip_lease.rb', line 67

def nat_inside_lease
  if self.network.nat_network_id.nil?
    self.class.find(:instance_nic_id=>self.instance_nic.id, :network_id=>nil)
  else
    nil
  end
end

#nat_outside_leaseIpLease?

get the lease of NAT outside network.

Returns:

  • (IpLease, nil)

    if the IpLease has a pair NAT address it will return outside IpLease.



56
57
58
59
60
61
62
# File 'lib/dcmgr/models/ip_lease.rb', line 56

def nat_outside_lease
  if self.network.nat_network_id
    self.class.find(:instance_nic_id=>self.instance_nic.id, :network_id=>self.network.nat_network_id)
  else
    nil
  end
end

#validateObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dcmgr/models/ip_lease.rb', line 33

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