Added status to VM (entry) on creation, correctly wrap (etcd) critical section using lock
This commit is contained in:
parent
126a3415e5
commit
588c2462de
3 changed files with 15 additions and 4 deletions
12
main.py
12
main.py
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue