diff --git a/enums.py b/enums.py new file mode 100644 index 0000000..20e1f67 --- /dev/null +++ b/enums.py @@ -0,0 +1,6 @@ +import enum + + +class VmStatus(enum): + REQUESTED_NEW = "REQUESTED_NEW" + SCHEDULED_DEPLOY = "SCHEDULED_DEPLOY" diff --git a/helper.py b/helper.py index deaf313..41ea877 100644 --- a/helper.py +++ b/helper.py @@ -5,6 +5,7 @@ from decouple import config from pyotp import TOTP from etcd import EtcdKeyNotFound + def check_otp(name, realm, seed): try: data = { diff --git a/main.py b/main.py index 55f1be0..2298351 100644 --- a/main.py +++ b/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