use crate::codec::{Decoder, Encoder};
use crate::error::Result;
use crate::header::RequestHeader;
pub const API_KEY: i16 = 74;
pub const TOPIC_RESOURCE_TYPE: i8 = 2;
pub const BROKER_RESOURCE_TYPE: i8 = 4;
pub const BROKER_LOGGER_RESOURCE_TYPE: i8 = 8;
pub const CLIENT_METRICS_RESOURCE_TYPE: i8 = 16;
pub const GROUP_RESOURCE_TYPE: i8 = 32;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListConfigResourcesRequestV0 {
pub correlation_id: i32,
pub client_id: Option<String>,
}
impl ListConfigResourcesRequestV0 {
pub fn encode(&self) -> Result<Vec<u8>> {
let mut encoder = Encoder::new();
RequestHeader {
api_key: API_KEY,
api_version: 0,
correlation_id: self.correlation_id,
client_id: self.client_id.clone(),
}
.encode_v2(&mut encoder)?;
encoder.write_empty_tagged_fields();
Ok(encoder.into_bytes())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListConfigResourcesResponseV0 {
pub throttle_time_ms: i32,
pub error_code: i16,
pub resources: Vec<ListedClientMetricsResourceV0>,
}
impl ListConfigResourcesResponseV0 {
pub fn decode_body(decoder: &mut Decoder<'_>) -> Result<Self> {
let throttle_time_ms = decoder.read_i32()?;
let error_code = decoder.read_i16()?;
let resources = decoder
.read_compact_array("list client metrics resources", |decoder| {
let name = decoder.read_compact_string()?;
decoder.read_tagged_fields()?;
Ok(ListedClientMetricsResourceV0 { name })
})?
.unwrap_or_default();
decoder.read_tagged_fields()?;
Ok(Self {
throttle_time_ms,
error_code,
resources,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListedClientMetricsResourceV0 {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListConfigResourcesRequestV1 {
pub correlation_id: i32,
pub client_id: Option<String>,
pub resource_types: Vec<i8>,
}
impl ListConfigResourcesRequestV1 {
pub fn encode(&self) -> Result<Vec<u8>> {
let mut encoder = Encoder::new();
RequestHeader {
api_key: API_KEY,
api_version: 1,
correlation_id: self.correlation_id,
client_id: self.client_id.clone(),
}
.encode_v2(&mut encoder)?;
encoder.write_compact_array(Some(&self.resource_types), |encoder, resource_type| {
encoder.write_i8(*resource_type);
Ok(())
})?;
encoder.write_empty_tagged_fields();
Ok(encoder.into_bytes())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListConfigResourcesResponseV1 {
pub throttle_time_ms: i32,
pub error_code: i16,
pub resources: Vec<ListedConfigResourceV1>,
}
impl ListConfigResourcesResponseV1 {
pub fn decode_body(decoder: &mut Decoder<'_>) -> Result<Self> {
let throttle_time_ms = decoder.read_i32()?;
let error_code = decoder.read_i16()?;
let resources = decoder
.read_compact_array("list config resources", |decoder| {
let resource_name = decoder.read_compact_string()?;
let resource_type = decoder.read_i8()?;
decoder.read_tagged_fields()?;
Ok(ListedConfigResourceV1 {
resource_name,
resource_type,
})
})?
.unwrap_or_default();
decoder.read_tagged_fields()?;
Ok(Self {
throttle_time_ms,
error_code,
resources,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListedConfigResourceV1 {
pub resource_name: String,
pub resource_type: i8,
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::{
ListConfigResourcesRequestV0, ListConfigResourcesRequestV1, ListConfigResourcesResponseV0,
ListConfigResourcesResponseV1, API_KEY, BROKER_RESOURCE_TYPE, GROUP_RESOURCE_TYPE,
TOPIC_RESOURCE_TYPE,
};
use crate::codec::{Decoder, Encoder};
#[test]
fn encodes_list_config_resources_v1_request() {
let request = ListConfigResourcesRequestV1 {
correlation_id: 12,
client_id: Some("kafrust".to_owned()),
resource_types: vec![TOPIC_RESOURCE_TYPE, GROUP_RESOURCE_TYPE],
};
assert_eq!(
request.encode().unwrap(),
[
0, 74, 0, 1, 0, 0, 0, 12, 0, 7, b'k', b'a', b'f', b'r', b'u', b's', b't', 0, 3, 2, 32, 0, ]
);
assert_eq!(API_KEY, 74);
}
#[test]
fn encodes_list_client_metrics_resources_v0_request() {
let request = ListConfigResourcesRequestV0 {
correlation_id: 13,
client_id: None,
};
assert_eq!(
request.encode().unwrap(),
[
0, 74, 0, 0, 0, 0, 0, 13, 0xff, 0xff, 0, 0, ]
);
}
#[test]
fn decodes_list_config_resources_v1_response() {
let mut body = Encoder::new();
body.write_i32(9);
body.write_i16(0);
body.write_compact_array(
Some(&["orders".to_owned(), "payments".to_owned()]),
|encoder, name| {
encoder.write_compact_string(name)?;
encoder.write_i8(if name == "orders" {
TOPIC_RESOURCE_TYPE
} else {
BROKER_RESOURCE_TYPE
});
encoder.write_empty_tagged_fields();
Ok(())
},
)
.unwrap();
body.write_empty_tagged_fields();
let bytes = body.into_bytes();
let mut decoder = Decoder::new(&bytes);
let response = ListConfigResourcesResponseV1::decode_body(&mut decoder).unwrap();
assert_eq!(response.throttle_time_ms, 9);
assert_eq!(response.error_code, 0);
assert_eq!(response.resources.len(), 2);
assert_eq!(response.resources[0].resource_name, "orders");
assert_eq!(response.resources[0].resource_type, TOPIC_RESOURCE_TYPE);
assert_eq!(response.resources[1].resource_type, BROKER_RESOURCE_TYPE);
assert!(decoder.is_empty());
}
#[test]
fn decodes_list_client_metrics_resources_v0_response() {
let mut body = Encoder::new();
body.write_i32(5);
body.write_i16(0);
body.write_compact_array(Some(&["latency", "throughput"]), |encoder, name| {
encoder.write_compact_string(name)?;
encoder.write_empty_tagged_fields();
Ok(())
})
.unwrap();
body.write_empty_tagged_fields();
let bytes = body.into_bytes();
let mut decoder = Decoder::new(&bytes);
let response = ListConfigResourcesResponseV0::decode_body(&mut decoder).unwrap();
assert_eq!(response.throttle_time_ms, 5);
assert_eq!(response.error_code, 0);
assert_eq!(response.resources.len(), 2);
assert_eq!(response.resources[0].name, "latency");
assert_eq!(response.resources[1].name, "throughput");
assert!(decoder.is_empty());
}
}