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!, #to_hash, unlock!, #with_timestamps?

Class Method Details

.lease(instance_nic, network) ⇒ Object

Raises:

  • (TypeError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dcmgr/models/ip_lease.rb', line 63

def self.lease(instance_nic, network)
  raise TypeError unless instance_nic.is_a?(InstanceNic)
  raise TypeError unless network.is_a?(Network)

  reserved = []
  reserved << network.ipv4_gw_ipaddress if network.ipv4_gw
  reserved << IPAddress::IPv4.new(network.dhcp_server) if network.dhcp_server
  reserved = reserved.map {|i| i.to_u32 }
  # use SELECT FOR UPDATE to lock rows within same network.
  addrs = network.ipv4_u32_dynamic_range_array - 
    reserved - network.ip_lease_dataset.for_update.all.map {|i| IPAddress::IPv4.new(i.ipv4).to_u32 }
  raise "Run out of dynamic IP addresses from the network segment: #{network.ipv4_network.to_s}/#{network.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.



36
37
38
# File 'lib/dcmgr/models/ip_lease.rb', line 36

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.



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

def nat_inside_lease
  if self.instance_nic.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.



44
45
46
47
48
49
50
# File 'lib/dcmgr/models/ip_lease.rb', line 44

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

#validateObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dcmgr/models/ip_lease.rb', line 21

def validate
  # validate ipv4 syntax
  begin
    addr = IPAddress::IPv4.new("#{self.ipv4}")
    # validate if ipv4 is in the range of network_id.
    unless 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