Commit e7fe60fd authored by Baptiste Jonglez's avatar Baptiste Jonglez
Browse files

[schema] Add validators for IPv4 and IPv6 networks

parent 98020c12
Pipeline #268864 passed with stages
in 6 minutes and 22 seconds
...@@ -43,6 +43,29 @@ class HashValidator::Validator::Ipv4AddressValidator < HashValidator::Validator: ...@@ -43,6 +43,29 @@ class HashValidator::Validator::Ipv4AddressValidator < HashValidator::Validator:
end end
end end
class HashValidator::Validator::Ipv4NetworkValidator < HashValidator::Validator::Base
def initialize
super('ipv4_network')
end
def validate(key, values, _validations, errors)
if not values.is_a?(String)
errors[key] = "IPv4 network should be a String"
elsif not values.include?('/')
errors[key] = "IPv4 network should contain a '/' character"
else
address = values.split("/")[0]
prefix_length = Integer(values.split("/")[1]) rescue nil
if (not prefix_length.is_a?(Integer)) || prefix_length < 0 || prefix_length > 32
errors[key] = "Invalid IPv4 prefix length for network #{values}"
elsif not address =~ Resolv::IPv4::Regex
errors[key] = "Invalid IPv4 network format #{values}"
end
end
end
end
class HashValidator::Validator::Ipv6AddressValidator < HashValidator::Validator::Base class HashValidator::Validator::Ipv6AddressValidator < HashValidator::Validator::Base
def initialize def initialize
...@@ -60,6 +83,31 @@ class HashValidator::Validator::Ipv6AddressValidator < HashValidator::Validator: ...@@ -60,6 +83,31 @@ class HashValidator::Validator::Ipv6AddressValidator < HashValidator::Validator:
end end
end end
class HashValidator::Validator::Ipv6NetworkValidator < HashValidator::Validator::Base
def initialize
super('ipv6_network')
end
def validate(key, values, _validations, errors)
if not values.is_a?(String)
errors[key] = "IPv6 network should be a String"
elsif not values.include?('/')
errors[key] = "IPv6 network should contain a '/' character"
else
address = values.split("/")[0]
prefix_length = Integer(values.split("/")[1]) rescue nil
if (not prefix_length.is_a?(Integer)) || prefix_length < 0 || prefix_length > 128
errors[key] = "Invalid IPv6 prefix length for network #{values}"
elsif not address =~ Resolv::IPv6::Regex
errors[key] = "Invalid IPv6 network format #{values}"
end
end
end
end
HashValidator.append_validator(HashValidator::Validator::LinecardPortValidator.new) HashValidator.append_validator(HashValidator::Validator::LinecardPortValidator.new)
HashValidator.append_validator(HashValidator::Validator::Ipv4AddressValidator.new) HashValidator.append_validator(HashValidator::Validator::Ipv4AddressValidator.new)
HashValidator.append_validator(HashValidator::Validator::Ipv4NetworkValidator.new)
HashValidator.append_validator(HashValidator::Validator::Ipv6AddressValidator.new) HashValidator.append_validator(HashValidator::Validator::Ipv6AddressValidator.new)
HashValidator.append_validator(HashValidator::Validator::Ipv6NetworkValidator.new)
...@@ -39,7 +39,7 @@ sys_admin_contact: email ...@@ -39,7 +39,7 @@ sys_admin_contact: email
security_contact: email security_contact: email
user_support_contact: email user_support_contact: email
compilation_server: boolean compilation_server: boolean
virt_ip_range: string virt_ip_range: ipv4_network
production: boolean production: boolean
laptops: laptops:
<optional_hash>: <optional_hash>:
...@@ -51,15 +51,15 @@ laptops: ...@@ -51,15 +51,15 @@ laptops:
ip: ipv4_address ip: ipv4_address
mac: optional_string mac: optional_string
g5ksubnet: g5ksubnet:
network: string network: ipv4_network
gateway: ipv4_address gateway: ipv4_address
kavlans: kavlans:
<multi>: <multi>:
network: string network: ipv4_network
gateway: ipv4_address gateway: ipv4_address
network_ipv6: string network_ipv6: ipv6_network
gateway_ipv6: optional_ipv6_address gateway_ipv6: optional_ipv6_address
kavlan_ip_range: string kavlan_ip_range: ipv4_network
kavlan_topo: optional_string kavlan_topo: optional_string
kavlan_nef: optional_integer kavlan_nef: optional_integer
pdus: pdus:
...@@ -77,6 +77,6 @@ pdus: ...@@ -77,6 +77,6 @@ pdus:
renater_ip: ipv4_address renater_ip: ipv4_address
frontend_ip: ipv4_address frontend_ip: ipv4_address
ipv6: ipv6:
prefix: string prefix: ipv6_network
site_global_kavlan: integer site_global_kavlan: integer
site_index: integer site_index: integer
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment