From b9e4fdbf7a44f37d1acbf34cf701ba29c4ac659f Mon Sep 17 00:00:00 2001 From: Aleksandr Tcitlionok <803797+terghalin@users.noreply.github.com> Date: Thu, 5 Dec 2024 06:46:54 +0000 Subject: [PATCH] fix(export): use content type for YAML export --- app/routes/export.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/app/routes/export.py b/app/routes/export.py index 52e3713..30b19ad 100644 --- a/app/routes/export.py +++ b/app/routes/export.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Response from database import fetch_all import yaml import json @@ -9,30 +9,26 @@ router = APIRouter() def fetch_k8s_data(): config.load_incluster_config() v1 = client.CoreV1Api() - # Nodes - nodes = v1.list_node() - node_data = [{ + nodes = [{ "node_name": node.metadata.name, "cpu": node.status.capacity.get("cpu"), "memory": node.status.capacity.get("memory"), - "pods_allocatable": node.status.allocatable.get("pods") - } for node in nodes.items] + "pods_allocatable": node.status.allocatable.get("pods"), + } for node in v1.list_node().items] - # Namespaces namespaces = [ns.metadata.name for ns in v1.list_namespace().items] - - return {"nodes": node_data, "namespaces": namespaces} + return {"nodes": nodes, "namespaces": namespaces} @router.get("/export") def export_data(format: str = "yaml"): - # Fetch database and Kubernetes data data = { "metal_nodes": fetch_all("metal_nodes"), "virtual_machines": fetch_all("virtual_machines"), "kubernetes": fetch_k8s_data(), } - # Return in the requested format if format.lower() == "yaml": - return yaml.safe_dump(data) + yaml_data = yaml.safe_dump(data, sort_keys=False) + return Response(content=yaml_data, media_type="text/yaml") + return json.dumps(data, indent=2)