-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (67 loc) · 2.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
import requests
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@app.get("/")
def read_root():
response = RedirectResponse(url="/docs")
return response
API_ENDPOINT = "https://api.vultr.com/v2/"
API_KEY = os.getenv("VULTR_API_KEY")
HEADERS = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
def format_response(data, error=None):
return {"data": data, "error": error}
@app.delete("/delete_cluster/{vke_id}")
async def delete_cluster(vke_id: str):
response = requests.delete(
f"{API_ENDPOINT}kubernetes/clusters/{vke_id}/delete-with-linked-resources",
headers=HEADERS,
)
if response.status_code == 204:
return {"message": "Cluster and all related resources deleted successfully"}
else:
raise HTTPException(status_code=response.status_code, detail=response.text)
@app.get("/list_clusters/")
async def list_clusters():
endpoint = f"{API_ENDPOINT}kubernetes/clusters"
response = requests.get(endpoint, headers=HEADERS)
if response.status_code == 200:
return format_response(response.json())
else:
return format_response(None, response.text)
@app.post("/create_clusters/")
async def create_cluster():
endpoint = f"{API_ENDPOINT}kubernetes/clusters"
payload = {
"region": "lax",
"label": "my-label",
"version": "v1.27.7+2",
"node_pools": [
{
"node_quantity": 1,
"plan": "vc2-4c-8gb",
"label": "my-label",
"auto_scaler": True,
"min_nodes": 1,
"max_nodes": 5,
}
],
}
response = requests.post(endpoint, headers=HEADERS, json=payload)
if response.status_code == 201:
return {
"message": "Successfully created Kubernetes cluster",
"data": response.json(),
}
else:
raise HTTPException(status_code=response.status_code, detail=response.text)