Skip to main content

kafrust_protocol/api/
delete_acls.rs

1use crate::codec::{Decoder, Encoder};
2use crate::error::Result;
3use crate::header::RequestHeader;
4
5pub const API_KEY: i16 = 31;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct DeleteAclsRequestV1 {
9    pub correlation_id: i32,
10    pub client_id: Option<String>,
11    pub filters: Vec<DeleteAclsFilterV1>,
12}
13
14impl DeleteAclsRequestV1 {
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.filters), |encoder, filter| {
25            encoder.write_i8(filter.resource_type_filter);
26            encoder.write_nullable_string(filter.resource_name_filter.as_deref())?;
27            encoder.write_i8(filter.pattern_type_filter);
28            encoder.write_nullable_string(filter.principal_filter.as_deref())?;
29            encoder.write_nullable_string(filter.host_filter.as_deref())?;
30            encoder.write_i8(filter.operation);
31            encoder.write_i8(filter.permission_type);
32            Ok(())
33        })?;
34        Ok(encoder.into_bytes())
35    }
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct DeleteAclsFilterV1 {
40    pub resource_type_filter: i8,
41    pub resource_name_filter: Option<String>,
42    pub pattern_type_filter: i8,
43    pub principal_filter: Option<String>,
44    pub host_filter: Option<String>,
45    pub operation: i8,
46    pub permission_type: i8,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct DeleteAclsResponseV1 {
51    pub throttle_time_ms: i32,
52    pub filter_results: Vec<DeleteAclsFilterResultV1>,
53}
54
55impl DeleteAclsResponseV1 {
56    pub fn decode_body(decoder: &mut Decoder<'_>) -> Result<Self> {
57        Ok(Self {
58            throttle_time_ms: decoder.read_i32()?,
59            filter_results: decoder
60                .read_array(
61                    "delete ACL filter results",
62                    DeleteAclsFilterResultV1::decode,
63                )?
64                .unwrap_or_default(),
65        })
66    }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub struct DeleteAclsFilterResultV1 {
71    pub error_code: i16,
72    pub error_message: Option<String>,
73    pub matching_acls: Vec<DeleteAclsMatchingAclV1>,
74}
75
76impl DeleteAclsFilterResultV1 {
77    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
78        Ok(Self {
79            error_code: decoder.read_i16()?,
80            error_message: decoder.read_nullable_string()?,
81            matching_acls: decoder
82                .read_array("deleted ACLs", DeleteAclsMatchingAclV1::decode)?
83                .unwrap_or_default(),
84        })
85    }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub struct DeleteAclsMatchingAclV1 {
90    pub error_code: i16,
91    pub error_message: Option<String>,
92    pub resource_type: i8,
93    pub resource_name: String,
94    pub pattern_type: i8,
95    pub principal: String,
96    pub host: String,
97    pub operation: i8,
98    pub permission_type: i8,
99}
100
101impl DeleteAclsMatchingAclV1 {
102    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
103        Ok(Self {
104            error_code: decoder.read_i16()?,
105            error_message: decoder.read_nullable_string()?,
106            resource_type: decoder.read_i8()?,
107            resource_name: decoder.read_string()?,
108            pattern_type: decoder.read_i8()?,
109            principal: decoder.read_string()?,
110            host: decoder.read_string()?,
111            operation: decoder.read_i8()?,
112            permission_type: decoder.read_i8()?,
113        })
114    }
115}
116
117#[cfg(test)]
118#[allow(clippy::unwrap_used)]
119mod tests {
120    use super::{DeleteAclsFilterV1, DeleteAclsRequestV1, DeleteAclsResponseV1, API_KEY};
121    use crate::codec::{Decoder, Encoder};
122
123    #[test]
124    fn encodes_delete_acls_v1_request() {
125        let request = DeleteAclsRequestV1 {
126            correlation_id: 11,
127            client_id: Some("kafrust".to_owned()),
128            filters: vec![DeleteAclsFilterV1 {
129                resource_type_filter: 2,
130                resource_name_filter: Some("orders".to_owned()),
131                pattern_type_filter: 3,
132                principal_filter: None,
133                host_filter: Some("*".to_owned()),
134                operation: 3,
135                permission_type: 1,
136            }],
137        };
138
139        let bytes = request.encode().unwrap();
140        assert_eq!(&bytes[0..4], &[0, API_KEY as u8, 0, 1]);
141        assert_eq!(&bytes[4..8], &[0, 0, 0, 11]);
142        assert_eq!(bytes.last(), Some(&1));
143    }
144
145    #[test]
146    fn decodes_delete_acls_v1_response() {
147        let mut bytes = Encoder::new();
148        bytes.write_i32(3);
149        bytes.write_i32(1);
150        bytes.write_i16(0);
151        bytes.write_nullable_string(None).unwrap();
152        bytes.write_i32(1);
153        bytes.write_i16(0);
154        bytes.write_nullable_string(None).unwrap();
155        bytes.write_i8(2);
156        bytes.write_string("orders").unwrap();
157        bytes.write_i8(3);
158        bytes.write_string("User:alice").unwrap();
159        bytes.write_string("*").unwrap();
160        bytes.write_i8(3);
161        bytes.write_i8(1);
162        let bytes = bytes.into_bytes();
163        let mut decoder = Decoder::new(&bytes);
164
165        let response = DeleteAclsResponseV1::decode_body(&mut decoder).unwrap();
166
167        assert_eq!(response.throttle_time_ms, 3);
168        assert_eq!(
169            response.filter_results[0].matching_acls[0].resource_name,
170            "orders"
171        );
172        assert_eq!(response.filter_results[0].matching_acls[0].pattern_type, 3);
173        assert!(decoder.is_empty());
174    }
175}