Shutdown VM, ListUserFiles added. Able to parse units included in specs
This commit is contained in:
parent
993fbbc3ac
commit
68f9bebccb
4 changed files with 115 additions and 35 deletions
49
main.py
49
main.py
|
|
@ -1,6 +1,5 @@
|
|||
# TODO
|
||||
# convert etcd3 usage to etcd3_wrapper
|
||||
|
||||
import etcd3
|
||||
import json
|
||||
|
||||
|
|
@ -10,6 +9,7 @@ from flask_restful import Resource, Api, reqparse
|
|||
from decouple import config
|
||||
from uuid import uuid4
|
||||
from etcd3_wrapper import Etcd3Wrapper
|
||||
from specs_parser import SpecsParser
|
||||
|
||||
app = Flask(__name__)
|
||||
api = Api(app)
|
||||
|
|
@ -45,6 +45,9 @@ add_vmid_args(add_otp_args(startvm_argparser))
|
|||
uservm_argparser = reqparse.RequestParser()
|
||||
add_otp_args(uservm_argparser)
|
||||
|
||||
# Specs parser
|
||||
specs_parser = SpecsParser(exceptional_devices=["cpu"])
|
||||
|
||||
|
||||
def is_image_valid(image_uuid):
|
||||
images = client.get_prefix("/v1/image/")
|
||||
|
|
@ -61,6 +64,10 @@ class CreateVM(Resource):
|
|||
if check_otp(name, realm, token) == 200:
|
||||
# User is good
|
||||
if is_image_valid(image_uuid):
|
||||
if not specs_parser.transform_specs(specs):
|
||||
return {"message": f"""Invalid unit - Please use following units {specs_parser.get_allowed_units()}"""}, 400
|
||||
|
||||
print(specs)
|
||||
vm_key = f"/v1/vm/{uuid4().hex}"
|
||||
vm_entry = {"owner": name,
|
||||
"specs": specs,
|
||||
|
|
@ -205,6 +212,23 @@ class ResumeVM(Resource):
|
|||
return {"message": "Invalid Credentials"}, 400
|
||||
|
||||
|
||||
class ShutdownVM(Resource):
|
||||
def post(self):
|
||||
args = startvm_argparser.parse_args()
|
||||
name, realm, token, vm_uuid = args.name, args.realm, args.token, args.vmid
|
||||
|
||||
if check_otp(name, realm, token) == 200:
|
||||
vm = client.get(f"/v1/vm/{vm_uuid}", value_in_json=True)
|
||||
if vm:
|
||||
vm.value["status"] = "REQUESTED_SHUTDOWN"
|
||||
client.put(vm.key, json.dumps(vm.value))
|
||||
return {"message": f"VM Shutdown Queued"}
|
||||
else:
|
||||
return {"message": "No such VM found"}
|
||||
else:
|
||||
return {"message": "Invalid Credentials"}, 400
|
||||
|
||||
|
||||
class ListUserVM(Resource):
|
||||
def get(self):
|
||||
args = uservm_argparser.parse_args()
|
||||
|
|
@ -228,17 +252,40 @@ class ListUserVM(Resource):
|
|||
return {"message": "Invalid Credentials"}, 400
|
||||
|
||||
|
||||
class ListUserFiles(Resource):
|
||||
def get(self):
|
||||
args = uservm_argparser.parse_args()
|
||||
name, realm, token = args.name, args.realm, args.token
|
||||
|
||||
if check_otp(name, realm, token) == 200:
|
||||
files = client.get_prefix(f"/v1/files/", value_in_json=True)
|
||||
if files:
|
||||
return_files = []
|
||||
user_files = list(filter(lambda f: f.value["owner"] == name, files))
|
||||
for file in user_files:
|
||||
return_files.append({
|
||||
"filename": vm.value["filename"]
|
||||
})
|
||||
return {"message": return_files}, 200
|
||||
else:
|
||||
return {"message": "No File found"}, 404
|
||||
else:
|
||||
return {"message": "Invalid Credentials"}, 400
|
||||
|
||||
api.add_resource(CreateVM, "/vm/create")
|
||||
api.add_resource(DeleteVM, "/vm/delete")
|
||||
api.add_resource(VmStatus, "/vm/status")
|
||||
|
||||
api.add_resource(StartVM, "/vm/start")
|
||||
api.add_resource(SuspendVM, "/vm/suspend")
|
||||
api.add_resource(ResumeVM, "/vm/resume")
|
||||
api.add_resource(ShutdownVM, "/vm/shutdown")
|
||||
|
||||
api.add_resource(CreateImage, "/image/create")
|
||||
api.add_resource(ListPublicImages, "/image/list-public")
|
||||
|
||||
api.add_resource(ListUserVM, "/user/vms")
|
||||
api.add_resource(ListUserFiles, "/user/files")
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="::", debug=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue