Skip to main content

kafrust_protocol/api/
create_acls.rs

1use crate::codec::{Decoder, Encoder};
2use crate::error::Result;
3use crate::header::RequestHeader;
4
5pub const API_KEY: i16 = 30;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct CreateAclsRequestV1 {
9    pub correlation_id: i32,
10    pub client_id: Option<String>,
11    pub creations: Vec<CreateAclsCreationV1>,
12}
13
14impl CreateAclsRequestV1 {
15    pub fn encode(&self) -> Result<Vec<u8>> {
16        let mut encoder = Encoder::new();
17        RequestHeader {
18            api_key: API_KEY,
19            api_version: 1,
20            correlation_id: self.correlation_id,
21            client_id: self.client_id.clone(),
22        }
23        .encode_v1(&mut encoder)?;
24        encoder.write_array(Some(&self.creations), |encoder, creation| {
25            encoder.write_i8(creation.resource_type);
26            encoder.write_string(&creation.resource_name)?;
27            encoder.write_i8(creation.resource_pattern_type);
28            encoder.write_string(&creation.principal)?;
29            encoder.write_string(&creation.host)?;
30            encoder.write_i8(creation.operation);
31            encoder.write_i8(creation.permission_type);
32            Ok(())
33        })?;
34        Ok(encoder.into_bytes())
35    }
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct CreateAclsCreationV1 {
40    pub resource_type: i8,
41    pub resource_name: String,
42    pub resource_pattern_type: i8,
43    pub principal: String,
44    pub host: String,
45    pub operation: i8,
46    pub permission_type: i8,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct CreateAclsResponseV1 {
51    pub throttle_time_ms: i32,
52    pub results: Vec<CreateAclsResultV1>,
53}
54
55impl CreateAclsResponseV1 {
56    pub fn decode_body(decoder: &mut Decoder<'_>) -> Result<Self> {
57        Ok(Self {
58            throttle_time_ms: decoder.read_i32()?,
59            results: decoder
60                .read_array("create ACL results", CreateAclsResultV1::decode)?
61                .unwrap_or_default(),
62        })
63    }
64}
65
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct CreateAclsResultV1 {
68    pub error_code: i16,
69    pub error_message: Option<String>,
70}
71
72impl CreateAclsResultV1 {
73    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
74        Ok(Self {
75            error_code: decoder.read_i16()?,
76            error_message: decoder.read_nullable_string()?,
77        })
78    }
79}
80
81#[cfg(test)]
82#[allow(clippy::unwrap_used)]
83mod tests {
84    use super::{CreateAclsCreationV1, CreateAclsRequestV1, CreateAclsResponseV1, API_KEY};
85    use crate::codec::{Decoder, Encoder};
86
87    #[test]
88    fn encodes_create_acls_v1_request() {
89        let request = CreateAclsRequestV1 {
90            correlation_id: 9,
91            client_id: None,
92            creations: vec![CreateAclsCreationV1 {
93                resource_type: 2,
94                resource_name: "orders".to_owned(),
95                resource_pattern_type: 3,
96                principal: "User:alice".to_owned(),
97                host: "*".to_owned(),
98                operation: 3,
99                permission_type: 1,
100            }],
101        };
102
103        let bytes = request.encode().unwrap();
104        assert_eq!(&bytes[0..4], &[0, API_KEY as u8, 0, 1]);
105        assert_eq!(&bytes[4..8], &[0, 0, 0, 9]);
106        assert_eq!(bytes.last(), Some(&1));
107    }
108
109    #[test]
110    fn decodes_create_acls_v1_response() {
111        let mut bytes = Encoder::new();
112        bytes.write_i32(5);
113        bytes.write_i32(2);
114        bytes.write_i16(0);
115        bytes.write_nullable_string(None).unwrap();
116        bytes.write_i16(29);
117        bytes.write_nullable_string(Some("denied")).unwrap();
118        let bytes = bytes.into_bytes();
119        let mut decoder = Decoder::new(&bytes);
120
121        let response = CreateAclsResponseV1::decode_body(&mut decoder).unwrap();
122
123        assert_eq!(response.throttle_time_ms, 5);
124        assert_eq!(response.results.len(), 2);
125        assert_eq!(response.results[0].error_code, 0);
126        assert_eq!(response.results[1].error_code, 29);
127        assert_eq!(response.results[1].error_message.as_deref(), Some("denied"));
128        assert!(decoder.is_empty());
129    }
130}