Class: Dcmgr::Models::Network

Inherits:
AccountResource show all
Defined in:
lib/dcmgr/models/network.rb

Overview

IP network definitions.

Defined Under Namespace

Modules: IpLeaseMethods

Constant Summary

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Instance Method Summary collapse

Methods inherited from AccountResource

#account

Methods inherited from BaseNew

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

Instance Method Details

#add_ipv4_dynamic_range(range_begin, range_end) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dcmgr/models/network.rb', line 143

def add_ipv4_dynamic_range(range_begin, range_end)
  test_inclusion(*validate_range_args(range_begin, range_end)) { |range, op|
     case op
     when :coverbegin
       range.range_end = range_begin
     when :coverend
       range.range_begin = range_end
     when :inccur
       range.destroy
     end
     range.save_changes
  }
  
  self.add_dhcp_range(:range_begin=>range_begin.to_s, :range_end=>range_end.to_s)
  
  self
end

#add_reserved(ipaddr) ⇒ Object

register reserved IP address in this network



126
127
128
129
# File 'lib/dcmgr/models/network.rb', line 126

def add_reserved(ipaddr)
  raise "Out of subnet range: #{ipaddr} to #{self.ipv4_ipaddress}/#{self.prefix}" if !self.include?(ipaddr)
  add_ip_lease(:ipv4=>ipaddr.to_s, :type=>IpLease::TYPE_RESERVED)
end

#available_ip_numsObject



131
132
133
# File 'lib/dcmgr/models/network.rb', line 131

def available_ip_nums
  self.ipv4_ipaddress.hosts.size - self.ip_lease_dataset.count
end

#before_destroyObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dcmgr/models/network.rb', line 86

def before_destroy
  #Make sure no other networks are natted to this one
  Network.filter(:nat_network_id => self[:id]).each { |n|
    n.nat_network_id = nil
    n.save
  }
  
  #Delete all reserved ipleases in this network
  self.ip_lease_dataset.filter(:alloc_type => IpLease::TYPE_RESERVED).each { |i|
    i.destroy
  }
  
  super
end

#before_validationObject



27
28
29
30
# File 'lib/dcmgr/models/network.rb', line 27

def before_validation
  self.link_interface ||= "br-#{self[:uuid]}"
  super
end

#del_ipv4_dynamic_range(range_begin, range_end) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/dcmgr/models/network.rb', line 161

def del_ipv4_dynamic_range(range_begin, range_end)
  test_inclusion(*validate_range_args(range_begin, range_end)) { |range, op|
    case op
    when :coverbegin
      range.range_end = range_begin
    when :coverend
      range.range_begin = range_end
    when :inccur
      range.destroy
    when :incnew
      t = range.range_end
      range.range_end = range_begin
      self.add_dhcp_range(:range_begin=>range_end, :range_end=>t)
    end
    range.save_changes
  }

  self
end

#include?(ipaddr) ⇒ Boolean

check if the given IP addess is in the range of this network.

Parameters:

  • ipaddr (String)

    IP address

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/dcmgr/models/network.rb', line 120

def include?(ipaddr)
  ipaddr = ipaddr.is_a?(IPAddress::IPv4) ? ipaddr : IPAddress::IPv4.new(ipaddr)
  self.ipv4_ipaddress.network.include?(ipaddr)
end

#ipv4_gw_ipaddressObject



113
114
115
116
# File 'lib/dcmgr/models/network.rb', line 113

def ipv4_gw_ipaddress
  return nil if self.ipv4_gw.nil?
  IPAddress::IPv4.new("#{self.ipv4_gw}/#{self.prefix}")
end

#ipv4_ipaddressObject



109
110
111
# File 'lib/dcmgr/models/network.rb', line 109

def ipv4_ipaddress
  IPAddress::IPv4.new("#{self.ipv4_network}/#{self.prefix}").network
end

#ipv4_u32_dynamic_range_arrayObject



135
136
137
138
139
140
141
# File 'lib/dcmgr/models/network.rb', line 135

def ipv4_u32_dynamic_range_array
  ary=[]
  dhcp_range_dataset.each { |r|
    ary += (r.range_begin.to_u32 .. r.range_end.to_u32).to_a
  }
  ary
end

#nat_networkObject



105
106
107
# File 'lib/dcmgr/models/network.rb', line 105

def nat_network
  Network.find(:id => self.nat_network_id)
end

#to_api_documentObject



101
102
103
# File 'lib/dcmgr/models/network.rb', line 101

def to_api_document
  to_hash.merge(:id=>self.canonical_uuid)
end

#to_hashObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dcmgr/models/network.rb', line 71

def to_hash
  h = super
  h.delete(:vlan_lease_id)
  h.merge({
            :bandwidth_mark=>self[:id],
            :description=>description.to_s,
            :vlan_id => vlan_lease.nil? ? 0 : vlan_lease.tag_id,
          })
  if self.physical_network
    h[:physical_network] = self.physical_network.to_hash
  end
 
  h
end

#validateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dcmgr/models/network.rb', line 32

def validate
  super
  
  unless (1..31).include?(self.prefix.to_i)
    errors.add(:prefix, "prefix must be 1-31: #{self.prefix}")
  end

  network_addr = begin
                   IPAddress::IPv4.new("#{self.ipv4_network}/#{self.prefix}").network
                 rescue => e
                   errors.add(:ipv4_network, "Invalid IP address syntax: #{self.ipv4_network}")
                 end
  # validate ipv4 syntax
  if self.ipv4_gw
    begin
      if !network_addr.include?(IPAddress::IPv4.new("#{self.ipv4_gw}"))
        errors.add(:ipv4_gw, "Out of network address range: #{network_addr.to_s}")
      end
    rescue => e
      errors.add(:ipv4_gw, "Invalid IP address syntax: #{self.ipv4_gw}")
    end
  end
  
  if self.dhcp_server
    begin
      if !network_addr.include?(IPAddress::IPv4.new("#{self.dhcp_server}"))
        errors.add(:dhcp_server, "Out of network address range: #{network_addr.to_s}")
      end
    rescue => e
      errors.add(:dhcp_server, "Invalid IP address syntax: #{self.dhcp_server}")
    end
  end

  if self.link_interface.size > 16
    errors.add(:link_interface, "Can not be the character lenth more than 16(=IF_NAMESIZ) ASCII characters.")
  end
  
end