Added status to VM (entry) on creation, correctly wrap (etcd) critical section using lock

This commit is contained in:
ahmadbilalkhalid 2019-06-24 17:43:48 +05:00
commit 588c2462de
3 changed files with 15 additions and 4 deletions

12
main.py
View file

@ -4,6 +4,7 @@ from helper import check_otp, get_next_id
from flask import Flask
from flask_restful import Resource, Api, reqparse
from decouple import config
from enums import VmStatus
app = Flask(__name__)
api = Api(app)
@ -26,12 +27,15 @@ class CreateVM(Resource):
if check_otp(name, realm, seed) == 200:
# User is good
next_vm_id = get_next_id(etcd_client, "/v1/vm/")
vm_entry = {"owner": name,
"specs": specs}
# Block until lock is acquired. There is no timeout associated with this lock.
with etcd_lock:
next_vm_id = get_next_id(etcd_client, "/v1/vm/")
vm_entry = {"owner": name,
"specs": specs,
"status": VmStatus.REQUESTED_NEW}
etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry)
return {'message': "VM Creation Queued"}, 200