Class: Subnet

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authorization, Taxonomix
Defined in:
app/models/subnet.rb

Defined Under Namespace

Classes: Jail

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Taxonomix

included

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Class Method Details

.import(proxy) ⇒ Object

imports subnets from a dhcp smart proxy


113
114
115
116
117
118
119
120
121
# File 'app/models/subnet.rb', line 113

def self.import proxy
  return unless proxy.features.include?(Feature.find_by_name("DHCP"))
  ProxyAPI::DHCP.new(:url => proxy.url).subnets.map do |s|
    # do not import existing networks.
    attrs = { :network => s["network"], :mask => s["netmask"] }
    next if first(:conditions => attrs)
    new(attrs.update(:dhcp => proxy))
  end.compact
end

.subnet_for(ip) ⇒ Object

Given an IP returns the subnet that contains that IP

ip

: “doted quad” string

Returns : Subnet object or nil if not found


63
64
65
66
# File 'app/models/subnet.rb', line 63

def self.subnet_for(ip)
  Subnet.all.each {|s| return s if s.contains? IPAddr.new(ip)}
  nil
end

Instance Method Details

#<=>(other) ⇒ Object

Subnets are sorted on their priority value

other

: Subnet object with which to compare ourself

returns : Subnet object with higher precedence


52
53
54
55
56
57
58
# File 'app/models/subnet.rb', line 52

def <=> (other)
  if self.vlanid.present? && other.vlanid.present?
    self.vlanid <=> other.vlanid
  else
    return -1
  end
end

#as_json(options = {}) ⇒ Object


123
124
125
126
# File 'app/models/subnet.rb', line 123

def as_json options = {}
  options ||= {}
  super({:methods => [:cidr, :to_label]}.merge(options))
end

#cidrObject


75
76
77
# File 'app/models/subnet.rb', line 75

def cidr
  IPAddr.new(mask).to_i.to_s(2).count("1")
end

#contains?(ip) ⇒ Boolean

Indicates whether the IP is within this subnet

ip

String: Contains 4 dotted decimal values

Returns Boolean: True if if ip is in this subnet


71
72
73
# File 'app/models/subnet.rb', line 71

def contains? ip
  IPAddr.new("#{network}/#{mask}", Socket::AF_INET).include? IPAddr.new(ip, Socket::AF_INET)
end

#dhcp?Boolean


79
80
81
# File 'app/models/subnet.rb', line 79

def dhcp?
  !!(dhcp and dhcp.url and !dhcp.url.blank?)
end

#dhcp_proxy(attrs = {}) ⇒ Object


83
84
85
# File 'app/models/subnet.rb', line 83

def dhcp_proxy attrs = {}
  @dhcp_proxy ||= ProxyAPI::DHCP.new({:url => dhcp.url}.merge(attrs)) if dhcp?
end

#dns?Boolean

do we support DNS PTR records for this subnet


96
97
98
# File 'app/models/subnet.rb', line 96

def dns?
  !!(dns and dns.url and !dns.url.blank?)
end

#dns_proxy(attrs = {}) ⇒ Object


100
101
102
# File 'app/models/subnet.rb', line 100

def dns_proxy attrs = {}
  @dns_proxy ||= ProxyAPI::DNS.new({:url => dns.url}.merge(attrs)) if dns?
end

#tftp?Boolean


87
88
89
# File 'app/models/subnet.rb', line 87

def tftp?
  !!(tftp and tftp.url and !tftp.url.blank?)
end

#tftp_proxy(attrs = {}) ⇒ Object


91
92
93
# File 'app/models/subnet.rb', line 91

def tftp_proxy attrs = {}
  @tftp_proxy ||= ProxyAPI::TFTP.new({:url => tftp.url}.merge(attrs)) if tftp?
end

#titleObject


45
46
47
# File 'app/models/subnet.rb', line 45

def title
  "#{name} (#{to_label})"
end

#to_labelObject

Subnets are displayed in the form of their network network/network mask


41
42
43
# File 'app/models/subnet.rb', line 41

def to_label
  "#{network}/#{cidr}"
end

#unused_ip(mac = nil) ⇒ Object


104
105
106
107
108
109
110
# File 'app/models/subnet.rb', line 104

def unused_ip mac = nil
  return unless dhcp?
  dhcp_proxy.unused_ip(self, mac)["ip"]
rescue => e
  logger.warn "Failed to fetch a free IP from our proxy: #{e}"
  nil
end