From 0253aa9afc7cf70d6f0bd54da458dd4706eb0120 Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Khalid Date: Mon, 1 Jul 2019 23:53:38 +0500 Subject: [PATCH] create_image view added + etcd3_wrapper module added --- .gitmodules | 3 +++ etcd3_wrapper | 1 + main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 .gitmodules create mode 160000 etcd3_wrapper diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..316b9ea --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "etcd3_wrapper"] + path = etcd3_wrapper + url = https://code.ungleich.ch/ahmedbilal/etcd3_wrapper diff --git a/etcd3_wrapper b/etcd3_wrapper new file mode 160000 index 0000000..59974e2 --- /dev/null +++ b/etcd3_wrapper @@ -0,0 +1 @@ +Subproject commit 59974e2047bbea08b72d32129fd905bbea6e9d70 diff --git a/main.py b/main.py index 93fe362..19ca6cb 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,6 @@ +# TODO +# convert etcd3 usage to etcd3_wrapper + import etcd3 import json @@ -6,11 +9,13 @@ from flask import Flask from flask_restful import Resource, Api, reqparse from decouple import config from uuid import uuid4 +from etcd3_wrapper import Etcd3Wrapper app = Flask(__name__) api = Api(app) etcd_client = etcd3.client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT"))) +client = Etcd3Wrapper() createvm_argparser = reqparse.RequestParser() createvm_argparser.add_argument("name", type=str, required=True) @@ -27,6 +32,11 @@ deletevm_argparser.add_argument("vmid", type=str, required=True) vmstatus_argparser = reqparse.RequestParser() vmstatus_argparser.add_argument("id", type=str, required=True) +createimage_argparser = reqparse.RequestParser() +createimage_argparser.add_argument("uuid", type=str, required=True) +createimage_argparser.add_argument("name", type=str, required=True) +createimage_argparser.add_argument("image_store", type=str, required=True) + class CreateVM(Resource): def post(self): @@ -82,9 +92,41 @@ class VmStatus(Resource): return {"Message": "Not Found"} +class CreateImage(Resource): + def post(self): + image_stores = list(client.get_prefix("/v1/image_store/")) + args = createimage_argparser.parse_args() + image_file_uuid = args.uuid + image_store_name = args.image_store + + file_entry = client.get(f"/v1/files/{image_file_uuid}") + if file_entry is None: + return {"Message": "Image File Not Found"}, 400 + + file_entry_value = json.loads(file_entry.value) + + image_store = list(filter(lambda s: json.loads(s.value)["name"] == image_store_name, image_stores)) + if not image_store: + return {"Message": f"Store '{image_store_name}' does not exists"}, 400 + + image_store = image_store[0] + image_entry_json = { + "status": "TO_BE_CREATED", + "owner": file_entry_value["owner"], + "filename": file_entry_value["filename"], + "name": args.name, + "store_name": image_store_name + } + client.put(f"/v1/image/{image_file_uuid}", json.dumps(image_entry_json)) + + return {"Message": "Image successfully created"} + + api.add_resource(CreateVM, "/vm/create") api.add_resource(DeleteVM, "/vm/delete") api.add_resource(VmStatus, "/vm/status") +api.add_resource(CreateImage, "/image/create") + if __name__ == "__main__": app.run(host="::", debug=True)