DeleteVM endpoint added
This commit is contained in:
parent
272bbcd5c6
commit
a65a9b243c
1 changed files with 35 additions and 5 deletions
40
main.py
40
main.py
|
@ -18,6 +18,12 @@ createvm_argparser.add_argument("realm", type=str, required=True)
|
||||||
createvm_argparser.add_argument("seed", type=str, required=True)
|
createvm_argparser.add_argument("seed", type=str, required=True)
|
||||||
createvm_argparser.add_argument("specs", type=dict, required=True)
|
createvm_argparser.add_argument("specs", type=dict, required=True)
|
||||||
|
|
||||||
|
deletevm_argparser = reqparse.RequestParser()
|
||||||
|
deletevm_argparser.add_argument("name", type=str, required=True)
|
||||||
|
deletevm_argparser.add_argument("realm", type=str, required=True)
|
||||||
|
deletevm_argparser.add_argument("seed", type=str, required=True)
|
||||||
|
deletevm_argparser.add_argument("vmid", type=str, required=True)
|
||||||
|
|
||||||
vmstatus_argparser = reqparse.RequestParser()
|
vmstatus_argparser = reqparse.RequestParser()
|
||||||
vmstatus_argparser.add_argument("id", type=str, required=True)
|
vmstatus_argparser.add_argument("id", type=str, required=True)
|
||||||
|
|
||||||
|
@ -38,9 +44,32 @@ class CreateVM(Resource):
|
||||||
|
|
||||||
etcd_client.put(f"/v1/vm/{uuid4().hex}", json.dumps(vm_entry))
|
etcd_client.put(f"/v1/vm/{uuid4().hex}", json.dumps(vm_entry))
|
||||||
|
|
||||||
return {'message': "VM Creation Queued"}, 200
|
return {"message": "VM Creation Queued"}, 200
|
||||||
else:
|
else:
|
||||||
return {'message': 'Invalid Credentials'}, 400
|
return {"message": "Invalid Credentials"}, 400
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteVM(Resource):
|
||||||
|
def post(self):
|
||||||
|
deletevm_args = deletevm_argparser.parse_args()
|
||||||
|
name, realm, seed, vmid = deletevm_args.name, deletevm_args.realm,\
|
||||||
|
deletevm_args.seed, deletevm_args.vmid
|
||||||
|
|
||||||
|
if check_otp(name, realm, seed) == 200:
|
||||||
|
# User is good
|
||||||
|
|
||||||
|
vmentry_etcd = etcd_client.get(f"/v1/vm/{vmid}")[0]
|
||||||
|
if vmentry_etcd:
|
||||||
|
vmentry_etcd = json.loads(vmentry_etcd)
|
||||||
|
vmentry_etcd["status"] = "REQUEST_DELETE"
|
||||||
|
|
||||||
|
etcd_client.put(f"/v1/vm/{vmid}", json.dumps(vmentry_etcd))
|
||||||
|
|
||||||
|
return {"message": "VM Deletion Queued"}, 200
|
||||||
|
else:
|
||||||
|
return {"message": "Invalid VM ID"}
|
||||||
|
else:
|
||||||
|
return {"message": "Invalid Credentials"}, 400
|
||||||
|
|
||||||
|
|
||||||
class VmStatus(Resource):
|
class VmStatus(Resource):
|
||||||
|
@ -53,8 +82,9 @@ class VmStatus(Resource):
|
||||||
return {"Message": "Not Found"}
|
return {"Message": "Not Found"}
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(CreateVM, '/vm/create')
|
api.add_resource(CreateVM, "/vm/create")
|
||||||
api.add_resource(VmStatus, '/vm/status')
|
api.add_resource(DeleteVM, "/vm/delete")
|
||||||
|
api.add_resource(VmStatus, "/vm/status")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
Loading…
Reference in a new issue