tansu_storage/service/describe_configs.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, ConfigResource, DescribeConfigsRequest, DescribeConfigsResponse};
17use tracing::{error, instrument};
18
19use crate::{Error, Result, Storage};
20
21/// A [`Service`] using [`Storage`] as [`Context`] taking [`DescribeConfigsRequest`] returning [`DescribeConfigsResponse`].
22/// ```
23/// use rama::{Context, Layer, Service as _, layer::MapStateLayer};
24/// use tansu_sans_io::{ConfigResource, DescribeConfigsRequest,
25/// EndpointType, ErrorCode, describe_configs_request::DescribeConfigsResource};
26/// use tansu_storage::{DescribeConfigsService, Error, StorageContainer};
27/// use url::Url;
28///
29/// # #[tokio::main]
30/// # async fn main() -> Result<(), Error> {
31/// let storage = StorageContainer::builder()
32/// .cluster_id("tansu")
33/// .node_id(111)
34/// .advertised_listener(Url::parse("tcp://localhost:9092")?)
35/// .storage(Url::parse("memory://tansu/")?)
36/// .build()
37/// .await?;
38///
39/// let service = MapStateLayer::new(|_| storage).into_layer(DescribeConfigsService);
40///
41/// let response = service
42/// .serve(
43/// Context::default(),
44/// DescribeConfigsRequest::default()
45/// .include_documentation(Some(false))
46/// .include_synonyms(Some(false))
47/// .resources(Some(
48/// [DescribeConfigsResource::default()
49/// .resource_name("abcba".into())
50/// .resource_type(ConfigResource::Topic.into())
51/// .configuration_keys(Some([].into()))]
52/// .into(),
53/// )),
54/// )
55/// .await?;
56///
57/// let results = response.results.unwrap_or_default();
58/// assert_eq!(1, results.len());
59/// assert_eq!(ErrorCode::None, ErrorCode::try_from(results[0].error_code)?);
60/// assert!(results[0].configs.as_deref().unwrap_or_default().is_empty());
61/// # Ok(())
62/// # }
63/// ```
64#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
65pub struct DescribeConfigsService;
66
67impl ApiKey for DescribeConfigsService {
68 const KEY: i16 = DescribeConfigsRequest::KEY;
69}
70
71impl<G> Service<G, DescribeConfigsRequest> for DescribeConfigsService
72where
73 G: Storage,
74{
75 type Response = DescribeConfigsResponse;
76 type Error = Error;
77
78 #[instrument(skip(ctx, req))]
79 async fn serve(
80 &self,
81 ctx: Context<G>,
82 req: DescribeConfigsRequest,
83 ) -> Result<Self::Response, Self::Error> {
84 let mut results = vec![];
85
86 for resource in req.resources.unwrap_or_default() {
87 results.push(
88 ctx.state()
89 .describe_config(
90 resource.resource_name.as_str(),
91 ConfigResource::from(resource.resource_type),
92 resource.configuration_keys.as_deref(),
93 )
94 .await
95 .inspect_err(|err| error!(?err))?,
96 );
97 }
98
99 Ok(DescribeConfigsResponse::default().results(Some(results)))
100 }
101}