mirror of
https://github.com/retspen/webvirtcloud
synced 2025-07-31 12:41:08 +00:00
Rest framework (#24)
* Add rest framework for API: First Commit * modify some shell scripts to make variable references safer; modify some python scripts to reduce the code complexity and cyclomatic complexity of functions. * Add REST API for some webvirtcloud functions. Instance list/delete/create, compute list/delete/create, storages-network list/retrieve. Add swagger and redoc for API interface * update requirements Co-authored-by: herengui <herengui@uniontech.com>
This commit is contained in:
parent
92254401dc
commit
cfce71ec2b
42 changed files with 1170 additions and 348 deletions
|
|
@ -57,7 +57,7 @@ class wvmInterface(wvmConnect):
|
|||
try:
|
||||
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
|
||||
return util.get_xml_path(xml, "/interface/start/@mode")
|
||||
except:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def is_active(self):
|
||||
|
|
@ -65,10 +65,7 @@ class wvmInterface(wvmConnect):
|
|||
|
||||
def get_mac(self):
|
||||
mac = self.iface.MACString()
|
||||
if mac:
|
||||
return mac
|
||||
else:
|
||||
return None
|
||||
return mac or None
|
||||
|
||||
def get_type(self):
|
||||
xml = self._XMLDesc()
|
||||
|
|
@ -78,11 +75,8 @@ class wvmInterface(wvmConnect):
|
|||
try:
|
||||
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
|
||||
ipaddr = util.get_xml_path(xml, "/interface/protocol[@family='ipv4']/ip/@address")
|
||||
if ipaddr:
|
||||
return "static"
|
||||
else:
|
||||
return "dhcp"
|
||||
except:
|
||||
return "static" if ipaddr else "dhcp"
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def get_ipv4(self):
|
||||
|
|
@ -92,17 +86,14 @@ class wvmInterface(wvmConnect):
|
|||
if not int_ipv4_ip or not int_ipv4_mask:
|
||||
return None
|
||||
else:
|
||||
return int_ipv4_ip + "/" + int_ipv4_mask
|
||||
return f"{int_ipv4_ip}/{int_ipv4_mask}"
|
||||
|
||||
def get_ipv6_type(self):
|
||||
try:
|
||||
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
|
||||
ipaddr = util.get_xml_path(xml, "/interface/protocol[@family='ipv6']/ip/@address")
|
||||
if ipaddr:
|
||||
return "static"
|
||||
else:
|
||||
return "dhcp"
|
||||
except:
|
||||
return "static" if ipaddr else "dhcp"
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def get_ipv6(self):
|
||||
|
|
@ -112,40 +103,39 @@ class wvmInterface(wvmConnect):
|
|||
if not int_ipv6_ip or not int_ipv6_mask:
|
||||
return None
|
||||
else:
|
||||
return int_ipv6_ip + "/" + int_ipv6_mask
|
||||
return f"{int_ipv6_ip}/{int_ipv6_mask}"
|
||||
|
||||
def get_bridge(self):
|
||||
bridge = None
|
||||
if self.get_type() == "bridge":
|
||||
bridge = util.get_xml_path(self._XMLDesc(), "/interface/bridge/interface/@name")
|
||||
for iface in self.get_bridge_slave_ifaces():
|
||||
if iface.get("state") == "up" and iface.get("speed") != "unknown":
|
||||
bridge = iface.get("name")
|
||||
return bridge
|
||||
return bridge
|
||||
else:
|
||||
if self.get_type() != "bridge":
|
||||
return None
|
||||
bridge = util.get_xml_path(self._XMLDesc(), "/interface/bridge/interface/@name")
|
||||
for iface in self.get_bridge_slave_ifaces():
|
||||
if iface.get("state") == "up" and iface.get("speed") != "unknown":
|
||||
bridge = iface.get("name")
|
||||
return bridge
|
||||
return bridge
|
||||
|
||||
def get_bridge_slave_ifaces(self):
|
||||
ifaces = list()
|
||||
if self.get_type() == "bridge":
|
||||
tree = ElementTree.fromstring(self._XMLDesc())
|
||||
for iface in tree.findall("./bridge/"):
|
||||
address = state = speed = None
|
||||
name = iface.get("name")
|
||||
if_type = iface.get("type")
|
||||
link = iface.find("link")
|
||||
if link is not None:
|
||||
state = link.get("state")
|
||||
speed = link.get("speed")
|
||||
mac = iface.find("mac")
|
||||
if mac is not None:
|
||||
address = mac.get("address")
|
||||
ifaces.append({"name": name, "type": if_type, "state": state, "speed": speed, "mac": address})
|
||||
return ifaces
|
||||
else:
|
||||
if self.get_type() != "bridge":
|
||||
return None
|
||||
|
||||
ifaces = []
|
||||
tree = ElementTree.fromstring(self._XMLDesc())
|
||||
for iface in tree.findall("./bridge/"):
|
||||
address = state = speed = None
|
||||
name = iface.get("name")
|
||||
if_type = iface.get("type")
|
||||
link = iface.find("link")
|
||||
if link is not None:
|
||||
state = link.get("state")
|
||||
speed = link.get("speed")
|
||||
mac = iface.find("mac")
|
||||
if mac is not None:
|
||||
address = mac.get("address")
|
||||
ifaces.append({"name": name, "type": if_type, "state": state, "speed": speed, "mac": address})
|
||||
return ifaces
|
||||
|
||||
def get_details(self):
|
||||
mac = self.get_mac()
|
||||
itype = self.get_type()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue