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
				
			
		
							
								
								
									
										6
									
								
								enums.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								enums.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					import enum
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class VmStatus(enum):
 | 
				
			||||||
 | 
					    REQUESTED_NEW = "REQUESTED_NEW"
 | 
				
			||||||
 | 
					    SCHEDULED_DEPLOY = "SCHEDULED_DEPLOY"
 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,7 @@ from decouple import config
 | 
				
			||||||
from pyotp import TOTP
 | 
					from pyotp import TOTP
 | 
				
			||||||
from etcd import EtcdKeyNotFound
 | 
					from etcd import EtcdKeyNotFound
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def check_otp(name, realm, seed):
 | 
					def check_otp(name, realm, seed):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        data = {
 | 
					        data = {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										12
									
								
								main.py
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								main.py
									
										
									
									
									
								
							| 
						 | 
					@ -4,6 +4,7 @@ from helper import check_otp, get_next_id
 | 
				
			||||||
from flask import Flask
 | 
					from flask import Flask
 | 
				
			||||||
from flask_restful import Resource, Api, reqparse
 | 
					from flask_restful import Resource, Api, reqparse
 | 
				
			||||||
from decouple import config
 | 
					from decouple import config
 | 
				
			||||||
 | 
					from enums import VmStatus
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app = Flask(__name__)
 | 
					app = Flask(__name__)
 | 
				
			||||||
api = Api(app)
 | 
					api = Api(app)
 | 
				
			||||||
| 
						 | 
					@ -26,12 +27,15 @@ class CreateVM(Resource):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if check_otp(name, realm, seed) == 200:
 | 
					        if check_otp(name, realm, seed) == 200:
 | 
				
			||||||
            # User is good
 | 
					            # 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:
 | 
					            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)
 | 
					                etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return {'message': "VM Creation Queued"}, 200
 | 
					            return {'message': "VM Creation Queued"}, 200
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue