1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-27 16:45:22 +00:00
webvirtcloud/vrtManager/nwfilters.py

130 lines
4.1 KiB
Python
Raw Normal View History

2018-09-21 13:50:44 +00:00
from xml.etree import ElementTree
2020-04-24 16:34:29 +00:00
from vrtManager.connection import wvmConnect
2018-09-21 13:50:44 +00:00
class wvmNWFilters(wvmConnect):
def get_nwfilter_info(self, name):
nwfilter = self.get_nwfilter(name)
xml = nwfilter.XMLDesc(0)
uuid = nwfilter.UUIDString()
return {"name": name, "uuid": uuid, "xml": xml}
2018-09-21 13:50:44 +00:00
def create_nwfilter(self, xml):
self.wvm.nwfilterDefineXML(xml)
2019-09-10 06:48:31 +00:00
def clone_nwfilter(self, name, cln_name):
2018-09-21 13:50:44 +00:00
nwfilter = self.get_nwfilter(name)
if nwfilter:
tree = ElementTree.fromstring(nwfilter.XMLDesc(0))
tree.set("name", cln_name)
uuid = tree.find("uuid")
2018-09-21 13:50:44 +00:00
tree.remove(uuid)
self.create_nwfilter(ElementTree.tostring(tree).decode())
2018-09-21 13:50:44 +00:00
class wvmNWFilter(wvmConnect):
def __init__(self, host, login, passwd, conn, nwfiltername):
wvmConnect.__init__(self, host, login, passwd, conn)
self.nwfilter = self.get_nwfilter(nwfiltername)
def _XMLDesc(self, flags):
return self.nwfilter.XMLDesc(flags)
def get_uuid(self):
return self.nwfilter.UUIDString()
def get_name(self):
return self.nwfilter.name()
def delete(self):
self.nwfilter.undefine()
def get_xml(self):
tree = ElementTree.fromstring(self._XMLDesc(0))
uuid = tree.find("uuid")
2018-09-21 13:50:44 +00:00
tree.remove(uuid)
return ElementTree.tostring(tree).decode()
2018-09-21 13:50:44 +00:00
def get_filter_refs(self):
tree = ElementTree.fromstring(self._XMLDesc(0))
return [ref.get("filter") for ref in tree.findall("./filterref")]
2018-09-21 13:50:44 +00:00
def get_rules(self):
rules = []
tree = ElementTree.fromstring(self._XMLDesc(0))
for r in tree.findall("./rule"):
rule_action = r.get("action")
rule_direction = r.get("direction")
rule_priority = r.get("priority")
rule_statematch = r.get("statematch")
2018-09-21 13:50:44 +00:00
rule_directives = r.find("./")
if rule_directives is not None:
rule_directives = ElementTree.tostring(rule_directives).decode()
2018-09-21 13:50:44 +00:00
rule_info = {
"action": rule_action,
"direction": rule_direction,
"priority": rule_priority,
"statematch": rule_statematch,
"directives": rule_directives,
2018-09-21 13:50:44 +00:00
}
rules.append(rule_info)
return rules
def delete_ref(self, name):
tree = ElementTree.fromstring(self._XMLDesc(0))
for ref in tree.findall("./filterref"):
if name == ref.get("filter"):
2018-09-21 13:50:44 +00:00
tree.remove(ref)
break
return ElementTree.tostring(tree).decode()
2018-09-21 13:50:44 +00:00
def delete_rule(self, action, direction, priority):
tree = ElementTree.fromstring(self._XMLDesc(0))
rule_tree = tree.findall(
2022-11-02 05:54:35 +00:00
"./rule[@action='%s'][@direction='%s'][@priority='%s']"
% (action, direction, priority)
)
2018-09-21 13:50:44 +00:00
if rule_tree:
tree.remove(rule_tree[0])
return ElementTree.tostring(tree).decode()
2018-09-21 13:50:44 +00:00
def add_ref(self, name):
tree = ElementTree.fromstring(self._XMLDesc(0))
element = ElementTree.Element("filterref")
element.attrib["filter"] = name
2018-09-21 13:50:44 +00:00
tree.append(element)
return ElementTree.tostring(tree).decode()
2018-09-21 13:50:44 +00:00
def add_rule(self, xml):
tree = ElementTree.fromstring(self._XMLDesc(0))
rule = ElementTree.fromstring(xml)
rule_action = rule.get("action")
rule_direction = rule.get("direction")
rule_priority = rule.get("priority")
2018-09-21 13:50:44 +00:00
rule_directives = rule.find("./")
rule_tree = tree.findall(
2022-11-02 05:54:35 +00:00
"./rule[@action='%s'][@direction='%s'][@priority='%s']"
% (rule_action, rule_direction, rule_priority)
)
2018-09-21 13:50:44 +00:00
if rule_tree:
rule_tree[0].append(rule_directives)
else:
element = ElementTree.Element("rule")
element.attrib["action"] = rule_action
element.attrib["direction"] = rule_direction
element.attrib["priority"] = rule_priority
2018-09-21 13:50:44 +00:00
element.append(rule_directives)
tree.append(element)
return ElementTree.tostring(tree).decode()