import json import base64 import requests import logging from django.http import JsonResponse from django.views.generic import TemplateView, View logger = logging.getLogger(__name__) class DashboardView(TemplateView): template_name = "monitor/dashboard.html" class ClusterGraphDataView(View): def get(self, request, *args, **kwargs): repo_name = request.GET.get("repo", "boday_tf") consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}" # ABSOLUTER SICHERHEITSGURT: Fängt jeden Parsing-Fehler ab, damit das Dashboard NIEMALS mit 500 abstürzt try: response = requests.get(consul_url, timeout=5) if response.status_code == 404: return JsonResponse({"name": f"Warte auf korrekten Consul-Key '{repo_name}'...", "type": "Root", "children": []}) consul_json = response.json() if isinstance(consul_json, list) and len(consul_json) > 0: raw_value = consul_json[0].get("Value") else: raw_value = consul_json.get("Value") if not raw_value: return JsonResponse({"name": f"Key '{repo_name}' leer.", "type": "Root", "children": []}) # Base64 decodieren state_json = json.loads(base64.b64decode(raw_value).decode('utf-8')) # Flache Ressourcen parsen resources = state_json.get("resources", []) ingresses = [] services = {} deployments = {} for res in resources: res_type = res.get("type") res_name = res.get("name") instances = res.get("instances", []) if not instances or not isinstance(instances, list): continue attributes = instances[0].get("attributes", {}) metadata = attributes.get("metadata", [{}]) actual_name = metadata[0].get("name", res_name) if isinstance(metadata, list) else metadata.get("name", res_name) if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]: spec = attributes.get("spec", [{}]) rules = spec[0].get("rule", []) if isinstance(spec, list) else spec.get("rule", []) ingresses.append({"name": actual_name, "rules": rules}) elif res_type in ["kubernetes_service", "kubernetes_service_v1"]: services[actual_name] = {"name": actual_name, "type": "Service"} elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]: deployments[actual_name] = {"name": actual_name, "type": "Deployment"} root_node = { "name": f"Consul Storage: {repo_name}", "type": "Root", "children": [] } for ing in ingresses: ing_node = {"name": ing["name"], "type": "Ingress", "children": []} for rule in ing["rules"]: http = rule.get("http", [{}]) paths = http[0].get("path", []) if isinstance(http, list) else http.get("path", []) for path in paths: backend = path.get("backend", [{}]) service_block = backend[0].get("service", {}) if isinstance(backend, list) else backend.get("service", {}) backend_svc = service_block.get("name") if backend_svc in services: svc_node = {"name": backend_svc, "type": "Service", "children": []} for dep_name in deployments.keys(): if dep_name in backend_svc or backend_svc in dep_name: svc_node["children"].append({"name": dep_name, "type": "Deployment", "children": []}) ing_node["children"].append(svc_node) root_node["children"].append(ing_node) return JsonResponse(root_node) except Exception as e: logger.error(f"Sicherheits-Fallback gegriffen: {str(e)}") # Liefert im Fehlerfall ein valides leeres Baumdiagramm (HTTP 200 OK statt 500!) return JsonResponse({ "name": f"Boday-IT Monitor // Synchronisiere '{repo_name}'...", "type": "Root", "children": [] })