use crate::handlers::common::{ create_resource, delete_resource, get_resource, list_resources, update_resource, ListResponse, }; use crate::response::{status_deleted, ApiResponse}; use crate::validation::validate_resource; use crate::{AppState, Result}; use axum::extract::{Path, State}; use axum::response::{IntoResponse, Response}; use axum::Json; use reddwarf_core::{GroupVersionKind, Namespace, ResourceKey}; use reddwarf_storage::KeyEncoder; use std::sync::Arc; use tracing::info; /// GET /api/v1/namespaces/{name} pub async fn get_namespace( State(state): State>, Path(name): Path, ) -> Result { let gvk = GroupVersionKind::from_api_version_kind("v1", "Namespace"); let key = ResourceKey::cluster_scoped(gvk, name); let namespace: Namespace = get_resource(&state, &key).await?; Ok(ApiResponse::ok(namespace).into_response()) } /// GET /api/v1/namespaces pub async fn list_namespaces(State(state): State>) -> Result { let prefix = KeyEncoder::encode_prefix("v1", "Namespace", None); let namespaces: Vec = list_resources(&state, &prefix).await?; let response = ListResponse::new("v1".to_string(), "NamespaceList".to_string(), namespaces); Ok(ApiResponse::ok(response).into_response()) } /// POST /api/v1/namespaces pub async fn create_namespace( State(state): State>, Json(namespace): Json, ) -> Result { info!("Creating namespace"); validate_resource(&namespace)?; let created = create_resource(&state, namespace).await?; Ok(ApiResponse::created(created).into_response()) } /// PUT /api/v1/namespaces/{name} pub async fn replace_namespace( State(state): State>, Path(name): Path, Json(mut namespace): Json, ) -> Result { info!("Replacing namespace: {}", name); namespace.metadata.name = Some(name); validate_resource(&namespace)?; let updated = update_resource(&state, namespace).await?; Ok(ApiResponse::ok(updated).into_response()) } /// DELETE /api/v1/namespaces/{name} pub async fn delete_namespace( State(state): State>, Path(name): Path, ) -> Result { info!("Deleting namespace: {}", name); let gvk = GroupVersionKind::from_api_version_kind("v1", "Namespace"); let key = ResourceKey::cluster_scoped(gvk, name.clone()); delete_resource(&state, &key).await?; Ok(status_deleted(&name, "Namespace")) }