44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
|
import etcd
|
||
|
|
||
|
from helper import check_otp, get_next_id
|
||
|
from flask import Flask
|
||
|
from flask_restful import Resource, Api, reqparse
|
||
|
from decouple import config
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
api = Api(app)
|
||
|
|
||
|
etcd_client = etcd.Client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT")))
|
||
|
|
||
|
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
|
||
|
next_vm_id = get_next_id(etcd_client, "/v1/vm/")
|
||
|
|
||
|
vm_entry = {"owner": name,
|
||
|
"specs": specs}
|
||
|
|
||
|
etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry)
|
||
|
|
||
|
return {'message': "VM Created"}, 200
|
||
|
else:
|
||
|
return {'message': 'Invalid Credentials'}, 400
|
||
|
|
||
|
|
||
|
api.add_resource(CreateVM, '/vm/create')
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(debug=True)
|