endpoint (/vm/status) created to dump vm status

This commit is contained in:
ahmadbilalkhalid 2019-06-25 15:07:23 +05:00
parent 7781a1b68b
commit 77741f363d

15
main.py
View file

@ -19,6 +19,9 @@ createvm_argparser.add_argument("realm", type=str, required=True)
createvm_argparser.add_argument("seed", type=str, required=True)
createvm_argparser.add_argument("specs", type=dict, required=True)
vmstatus_argparser = reqparse.RequestParser()
vmstatus_argparser.add_argument("id", type=str, required=True)
class CreateVM(Resource):
def post(self):
@ -31,6 +34,7 @@ class CreateVM(Resource):
vm_entry = {"owner": name,
"specs": specs,
"hostname": "",
"status": VmStatus.REQUESTED_NEW.value}
etcd_client.put(f"/v1/vm/{uuid4().hex}", json.dumps(vm_entry))
@ -40,7 +44,18 @@ class CreateVM(Resource):
return {'message': 'Invalid Credentials'}, 400
class VmStatus(Resource):
def get(self):
args = vmstatus_argparser.parse_args()
r = etcd_client.get(f"/v1/vm/{args.id}")[0]
if r:
r = dict(json.loads(r.decode("utf-8")))
return r
return {"Message": "Not Found"}
api.add_resource(CreateVM, '/vm/create')
api.add_resource(VmStatus, '/vm/status')
if __name__ == '__main__':
app.run(debug=True)