def validate_address(ip_address, network): """Determine whether an address is valid.""" if ip_address.version != network.version: msg = ("Invalid IP version - must match network version " + ip_version_to_string(network.version)) raise ValidateFail(msg) elif ip_address == network: raise ValidateFail("Cannot use network address") elif ip_address == network.broadcast: raise ValidateFail("Cannot use broadcast address") elif ip_address not in network: raise ValidateFail("Address must be in subnet %s" % str(network))
def validate_nameserver_address_str(ip_address_str, subnet_version=None): """Determine whether a nameserver address is valid.""" try: ip_address = netaddr.IPAddress(ip_address_str) if subnet_version is not None and ip_address.version != subnet_version: msg = ("Invalid IP version - must match OAM subnet version " + ip_version_to_string(subnet_version)) raise ValidateFail(msg) return ip_address except netaddr.AddrFormatError: msg = "Invalid address - not a valid %s address" % \ ip_version_to_string(subnet_version) raise ValidateFail(msg)
def check_network_overlap(new_network, configured_networks): """ Validate that new_network does not overlap any configured_networks. """ if any(new_network.ip in subnet for subnet in configured_networks): raise ValidateFail( "Subnet %s overlaps with another configured subnet" % new_network)
def validate_address_str(ip_address_str, network): """Determine whether an address is valid.""" try: ip_address = netaddr.IPAddress(ip_address_str) if ip_address.version != network.version: msg = ("Invalid IP version - must match network version " + ip_version_to_string(network.version)) raise ValidateFail(msg) elif ip_address == network: raise ValidateFail("Cannot use network address") elif ip_address == network.broadcast: raise ValidateFail("Cannot use broadcast address") elif ip_address not in network: raise ValidateFail("Address must be in subnet %s" % str(network)) return ip_address except netaddr.AddrFormatError: raise ValidateFail("Invalid address - not a valid IP address")
def get_service(conf, section, key): if key in EXPECTED_SERVICE_NAME_AND_TYPE: if conf.has_option(section, key): value = conf.get(section, key) if value != EXPECTED_SERVICE_NAME_AND_TYPE[key]: raise ValidateFail("Unsupported %s: %s " % (key, value)) else: value = EXPECTED_SERVICE_NAME_AND_TYPE[key] return value else: return conf.get(section, key)
def validate_network_str(network_str, minimum_size, existing_networks=None, multicast=False): """Determine whether a network is valid.""" try: network = netaddr.IPNetwork(network_str) if network.size < minimum_size: raise ValidateFail("Subnet too small - must have at least %d " "addresses" % minimum_size) elif network.version == 6 and network.prefixlen < 64: raise ValidateFail("IPv6 minimum prefix length is 64") elif existing_networks: if any(network.ip in subnet for subnet in existing_networks): raise ValidateFail("Subnet overlaps with another " "configured subnet") elif multicast and not network.is_multicast(): raise ValidateFail("Invalid subnet - must be multicast") return network except netaddr.AddrFormatError: raise ValidateFail( "Invalid subnet - not a valid IP subnet")