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
parent 126a3415e5
commit 588c2462de
3 changed files with 15 additions and 4 deletions

6
enums.py Normal file
View File

@ -0,0 +1,6 @@
import enum
class VmStatus(enum):
REQUESTED_NEW = "REQUESTED_NEW"
SCHEDULED_DEPLOY = "SCHEDULED_DEPLOY"

View File

@ -5,6 +5,7 @@ from decouple import config
from pyotp import TOTP
from etcd import EtcdKeyNotFound
def check_otp(name, realm, seed):
try:
data = {

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