create_image view added + etcd3_wrapper module added

This commit is contained in:
ahmadbilalkhalid 2019-07-01 23:53:38 +05:00
parent 78d6fc2105
commit 0253aa9afc
3 changed files with 46 additions and 0 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "etcd3_wrapper"]
path = etcd3_wrapper
url = https://code.ungleich.ch/ahmedbilal/etcd3_wrapper

1
etcd3_wrapper Submodule

@ -0,0 +1 @@
Subproject commit 59974e2047bbea08b72d32129fd905bbea6e9d70

42
main.py
View file

@ -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)