Commit c5840072 by Karsa Zoltán István

rp create and delete

parent 86a187cb
......@@ -11,6 +11,7 @@ from fastapi import HTTPException
import requests
import json
from core.models import ResourcePoolSchema
from sredis.models import ResourcePool
import logging
logger = logging.getLogger(__name__)
......@@ -97,14 +98,14 @@ def create_rp(username: str, rp: ResourcePoolSchema):
for dc, cnt in rp.manual.items():
for _ in range(cnt):
try:
create_vm_from_template(
body = create_vm_from_template(
dc,
username,
name=f"{rp.rpname} #{id}",
template_name=rp.from_template,
)
save_rpvm(rp.rpname, username, dc, id, rp.key)
save_rpvm(rp.rpname, username, dc, body["id"], rp.key)
id += 1
except Exception:
......@@ -119,14 +120,14 @@ def create_rp(username: str, rp: ResourcePoolSchema):
cnt = int(frac * ram / details["ram_size"])
for _ in range(cnt):
try:
create_vm_from_template(
body = create_vm_from_template(
dc,
username,
name=f"{rp.rpname} #{id}",
template_name=rp.from_template,
)
save_rpvm(rp.rpname, username, dc, id, rp.key)
save_rpvm(rp.rpname, username, dc, body["id"], rp.key)
id += 1
except Exception:
......@@ -135,6 +136,22 @@ def create_rp(username: str, rp: ResourcePoolSchema):
return id, errors
def delete_rp(username: str, rpname: str):
rpvm = ResourcePool.find(ResourcePool.rpname == rpname).first()
while rpvm:
if rpvm.owner != username:
raise HTTPException(
tatus_code=401,
detail=f"Cannot delete this resourcepool",
)
proxy_datacenters(f"dashboard/acpi/vm/{rpvm.id}/",
username,
"DELETE",
datacenter=rpvm.datacenter)
rpvm.delete()
rpvm = ResourcePool.find(ResourcePool.rpname == rpname).first()
def check_template_details_from_name(template_name, username):
dcs = get_all_datacenter()
......
from fastapi import FastAPI, Response, Body, Depends
import os
from typing import List
from balancer.util import get_vm_from_rp, proxy_datacenters, create_rp
from balancer.util import delete_rp, get_vm_from_rp, proxy_datacenters, create_rp
from sredis.sredis import (
check_user,
create_puser,
......@@ -97,10 +97,16 @@ def proxy_post_ff(
@app.post("/createrp/")
def create_resource_pool(
username=Depends(get_current_user), rp: ResourcePoolSchema = Body(...)
username=Depends(admin_user), rp: ResourcePoolSchema = Body(...)
):
create_rp(username, rp)
@app.delete("/deleterp/")
def create_resource_pool(
username=Depends(admin_user), rpname: str = ""
):
delete_rp(username, rpname)
@app.post("/rp/")
def get_vm_from_pool(
......
......@@ -243,4 +243,4 @@ def pop_vm(rpname, key):
vm = rpvm.datacenter, rpvm.id
rpvm.delete()
return vm
return None
return None
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment