Module: Proxy::Ipam::IpamValidator

Includes:
IpamHelper, Validations
Included in:
Api, Netbox::NetboxClient, Phpipam::PhpipamClient
Defined in:
lib/smart_proxy_ipam/ipam_validator.rb

Overview

Module containing validation methods for use by all External IPAM provider implementations

Constant Summary

Constants included from IpamHelper

Proxy::Ipam::IpamHelper::ERRORS, Proxy::Ipam::IpamHelper::MAX_IP_RETRIES

Instance Method Summary collapse

Methods included from IpamHelper

#cache_next_ip, #find_new_ip, #get_request_group, #increment_ip, #provider, #usable_ip

Instance Method Details

#validate_cidr!(address, prefix) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/smart_proxy_ipam/ipam_validator.rb', line 24

def validate_cidr!(address, prefix)
  cidr = "#{address}/#{prefix}"
  network = IPAddr.new(cidr).to_s
  if IPAddr.new(cidr).to_s != IPAddr.new(address).to_s
    raise Proxy::Validations::Error, "Network address #{address} should be #{network} with prefix #{prefix}"
  end
  cidr
rescue IPAddr::Error => e
  raise Proxy::Validations::Error, e.to_s
end

#validate_ip!(ip) ⇒ Object

Raises:

  • (Proxy::Validations::Error)


18
19
20
21
22
# File 'lib/smart_proxy_ipam/ipam_validator.rb', line 18

def validate_ip!(ip)
  good_ip = ip =~ Regexp.union([Resolv::IPv4::Regex, Resolv::IPv6::Regex])
  raise Proxy::Validations::Error, ERRORS[:bad_ip] if good_ip.nil?
  ip
end

#validate_ip_in_cidr!(ip, cidr) ⇒ Object



35
36
37
38
39
# File 'lib/smart_proxy_ipam/ipam_validator.rb', line 35

def validate_ip_in_cidr!(ip, cidr)
  unless IPAddr.new(cidr).include?(IPAddr.new(ip))
    raise Proxy::Validations::Error.new, "IP #{ip} is not in #{cidr}"
  end
end

#validate_mac!(mac) ⇒ Object



41
42
43
44
45
46
# File 'lib/smart_proxy_ipam/ipam_validator.rb', line 41

def validate_mac!(mac)
  unless mac.match(/^([0-9a-fA-F]{2}[:]){5}[0-9a-fA-F]{2}$/i)
    raise Proxy::Validations::Error.new, ERRORS[:bad_mac]
  end
  mac
end

#validate_required_params!(required_params, params) ⇒ Object

Raises:

  • (Proxy::Validations::Error)


8
9
10
11
12
13
14
15
16
# File 'lib/smart_proxy_ipam/ipam_validator.rb', line 8

def validate_required_params!(required_params, params)
  err = []
  required_params.each do |param|
    unless params[param.to_sym]
      err.push errors[param.to_sym]
    end
  end
  raise Proxy::Validations::Error, err unless err.empty?
end