Skip to main content

mz_environmentd/http/
catalog.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Catalog introspection HTTP endpoints.
11
12use axum::Json;
13use axum::response::IntoResponse;
14use axum_extra::TypedHeader;
15use headers::ContentType;
16use http::StatusCode;
17use mz_adapter::catalog::InjectedAuditEvent;
18
19use crate::http::AuthedClient;
20
21pub async fn handle_catalog_dump(client: AuthedClient) -> impl IntoResponse {
22    match client.client.dump_catalog().await.map(|c| c.into_string()) {
23        Ok(res) => Ok((TypedHeader(ContentType::json()), res)),
24        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
25    }
26}
27
28pub async fn handle_catalog_check(client: AuthedClient) -> impl IntoResponse {
29    let response = match client.client.check_catalog().await {
30        Ok(_) => serde_json::Value::String("".to_string()),
31        Err(inconsistencies) => serde_json::json!({ "err": inconsistencies }),
32    };
33    (TypedHeader(ContentType::json()), response.to_string())
34}
35
36pub async fn handle_coordinator_check(client: AuthedClient) -> impl IntoResponse {
37    let response = match client.client.check_coordinator().await {
38        Ok(_) => serde_json::Value::String("".to_string()),
39        Err(inconsistencies) => serde_json::json!({ "err": inconsistencies }),
40    };
41    (TypedHeader(ContentType::json()), response.to_string())
42}
43
44pub async fn handle_inject_audit_events(
45    mut client: AuthedClient,
46    Json(events): Json<Vec<InjectedAuditEvent>>,
47) -> impl IntoResponse {
48    match client.client.inject_audit_events(events).await {
49        Ok(()) => Ok(StatusCode::OK),
50        Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
51    }
52}
53
54pub async fn handle_coordinator_dump(client: AuthedClient) -> impl IntoResponse {
55    let (status, result) = match client.client.dump_coordinator_state().await {
56        Ok(dump) => (StatusCode::OK, dump),
57        Err(e) => (
58            StatusCode::INTERNAL_SERVER_ERROR,
59            serde_json::json!({ "err": e.to_string() }),
60        ),
61    };
62    (status, TypedHeader(ContentType::json()), result.to_string())
63}