diff --git a/main.py b/main.py index 3e06b30..5e8fe3e 100644 --- a/main.py +++ b/main.py @@ -202,6 +202,71 @@ async def auth_me(request: Request): """Returns current user information.""" return JSONResponse(request.session.get('user')) +@app.get('/admin') +@require_auth +async def admin_panel(request: Request): + return FileResponse("static/admin.html") + +@app.post('/admin/create-customer') +@require_auth +async def create_customer(request: Request): + import subprocess, shlex + data = await request.json() + customer_name = data.get("customer_name", "").strip() + project_id = data.get("project_id", "").strip() + billing_account_id = data.get("billing_account_id", "").strip() + alert_email = data.get("alert_email", "").strip() + container_image = data.get("container_image", "").strip() + region = data.get("region", "europe-north1").strip() + + if not all([customer_name, project_id, billing_account_id, alert_email, container_image]): + raise HTTPException(status_code=400, detail="Alle felt er påkrevd") + + import re + if not re.match(r'^[a-z0-9_-]+$', customer_name): + raise HTTPException(status_code=400, detail="customer_name kan kun inneholde a-z, 0-9, - og _") + + customer_dir = f"infrastructure/terraform/customers/{customer_name}" + template_dir = "infrastructure/terraform/customers/_template" + + import os, shutil + if os.path.exists(customer_dir): + raise HTTPException(status_code=409, detail=f"Kunde {customer_name} eksisterer allerede") + + shutil.copytree(template_dir, customer_dir) + + tfvars_content = f'''customer_id = "{customer_name}" +project_id = "{project_id}" +region = "{region}" +billing_account_id = "{billing_account_id}" +alert_email = "{alert_email}" +billing_viewer_emails = ["chris.christiansen@vauco.no", "jason.vauger@vauco.no"] +container_image = "{container_image}" +''' + with open(f"{customer_dir}/terraform.tfvars", "w") as f: + f.write(tfvars_content) + + try: + init = subprocess.run( + ["terraform", f"-chdir={customer_dir}", "init", "-no-color"], + capture_output=True, text=True, timeout=120 + ) + if init.returncode != 0: + shutil.rmtree(customer_dir) + raise HTTPException(status_code=500, detail=f"terraform init feilet: {init.stderr[-500:]}") + + apply = subprocess.run( + ["terraform", f"-chdir={customer_dir}", "apply", "-auto-approve", "-no-color"], + capture_output=True, text=True, timeout=600 + ) + if apply.returncode != 0: + raise HTTPException(status_code=500, detail=f"terraform apply feilet: {apply.stderr[-500:]}") + + return {"status": "ok", "customer": customer_name, "project_id": project_id} + + except subprocess.TimeoutExpired: + raise HTTPException(status_code=504, detail="Terraform tok for lang tid (>10 min)") + # ── BILLING ENDPOINTS (CG1 + CG2) ──────────────────────────────────────────── diff --git a/static/admin.html b/static/admin.html new file mode 100644 index 0000000..b8407de --- /dev/null +++ b/static/admin.html @@ -0,0 +1,172 @@ + + + + + + OSVauco Admin - Ny kunde + + + +
+

OSVauco Admin — Ny kunde

+
+ + + + + + + + + + + + + + + + + + +

+ ⚠️ Dette oppretter et nytt GCP-prosjekt og koster penger. Bekreft at + alle verdier er riktige før du trykker. +

+ +
+
+
+
+ + + +