race condition removed + return message on success updated

This commit is contained in:
ahmadbilalkhalid 2019-06-24 17:21:18 +05:00
parent 55dfda523b
commit 126a3415e5

View file

@ -9,6 +9,7 @@ app = Flask(__name__)
api = Api(app) api = Api(app)
etcd_client = etcd.Client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT"))) etcd_client = etcd.Client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT")))
etcd_lock = etcd.Lock(etcd_client, 'etcd_lock')
createvm_argparser = reqparse.RequestParser() createvm_argparser = reqparse.RequestParser()
createvm_argparser.add_argument("name", type=str, required=True) createvm_argparser.add_argument("name", type=str, required=True)
@ -30,9 +31,10 @@ class CreateVM(Resource):
vm_entry = {"owner": name, vm_entry = {"owner": name,
"specs": specs} "specs": specs}
etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry) with etcd_lock:
etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry)
return {'message': "VM Created"}, 200 return {'message': "VM Creation Queued"}, 200
else: else:
return {'message': 'Invalid Credentials'}, 400 return {'message': 'Invalid Credentials'}, 400