tansu_storage/service/describe_cluster.rs
1// Copyright ⓒ 2024-2025 Peter Morgan <peter.james.morgan@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use rama::{Context, Service};
16use tansu_sans_io::{ApiKey, DescribeClusterRequest, DescribeClusterResponse, ErrorCode};
17use tracing::{debug, instrument};
18
19use crate::{Error, Result, Storage};
20
21/// A [`Service`] using [`Storage`] as [`Context`] taking [`DescribeClusterRequest`] returning [`DescribeClusterResponse`].
22/// ```
23/// use rama::{Context, Layer, Service as _, layer::MapStateLayer};
24/// use tansu_sans_io::{DescribeClusterRequest, EndpointType, ErrorCode};
25/// use tansu_storage::{DescribeClusterService, Error, StorageContainer};
26/// use url::Url;
27///
28/// # #[tokio::main]
29/// # async fn main() -> Result<(), Error> {
30/// const HOST: &str = "localhost";
31/// const PORT: i32 = 9092;
32/// const NODE_ID: i32 = 111;
33///
34/// let storage = StorageContainer::builder()
35/// .cluster_id("tansu")
36/// .node_id(NODE_ID)
37/// .advertised_listener(Url::parse(&format!("tcp://{HOST}:{PORT}"))?)
38/// .storage(Url::parse("memory://tansu/")?)
39/// .build()
40/// .await?;
41///
42/// let service = MapStateLayer::new(|_| storage).into_layer(DescribeClusterService);
43///
44/// let response = service
45/// .serve(
46/// Context::default(),
47/// DescribeClusterRequest::default()
48/// .endpoint_type(Some(EndpointType::Broker.into()))
49/// .include_cluster_authorized_operations(false),
50/// )
51/// .await?;
52///
53/// let brokers = response.brokers.unwrap_or_default();
54/// assert_eq!(1, brokers.len());
55/// assert_eq!(NODE_ID, brokers[0].broker_id);
56/// assert_eq!(HOST, brokers[0].host.as_str());
57/// assert_eq!(PORT, brokers[0].port);
58/// # Ok(())
59/// # }
60/// ```
61#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
62pub struct DescribeClusterService;
63
64impl ApiKey for DescribeClusterService {
65 const KEY: i16 = DescribeClusterRequest::KEY;
66}
67
68impl<G> Service<G, DescribeClusterRequest> for DescribeClusterService
69where
70 G: Storage,
71{
72 type Response = DescribeClusterResponse;
73 type Error = Error;
74
75 #[instrument(skip(ctx, req))]
76 async fn serve(
77 &self,
78 ctx: Context<G>,
79 req: DescribeClusterRequest,
80 ) -> Result<Self::Response, Self::Error> {
81 let brokers = ctx.state().brokers().await?;
82 debug!(?brokers);
83
84 let cluster_id = ctx.state().cluster_id().await?;
85
86 Ok(DescribeClusterResponse::default()
87 .throttle_time_ms(0)
88 .error_code(ErrorCode::None.into())
89 .error_message(None)
90 .endpoint_type(req.endpoint_type)
91 .controller_id(brokers.first().map(|broker| broker.broker_id).unwrap_or(-1))
92 .cluster_id(cluster_id.to_owned())
93 .brokers(Some(brokers))
94 .cluster_authorized_operations(-2_147_483_648))
95 }
96}