mirror of
https://github.com/ThomasGsp/HyperProxmox.git
synced 2025-03-09 15:40:18 +00:00
Add error managment
This commit is contained in:
parent
d9de9384a2
commit
898f5af57e
3 changed files with 136 additions and 85 deletions
|
@ -25,15 +25,9 @@ class Cluster:
|
||||||
def GET(self, cluster=None):
|
def GET(self, cluster=None):
|
||||||
try:
|
try:
|
||||||
if cluster:
|
if cluster:
|
||||||
result = {
|
result = core.get_cluster(cluster)
|
||||||
"result": "OK",
|
|
||||||
"value": core.get_cluster(cluster)
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
result = {
|
result = core.get_cluster()
|
||||||
"result": "OK",
|
|
||||||
"value": core.get_cluster()
|
|
||||||
}
|
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
result = {
|
result = {
|
||||||
"result": "ERROR",
|
"result": "ERROR",
|
||||||
|
@ -45,10 +39,7 @@ class Cluster:
|
||||||
def POST(self):
|
def POST(self):
|
||||||
try:
|
try:
|
||||||
data = json.loads(web.data().decode('utf-8'))
|
data = json.loads(web.data().decode('utf-8'))
|
||||||
result = {
|
result = core.insert_cluster(data)
|
||||||
"result": "OK",
|
|
||||||
"value": core.insert_cluster(data)
|
|
||||||
}
|
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
result = {
|
result = {
|
||||||
"result": "ERROR",
|
"result": "ERROR",
|
||||||
|
@ -60,10 +51,7 @@ class Cluster:
|
||||||
def PUT(self, cluster):
|
def PUT(self, cluster):
|
||||||
try:
|
try:
|
||||||
data = json.loads(web.data().decode('utf-8'))
|
data = json.loads(web.data().decode('utf-8'))
|
||||||
result = {
|
result = core.change_cluster(cluster, data)
|
||||||
"result": "OK",
|
|
||||||
"value": core.change_cluster(cluster, data)
|
|
||||||
}
|
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
result = {
|
result = {
|
||||||
"result": "ERROR",
|
"result": "ERROR",
|
||||||
|
@ -74,10 +62,7 @@ class Cluster:
|
||||||
|
|
||||||
def DELETE(self, cluster):
|
def DELETE(self, cluster):
|
||||||
try:
|
try:
|
||||||
result = {
|
result = core.delete_cluste(cluster)
|
||||||
"result": "OK",
|
|
||||||
"value": core.delete_cluste(cluster)
|
|
||||||
}
|
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
result = {
|
result = {
|
||||||
"result": "ERROR",
|
"result": "ERROR",
|
||||||
|
@ -90,17 +75,26 @@ class Cluster:
|
||||||
# Ajouter le retour d'erreur des requetes foireuses
|
# Ajouter le retour d'erreur des requetes foireuses
|
||||||
class Instance:
|
class Instance:
|
||||||
def GET(self, vmid=None, status=None):
|
def GET(self, vmid=None, status=None):
|
||||||
|
try:
|
||||||
if status:
|
if status:
|
||||||
""" GET INSTANCE STATUS """
|
""" GET INSTANCE STATUS """
|
||||||
return core.status_instance(vmid, status)
|
result = core.status_instance(vmid, status)
|
||||||
elif vmid:
|
elif vmid:
|
||||||
""" GET INSTANCE INFORMATION """
|
""" GET INSTANCE INFORMATION """
|
||||||
return core.info_instance(vmid)
|
result = core.info_instance(vmid)
|
||||||
|
except BaseException as e:
|
||||||
|
result = {
|
||||||
|
"result": "ERROR",
|
||||||
|
"type": "PYTHON - API",
|
||||||
|
"value": "{0} {1}".format("Invalid request:", e)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
def POST(self, vmid=None, status=None):
|
def POST(self, vmid=None, status=None):
|
||||||
|
try:
|
||||||
if vmid:
|
if vmid:
|
||||||
""" GET INSTANCE INFORMATION """
|
""" GET INSTANCE INFORMATION """
|
||||||
return core.status_instance(vmid, status)
|
result = core.status_instance(vmid, status)
|
||||||
else:
|
else:
|
||||||
""" CREATE NEWS INSTANCES"""
|
""" CREATE NEWS INSTANCES"""
|
||||||
count = json.loads(web.data().decode('utf-8'))["count"]
|
count = json.loads(web.data().decode('utf-8'))["count"]
|
||||||
|
@ -134,13 +128,36 @@ class Instance:
|
||||||
|
|
||||||
""" Return messages """
|
""" Return messages """
|
||||||
return ast.literal_eval(Lredis.get_message(command_id))
|
return ast.literal_eval(Lredis.get_message(command_id))
|
||||||
|
except BaseException as e:
|
||||||
|
result = {
|
||||||
|
"result": "ERROR",
|
||||||
|
"type": "PYTHON - API",
|
||||||
|
"value": "{0} {1}".format("Invalid request:", e)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
def PUT(self, vmid):
|
def PUT(self, vmid):
|
||||||
|
try:
|
||||||
data = json.loads(web.data().decode('utf-8'))
|
data = json.loads(web.data().decode('utf-8'))
|
||||||
return core.change_instance(vmid, data)
|
result = core.change_instance(vmid, data)
|
||||||
|
except BaseException as e:
|
||||||
|
result = {
|
||||||
|
"result": "ERROR",
|
||||||
|
"type": "PYTHON - API",
|
||||||
|
"value": "{0} {1}".format("Invalid request:", e)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
def DELETE(self, vmid):
|
def DELETE(self, vmid):
|
||||||
return core.delete_instance(vmid)
|
try:
|
||||||
|
result = core.delete_instance(vmid)
|
||||||
|
except BaseException as e:
|
||||||
|
result = {
|
||||||
|
"result": "ERROR",
|
||||||
|
"type": "PYTHON - API",
|
||||||
|
"value": "{0} {1}".format("Invalid request:", e)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class ThreadAPI(threading.Thread):
|
class ThreadAPI(threading.Thread):
|
||||||
|
|
|
@ -307,8 +307,8 @@ class Core:
|
||||||
"type": "PROXMOX - VALUES",
|
"type": "PROXMOX - VALUES",
|
||||||
"value": "{0} is not a valid VMID: {1}".format(vmid, ierror)
|
"value": "{0} is not a valid VMID: {1}".format(vmid, ierror)
|
||||||
}
|
}
|
||||||
|
# Voir comment return ça proprement
|
||||||
return result
|
return
|
||||||
|
|
||||||
"""
|
"""
|
||||||
#######################
|
#######################
|
||||||
|
@ -330,9 +330,17 @@ class Core:
|
||||||
data["password"] = base64.b64encode(pcrypt(data["password"], self.generalconf["keys"]["key_pvt"])["data"]).decode('utf-8')
|
data["password"] = base64.b64encode(pcrypt(data["password"], self.generalconf["keys"]["key_pvt"])["data"]).decode('utf-8')
|
||||||
new_cluster = self.mongo.insert_new_cluster(data)
|
new_cluster = self.mongo.insert_new_cluster(data)
|
||||||
else:
|
else:
|
||||||
new_cluster = {"value": "{0}".format("Duplicate entry, please delete the current cluster or update it")}
|
new_cluster = {
|
||||||
|
"value": "{0}".format("Duplicate entry, please delete the current cluster or update it")
|
||||||
|
"result": "ERROR",
|
||||||
|
"type": "PROXMOX - VALUES"
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
new_cluster = {"value": "{1} {0}".format(testdata, "Invalid or miss paramettrer")}
|
new_cluster = {
|
||||||
|
"value": "{1} {0}".format(testdata, "Invalid or miss paramettrer")
|
||||||
|
"result": "ERROR",
|
||||||
|
"type": "PROXMOX - VALUES"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return new_cluster
|
return new_cluster
|
||||||
|
|
|
@ -40,23 +40,27 @@ class Proxmox:
|
||||||
self.ticket = self.socket.post(request, params=params, verify=False, timeout=5)
|
self.ticket = self.socket.post(request, params=params, verify=False, timeout=5)
|
||||||
|
|
||||||
if self.ticket.status_code == 200:
|
if self.ticket.status_code == 200:
|
||||||
result = {"result": "OK",
|
result = {
|
||||||
|
"result": "OK",
|
||||||
"value": self.ticket.json()
|
"value": self.ticket.json()
|
||||||
}
|
}
|
||||||
self.PVEAuthCookie = {'PVEAuthCookie': self.ticket.json()['data']['ticket']}
|
self.PVEAuthCookie = {'PVEAuthCookie': self.ticket.json()['data']['ticket']}
|
||||||
self.csrf = {'CSRFPreventionToken': self.ticket.json()['data']['CSRFPreventionToken']}
|
self.csrf = {'CSRFPreventionToken': self.ticket.json()['data']['CSRFPreventionToken']}
|
||||||
else:
|
else:
|
||||||
result = {"result": "ERROR",
|
result = {
|
||||||
|
"result": "ERROR",
|
||||||
"target": "{0}".format(request),
|
"target": "{0}".format(request),
|
||||||
"type": "PROXMOX - STATUS CODE",
|
"type": "PROXMOX - STATUS CODE",
|
||||||
"value": "Error nodes informations. Bad HTTP Status code : "
|
"value": "Error nodes informations. Bad HTTP Status code : "
|
||||||
"{0} -- {1}".format(self.ticket.status_code, self.ticket.text)
|
"{0} -- {1}".format(self.ticket.status_code, self.ticket.text)
|
||||||
}
|
}
|
||||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||||
result = {"result": "ERROR",
|
result = {
|
||||||
|
"result": "ERROR",
|
||||||
"target": "{0}".format(url),
|
"target": "{0}".format(url),
|
||||||
"type": "PYTHON",
|
"type": "PYTHON",
|
||||||
"value": "Cannot get ticket session {0} ({1})".format(url, e)}
|
"value": "Cannot get ticket session {0} ({1})".format(url, e)
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -104,10 +108,18 @@ class Proxmox:
|
||||||
request = "https://{0}/api2/json/nodes/{1}/status".format(url, nodename)
|
request = "https://{0}/api2/json/nodes/{1}/status".format(url, nodename)
|
||||||
try:
|
try:
|
||||||
self.status = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json()
|
self.status = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json()
|
||||||
result = {"result": "OK"}
|
result = {
|
||||||
|
"result": "OK",
|
||||||
|
"value": self.status
|
||||||
|
|
||||||
|
}
|
||||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||||
result = {"result": "ERROR", "target": "[{3}]",
|
result = {
|
||||||
"value": "Cannot get node information for {0} ({1})".format(url, e, nodename)}
|
"result": "ERROR",
|
||||||
|
"target": "{0}".format(nodename),
|
||||||
|
"type": "PYTHON - ERROR",
|
||||||
|
"value": "Cannot get node information for {0} ({1})".format(url, e)
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -120,10 +132,17 @@ class Proxmox:
|
||||||
request = "https://{0}/api2/json/nodes/{1}/storage".format(url, nodename)
|
request = "https://{0}/api2/json/nodes/{1}/storage".format(url, nodename)
|
||||||
try:
|
try:
|
||||||
self.storage = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json()
|
self.storage = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json()
|
||||||
result = {"result": "OK"}
|
result = {
|
||||||
|
"result": "OK",
|
||||||
|
"value": self.storage
|
||||||
|
}
|
||||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||||
result = {"result": "ERROR", "target": "[{3}]",
|
result = {
|
||||||
"value": "Cannot get storage information for {0} ({1})".format(url, e, nodename)}
|
"result": "ERROR",
|
||||||
|
"type": "PYTHON - ERROR",
|
||||||
|
"target": "{0}".format(nodename),
|
||||||
|
"value": "Cannot get storage information for {0} ({1})".format(url, e)
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -137,10 +156,17 @@ class Proxmox:
|
||||||
request = "https://{0}/api2/json/nodes/{1}/storage/{2}/content".format(url, nodename, sto_id)
|
request = "https://{0}/api2/json/nodes/{1}/storage/{2}/content".format(url, nodename, sto_id)
|
||||||
try:
|
try:
|
||||||
self.disks = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json()
|
self.disks = self.socket.get(request, cookies=self.PVEAuthCookie, verify=False, timeout=5).json()
|
||||||
result = {"result": "OK"}
|
result = {
|
||||||
|
"result": "OK",
|
||||||
|
"value": self.disks
|
||||||
|
}
|
||||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||||
result = {"result": "ERROR", "target": "[{3}]",
|
result = {
|
||||||
"value": "Cannot get disks information for {0} ({1})".format(url, e, nodename)}
|
"result": "ERROR",
|
||||||
|
"type": "PYTHON - ERROR",
|
||||||
|
"target": "{0}".format(nodename),
|
||||||
|
"value": "Cannot get disks information for {0} ({1})".format(url, e)
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue