pvc/daemon-common/network.py

626 lines
24 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
2018-09-23 15:26:20 -04:00
# network.py - PVC client function library, Network fuctions
# Part of the Parallel Virtual Cluster (PVC) system
#
2021-03-25 17:01:55 -04:00
# Copyright (C) 2018-2021 Joshua M. Boniface <joshua@boniface.me>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
###############################################################################
import re
#
# Cluster search functions
#
def getClusterNetworkList(zkhandler):
2018-09-23 15:26:20 -04:00
# Get a list of VNIs by listing the children of /networks
vni_list = zkhandler.children('/networks')
2018-09-23 15:26:20 -04:00
description_list = []
# For each VNI, get the corresponding description from the data
for vni in vni_list:
description_list.append(zkhandler.read('/networks/{}'.format(vni)))
2018-09-23 15:26:20 -04:00
return vni_list, description_list
def searchClusterByVNI(zkhandler, vni):
try:
# Get the lists
vni_list, description_list = getClusterNetworkList(zkhandler)
# We're looking for UUID, so find that element ID
2018-09-23 15:26:20 -04:00
index = vni_list.index(vni)
# Get the name_list element at that index
2018-09-23 15:26:20 -04:00
description = description_list[index]
except ValueError:
# We didn't find anything
return None
2018-09-23 15:26:20 -04:00
return description
def searchClusterByDescription(zkhandler, description):
try:
# Get the lists
vni_list, description_list = getClusterNetworkList(zkhandler)
# We're looking for name, so find that element ID
2018-09-23 15:26:20 -04:00
index = description_list.index(description)
# Get the uuid_list element at that index
2018-09-23 15:26:20 -04:00
vni = vni_list[index]
except ValueError:
# We didn't find anything
return None
2018-09-23 15:26:20 -04:00
return vni
def getNetworkVNI(zkhandler, network):
2018-09-23 15:26:20 -04:00
# Validate and obtain alternate passed value
if network.isdigit():
net_description = searchClusterByVNI(zkhandler, network)
net_vni = searchClusterByDescription(zkhandler, net_description)
2018-09-23 15:26:20 -04:00
else:
net_vni = searchClusterByDescription(zkhandler, network)
net_description = searchClusterByVNI(zkhandler, net_vni)
2018-09-23 15:26:20 -04:00
return net_vni
def getNetworkDescription(zkhandler, network):
# Validate and obtain alternate passed value
2018-09-23 15:26:20 -04:00
if network.isdigit():
net_description = searchClusterByVNI(zkhandler, network)
net_vni = searchClusterByDescription(zkhandler, net_description)
else:
net_vni = searchClusterByDescription(zkhandler, network)
net_description = searchClusterByVNI(zkhandler, net_vni)
2018-09-23 15:26:20 -04:00
return net_description
def getNetworkDHCPLeases(zkhandler, vni):
2018-11-18 00:55:04 -05:00
# Get a list of DHCP leases by listing the children of /networks/<vni>/dhcp4_leases
dhcp4_leases = zkhandler.children('/networks/{}/dhcp4_leases'.format(vni))
2018-11-18 00:55:04 -05:00
return sorted(dhcp4_leases)
def getNetworkDHCPReservations(zkhandler, vni):
2018-11-18 00:55:04 -05:00
# Get a list of DHCP reservations by listing the children of /networks/<vni>/dhcp4_reservations
dhcp4_reservations = zkhandler.children('/networks/{}/dhcp4_reservations'.format(vni))
2018-11-18 00:55:04 -05:00
return sorted(dhcp4_reservations)
def getNetworkACLs(zkhandler, vni, _direction):
2018-10-17 00:23:27 -04:00
# Get the (sorted) list of active ACLs
2018-10-17 20:05:22 -04:00
if _direction == 'both':
directions = ['in', 'out']
else:
directions = [_direction]
2018-10-17 00:23:27 -04:00
full_acl_list = []
2018-10-17 20:05:22 -04:00
for direction in directions:
unordered_acl_list = zkhandler.children('/networks/{}/firewall_rules/{}'.format(vni, direction))
ordered_acls = dict()
2018-10-17 20:05:22 -04:00
for acl in unordered_acl_list:
order = zkhandler.read('/networks/{}/firewall_rules/{}/{}/order'.format(vni, direction, acl))
2018-10-17 20:05:22 -04:00
ordered_acls[order] = acl
2018-10-17 00:23:27 -04:00
2018-10-17 20:05:22 -04:00
for order in sorted(ordered_acls.keys()):
rule = zkhandler.read('/networks/{}/firewall_rules/{}/{}/rule'.format(vni, direction, acl))
2019-12-22 21:57:18 -05:00
full_acl_list.append({'direction': direction, 'order': int(order), 'description': ordered_acls[order], 'rule': rule})
2018-10-17 00:23:27 -04:00
return full_acl_list
def getNetworkInformation(zkhandler, vni):
description = zkhandler.read('/networks/{}'.format(vni))
nettype = zkhandler.read('/networks/{}/nettype'.format(vni))
domain = zkhandler.read('/networks/{}/domain'.format(vni))
name_servers = zkhandler.read('/networks/{}/name_servers'.format(vni))
ip6_network = zkhandler.read('/networks/{}/ip6_network'.format(vni))
ip6_gateway = zkhandler.read('/networks/{}/ip6_gateway'.format(vni))
dhcp6_flag = zkhandler.read('/networks/{}/dhcp6_flag'.format(vni))
ip4_network = zkhandler.read('/networks/{}/ip4_network'.format(vni))
ip4_gateway = zkhandler.read('/networks/{}/ip4_gateway'.format(vni))
dhcp4_flag = zkhandler.read('/networks/{}/dhcp4_flag'.format(vni))
dhcp4_start = zkhandler.read('/networks/{}/dhcp4_start'.format(vni))
dhcp4_end = zkhandler.read('/networks/{}/dhcp4_end'.format(vni))
2019-07-04 23:01:22 -04:00
# Construct a data structure to represent the data
network_information = {
2019-12-22 21:57:18 -05:00
'vni': int(vni),
2019-07-04 23:01:22 -04:00
'description': description,
'type': nettype,
'domain': domain,
2019-12-08 23:32:03 -05:00
'name_servers': name_servers.split(','),
2019-07-04 23:01:22 -04:00
'ip6': {
'network': ip6_network,
'gateway': ip6_gateway,
'dhcp_flag': dhcp6_flag,
},
'ip4': {
2019-07-04 23:01:22 -04:00
'network': ip4_network,
'gateway': ip4_gateway,
'dhcp_flag': dhcp4_flag,
'dhcp_start': dhcp4_start,
'dhcp_end': dhcp4_end
}
2019-07-04 23:01:22 -04:00
}
return network_information
def getDHCPLeaseInformation(zkhandler, vni, mac_address):
# Check whether this is a dynamic or static lease
if zkhandler.exists('/networks/{}/dhcp4_leases/{}'.format(vni, mac_address)):
type_key = 'dhcp4_leases'
elif zkhandler.exists('/networks/{}/dhcp4_reservations/{}'.format(vni, mac_address)):
type_key = 'dhcp4_reservations'
else:
return {}
hostname = zkhandler.read('/networks/{}/{}/{}/hostname'.format(vni, type_key, mac_address))
ip4_address = zkhandler.read('/networks/{}/{}/{}/ipaddr'.format(vni, type_key, mac_address))
if type_key == 'dhcp4_leases':
timestamp = zkhandler.read('/networks/{}/{}/{}/expiry'.format(vni, type_key, mac_address))
else:
timestamp = 'static'
2019-07-04 23:01:22 -04:00
# Construct a data structure to represent the data
lease_information = {
'hostname': hostname,
'ip4_address': ip4_address,
'mac_address': mac_address,
'timestamp': timestamp
}
return lease_information
def getACLInformation(zkhandler, vni, direction, description):
order = zkhandler.read('/networks/{}/firewall_rules/{}/{}/order'.format(vni, direction, description))
rule = zkhandler.read('/networks/{}/firewall_rules/{}/{}/rule'.format(vni, direction, description))
2019-07-04 23:01:22 -04:00
# Construct a data structure to represent the data
acl_information = {
'order': order,
'description': description,
2019-12-22 21:57:18 -05:00
'rule': rule,
'direction': direction
2019-07-04 23:01:22 -04:00
}
return acl_information
2018-10-17 00:23:27 -04:00
def isValidMAC(macaddr):
allowed = re.compile(r"""
(
^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$
)
""",
re.VERBOSE | re.IGNORECASE)
2019-06-24 13:37:56 -04:00
if allowed.match(macaddr):
return True
2019-06-24 13:37:56 -04:00
else:
return False
def isValidIP(ipaddr):
ip4_blocks = str(ipaddr).split(".")
if len(ip4_blocks) == 4:
for block in ip4_blocks:
# Check if number is digit, if not checked before calling this function
if not block.isdigit():
return False
tmp = int(block)
if 0 > tmp > 255:
return False
return True
return False
2018-09-23 15:26:20 -04:00
#
# Direct functions
#
def add_network(zkhandler, vni, description, nettype,
2019-12-08 23:32:03 -05:00
domain, name_servers, ip4_network, ip4_gateway, ip6_network, ip6_gateway,
dhcp4_flag, dhcp4_start, dhcp4_end):
# Ensure start and end DHCP ranges are set if the flag is set
2020-11-07 12:58:54 -05:00
if dhcp4_flag and (not dhcp4_start or not dhcp4_end):
return False, 'ERROR: DHCPv4 start and end addresses are required for a DHCPv4-enabled network.'
2018-10-17 00:23:27 -04:00
# Check if a network with this VNI or description already exists
if zkhandler.exists('/networks/{}'.format(vni)):
return False, 'ERROR: A network with VNI "{}" already exists!'.format(vni)
for network in zkhandler.children('/networks'):
network_description = zkhandler.read('/networks/{}'.format(network))
2018-10-17 00:23:27 -04:00
if network_description == description:
return False, 'ERROR: A network with description "{}" already exists!'.format(description)
# We're generating the default gateway to be ip6_network::1/YY
if ip6_network:
dhcp6_flag = 'True'
if not ip6_gateway:
ip6_netpart, ip6_maskpart = ip6_network.split('/')
2018-11-14 00:19:43 -05:00
ip6_gateway = '{}1'.format(ip6_netpart)
else:
dhcp6_flag = 'False'
if nettype == 'managed' and not domain:
2019-06-24 12:59:32 -04:00
domain = '{}.local'.format(description)
2018-09-23 15:26:20 -04:00
# Add the new network to Zookeeper
zkhandler.write([
('/networks/{}'.format(vni), description),
('/networks/{}/nettype'.format(vni), nettype),
('/networks/{}/domain'.format(vni), domain),
('/networks/{}/name_servers'.format(vni), name_servers),
('/networks/{}/ip6_network'.format(vni), ip6_network),
('/networks/{}/ip6_gateway'.format(vni), ip6_gateway),
('/networks/{}/dhcp6_flag'.format(vni), dhcp6_flag),
('/networks/{}/ip4_network'.format(vni), ip4_network),
('/networks/{}/ip4_gateway'.format(vni), ip4_gateway),
('/networks/{}/dhcp4_flag'.format(vni), dhcp4_flag),
('/networks/{}/dhcp4_start'.format(vni), dhcp4_start),
('/networks/{}/dhcp4_end'.format(vni), dhcp4_end),
('/networks/{}/dhcp4_leases'.format(vni), ''),
('/networks/{}/dhcp4_reservations'.format(vni), ''),
('/networks/{}/firewall_rules'.format(vni), ''),
('/networks/{}/firewall_rules/in'.format(vni), ''),
('/networks/{}/firewall_rules/out'.format(vni), '')
])
2018-09-23 15:26:20 -04:00
return True, 'Network "{}" added successfully!'.format(description)
def modify_network(zkhandler, vni, description=None, domain=None, name_servers=None,
ip4_network=None, ip4_gateway=None, ip6_network=None, ip6_gateway=None,
dhcp4_flag=None, dhcp4_start=None, dhcp4_end=None):
# Add the modified parameters to Zookeeper
update_data = list()
if description is not None:
update_data.append(('/networks/{}'.format(vni), description))
if domain is not None:
update_data.append(('/networks/{}/domain'.format(vni), domain))
if name_servers is not None:
update_data.append(('/networks/{}/name_servers'.format(vni), name_servers))
if ip4_network is not None:
update_data.append(('/networks/{}/ip4_network'.format(vni), ip4_network))
if ip4_gateway is not None:
update_data.append(('/networks/{}/ip4_gateway'.format(vni), ip4_gateway))
if ip6_network is not None:
update_data.append(('/networks/{}/ip6_network'.format(vni), ip6_network))
if ip6_network:
update_data.append(('/networks/{}/dhcp6_flag'.format(vni), 'True'))
else:
update_data.append(('/networks/{}/dhcp6_flag'.format(vni), 'False'))
if ip6_gateway is not None:
update_data.append(('/networks/{}/ip6_gateway'.format(vni), ip6_gateway))
2018-11-14 00:19:43 -05:00
else:
# If we're changing the network, but don't also specify the gateway,
# generate a new one automatically
if ip6_network:
ip6_netpart, ip6_maskpart = ip6_network.split('/')
2018-11-14 00:19:43 -05:00
ip6_gateway = '{}1'.format(ip6_netpart)
update_data.append(('/networks/{}/ip6_gateway'.format(vni), ip6_gateway))
if dhcp4_flag is not None:
update_data.append(('/networks/{}/dhcp4_flag'.format(vni), dhcp4_flag))
if dhcp4_start is not None:
update_data.append(('/networks/{}/dhcp4_start'.format(vni), dhcp4_start))
if dhcp4_end is not None:
update_data.append(('/networks/{}/dhcp4_end'.format(vni), dhcp4_end))
zkhandler.write(update_data)
2019-03-15 11:28:49 -04:00
2018-09-23 15:26:20 -04:00
return True, 'Network "{}" modified successfully!'.format(vni)
def remove_network(zkhandler, network):
# Validate and obtain alternate passed value
vni = getNetworkVNI(zkhandler, network)
description = getNetworkDescription(zkhandler, network)
2018-09-23 15:26:20 -04:00
if not vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
2018-09-23 15:26:20 -04:00
# Delete the configuration
zkhandler.delete('/networks/{}'.format(vni))
2018-09-23 15:26:20 -04:00
return True, 'Network "{}" removed successfully!'.format(description)
def add_dhcp_reservation(zkhandler, network, ipaddress, macaddress, hostname):
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Use lowercase MAC format exclusively
macaddress = macaddress.lower()
if not isValidMAC(macaddress):
return False, 'ERROR: MAC address "{}" is not valid! Always use ":" as a separator.'.format(macaddress)
if not isValidIP(ipaddress):
return False, 'ERROR: IP address "{}" is not valid!'.format(macaddress)
if zkhandler.exists('/networks/{}/dhcp4_reservations/{}'.format(net_vni, macaddress)):
2018-09-30 11:22:25 -04:00
return False, 'ERROR: A reservation with MAC "{}" already exists!'.format(macaddress)
# Add the new static lease to ZK
try:
zkhandler.write([
('/networks/{}/dhcp4_reservations/{}'.format(net_vni, macaddress), 'static'),
('/networks/{}/dhcp4_reservations/{}/hostname'.format(net_vni, macaddress), hostname),
('/networks/{}/dhcp4_reservations/{}/ipaddr'.format(net_vni, macaddress), ipaddress)
])
except Exception as e:
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
return True, 'DHCP reservation "{}" added successfully!'.format(macaddress)
def remove_dhcp_reservation(zkhandler, network, reservation):
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
match_description = ''
# Check if the reservation matches a static reservation description, a mac, or an IP address currently in the database
dhcp4_reservations_list = getNetworkDHCPReservations(zkhandler, net_vni)
2018-11-18 00:55:04 -05:00
for macaddr in dhcp4_reservations_list:
hostname = zkhandler.read('/networks/{}/dhcp4_reservations/{}/hostname'.format(net_vni, macaddr))
ipaddress = zkhandler.read('/networks/{}/dhcp4_reservations/{}/ipaddr'.format(net_vni, macaddr))
2018-10-03 23:53:33 -04:00
if reservation == macaddr or reservation == hostname or reservation == ipaddress:
2018-09-30 11:22:25 -04:00
match_description = macaddr
lease_type_zk = 'reservations'
lease_type_human = 'static reservation'
# Check if the reservation matches a dynamic reservation description, a mac, or an IP address currently in the database
dhcp4_leases_list = getNetworkDHCPLeases(zkhandler, net_vni)
for macaddr in dhcp4_leases_list:
hostname = zkhandler.read('/networks/{}/dhcp4_leases/{}/hostname'.format(net_vni, macaddr))
ipaddress = zkhandler.read('/networks/{}/dhcp4_leases/{}/ipaddr'.format(net_vni, macaddr))
if reservation == macaddr or reservation == hostname or reservation == ipaddress:
match_description = macaddr
lease_type_zk = 'leases'
lease_type_human = 'dynamic lease'
2019-03-15 11:28:49 -04:00
if not match_description:
return False, 'ERROR: No DHCP reservation or lease exists matching "{}"!'.format(reservation)
# Remove the entry from zookeeper
zkhandler.delete('/networks/{}/dhcp4_{}/{}'.format(net_vni, lease_type_zk, match_description))
return True, 'DHCP {} "{}" removed successfully!'.format(lease_type_human, match_description)
def add_acl(zkhandler, network, direction, description, rule, order):
2018-10-17 00:23:27 -04:00
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
2018-10-17 00:23:27 -04:00
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Check if the ACL matches a description currently in the database
2019-12-29 16:19:33 -05:00
match_description = ''
full_acl_list = getNetworkACLs(zkhandler, net_vni, 'both')
for acl in full_acl_list:
if acl['description'] == description:
match_description = acl['description']
if match_description:
return False, 'ERROR: A rule with description "{}" already exists!'.format(description)
2018-10-17 00:23:27 -04:00
# Change direction to something more usable
if direction:
direction = "in"
2018-10-17 00:23:27 -04:00
else:
direction = "out"
# Handle reordering
full_acl_list = getNetworkACLs(zkhandler, net_vni, direction)
2018-10-17 00:23:27 -04:00
acl_list_length = len(full_acl_list)
# Set order to len
2019-06-24 13:37:56 -04:00
if not order or int(order) > acl_list_length:
2018-10-17 00:23:27 -04:00
order = acl_list_length
# Convert passed-in order to an integer
else:
order = int(order)
2019-03-15 11:28:49 -04:00
2018-10-17 00:23:27 -04:00
# Insert into the array at order-1
2019-12-22 21:57:18 -05:00
full_acl_list.insert(order, {'direction': direction, 'description': description, 'rule': rule})
2018-10-17 00:23:27 -04:00
# Update the existing ordering
for idx, acl in enumerate(full_acl_list):
2018-10-17 20:05:22 -04:00
if acl['description'] == description:
2018-10-17 00:23:27 -04:00
continue
if idx == acl['order']:
continue
else:
zkhandler.write([
('/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, acl['description']), idx)
])
2018-10-17 00:23:27 -04:00
# Add the new rule
zkhandler.write([
('/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, description), ''),
('/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, description), order),
('/networks/{}/firewall_rules/{}/{}/rule'.format(net_vni, direction, description), rule)
])
2018-10-17 00:23:27 -04:00
return True, 'Firewall rule "{}" added successfully!'.format(description)
def remove_acl(zkhandler, network, description):
2018-10-17 00:23:27 -04:00
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
2018-10-17 00:23:27 -04:00
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
match_description = ''
# Check if the ACL matches a description currently in the database
acl_list = getNetworkACLs(zkhandler, net_vni, 'both')
2018-10-17 00:23:27 -04:00
for acl in acl_list:
2019-07-05 21:38:47 -04:00
if acl['description'] == description:
2018-10-17 20:05:22 -04:00
match_description = acl['description']
2019-12-22 21:57:18 -05:00
match_direction = acl['direction']
2019-03-15 11:28:49 -04:00
2018-10-17 00:23:27 -04:00
if not match_description:
2019-07-05 21:38:47 -04:00
return False, 'ERROR: No firewall rule exists matching description "{}"!'.format(description)
2018-10-17 00:23:27 -04:00
# Remove the entry from zookeeper
try:
zkhandler.delete('/networks/{}/firewall_rules/{}/{}'.format(net_vni, match_direction, match_description))
2018-10-17 00:23:27 -04:00
except Exception as e:
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
# Update the existing ordering
updated_acl_list = getNetworkACLs(zkhandler, net_vni, match_direction)
2018-10-17 20:05:22 -04:00
for idx, acl in enumerate(updated_acl_list):
if acl['description'] == description:
continue
2018-10-17 00:23:27 -04:00
if idx == acl['order']:
continue
else:
zkhandler.write([
('/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, match_direction, acl['description']), idx)
])
2018-10-17 00:23:27 -04:00
return True, 'Firewall rule "{}" removed successfully!'.format(match_description)
def get_info(zkhandler, network):
# Validate and obtain alternate passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
2018-09-23 15:26:20 -04:00
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
network_information = getNetworkInformation(zkhandler, network)
2019-07-04 23:01:22 -04:00
if not network_information:
return False, 'ERROR: Could not get information about network "{}"'.format(network)
2019-07-04 23:01:22 -04:00
return True, network_information
def get_list(zkhandler, limit, is_fuzzy=True):
2018-09-25 01:47:29 -04:00
net_list = []
full_net_list = zkhandler.children('/networks')
2018-09-25 02:20:32 -04:00
2018-09-25 01:47:29 -04:00
for net in full_net_list:
description = zkhandler.read('/networks/{}'.format(net))
2019-06-24 13:37:56 -04:00
if limit:
2018-09-25 01:47:29 -04:00
try:
2019-12-22 21:57:18 -05:00
if not is_fuzzy:
limit = '^' + limit + '$'
2018-09-25 02:20:32 -04:00
2019-06-24 13:37:56 -04:00
if re.match(limit, net):
net_list.append(getNetworkInformation(zkhandler, net))
2019-06-24 13:37:56 -04:00
if re.match(limit, description):
net_list.append(getNetworkInformation(zkhandler, net))
2018-09-25 01:47:29 -04:00
except Exception as e:
return False, 'Regex Error: {}'.format(e)
2018-09-25 02:20:32 -04:00
else:
net_list.append(getNetworkInformation(zkhandler, net))
2019-07-04 23:01:22 -04:00
return True, net_list
2018-09-25 13:58:52 -04:00
def get_list_dhcp(zkhandler, network, limit, only_static=False, is_fuzzy=True):
# Validate and obtain alternate passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
dhcp_list = []
if only_static:
full_dhcp_list = getNetworkDHCPReservations(zkhandler, net_vni)
else:
full_dhcp_list = getNetworkDHCPReservations(zkhandler, net_vni)
full_dhcp_list += getNetworkDHCPLeases(zkhandler, net_vni)
2019-12-22 21:57:18 -05:00
if limit:
try:
2019-12-22 21:57:18 -05:00
if not is_fuzzy:
limit = '^' + limit + '$'
# Implcitly assume fuzzy limits
if not re.match(r'\^.*', limit):
limit = '.*' + limit
if not re.match(r'.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
2019-03-15 11:28:49 -04:00
for lease in full_dhcp_list:
valid_lease = False
if limit:
2019-06-24 13:37:56 -04:00
if re.match(limit, lease):
valid_lease = True
2019-06-24 13:37:56 -04:00
if re.match(limit, lease):
2018-10-02 00:14:08 -04:00
valid_lease = True
else:
valid_lease = True
if valid_lease:
dhcp_list.append(getDHCPLeaseInformation(zkhandler, net_vni, lease))
2019-07-04 23:01:22 -04:00
return True, dhcp_list
def get_list_acl(zkhandler, network, limit, direction, is_fuzzy=True):
# Validate and obtain alternate passed value
net_vni = getNetworkVNI(zkhandler, network)
2019-06-24 13:37:56 -04:00
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
2018-10-17 00:23:27 -04:00
# Change direction to something more usable
2018-10-17 20:05:22 -04:00
if direction is None:
direction = "both"
elif direction is True:
2018-10-17 00:23:27 -04:00
direction = "in"
2018-10-17 20:05:22 -04:00
elif direction is False:
2018-10-17 00:23:27 -04:00
direction = "out"
acl_list = []
full_acl_list = getNetworkACLs(zkhandler, net_vni, direction)
2018-10-17 00:23:27 -04:00
2019-12-22 21:57:18 -05:00
if limit:
2018-10-17 00:23:27 -04:00
try:
2019-12-22 21:57:18 -05:00
if not is_fuzzy:
limit = '^' + limit + '$'
2018-10-17 00:23:27 -04:00
# Implcitly assume fuzzy limits
if not re.match(r'\^.*', limit):
2018-10-17 00:23:27 -04:00
limit = '.*' + limit
if not re.match(r'.*\$', limit):
2018-10-17 00:23:27 -04:00
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
2018-10-17 20:05:22 -04:00
for acl in full_acl_list:
2018-10-17 00:23:27 -04:00
valid_acl = False
if limit:
2019-06-24 13:37:56 -04:00
if re.match(limit, acl['description']):
2018-10-17 00:23:27 -04:00
valid_acl = True
else:
valid_acl = True
if valid_acl:
acl_list.append(acl)
2019-07-04 23:01:22 -04:00
return True, acl_list