2019-06-24 17:14:45 +00:00
|
|
|
import etcd3
|
|
|
|
import json
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
from helper import check_otp
|
2019-06-24 10:46:06 +00:00
|
|
|
from flask import Flask
|
|
|
|
from flask_restful import Resource, Api, reqparse
|
|
|
|
from decouple import config
|
2019-06-24 12:43:48 +00:00
|
|
|
from enums import VmStatus
|
2019-06-24 17:14:45 +00:00
|
|
|
from uuid import uuid4
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
etcd_client = etcd3.client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT")))
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
createvm_argparser = reqparse.RequestParser()
|
|
|
|
createvm_argparser.add_argument("name", type=str, required=True)
|
|
|
|
createvm_argparser.add_argument("realm", type=str, required=True)
|
|
|
|
createvm_argparser.add_argument("seed", type=str, required=True)
|
|
|
|
createvm_argparser.add_argument("specs", type=dict, required=True)
|
|
|
|
|
|
|
|
|
|
|
|
class CreateVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
createvm_args = createvm_argparser.parse_args()
|
|
|
|
name, realm, seed, specs = createvm_args.name, createvm_args.realm,\
|
|
|
|
createvm_args.seed, createvm_args.specs
|
|
|
|
|
|
|
|
if check_otp(name, realm, seed) == 200:
|
|
|
|
# User is good
|
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
vm_entry = {"owner": name,
|
|
|
|
"specs": specs,
|
|
|
|
"status": VmStatus.REQUESTED_NEW.value}
|
2019-06-24 12:43:48 +00:00
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
etcd_client.put(f"/v1/vm/{uuid4().hex}", json.dumps(vm_entry))
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-06-24 12:21:18 +00:00
|
|
|
return {'message': "VM Creation Queued"}, 200
|
2019-06-24 10:46:06 +00:00
|
|
|
else:
|
|
|
|
return {'message': 'Invalid Credentials'}, 400
|
|
|
|
|
|
|
|
|
|
|
|
api.add_resource(CreateVM, '/vm/create')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|