Skip to main content

iggy_cli/commands/binary_consumer_groups/
get_consumer_group.rs

1/* Licensed to the Apache Software Foundation (ASF) under one
2 * or more contributor license agreements.  See the NOTICE file
3 * distributed with this work for additional information
4 * regarding copyright ownership.  The ASF licenses this file
5 * to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance
7 * with the License.  You may obtain a copy of the License at
8 *
9 *   http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied.  See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use comfy_table::{Table, presets::ASCII_NO_BORDERS};
23use iggy_common::Client;
24use iggy_common::Identifier;
25use tracing::{Level, event};
26
27pub struct GetConsumerGroupCmd {
28    stream_id: Identifier,
29    topic_id: Identifier,
30    group_id: Identifier,
31}
32
33impl GetConsumerGroupCmd {
34    pub fn new(stream_id: Identifier, topic_id: Identifier, consumer_group_id: Identifier) -> Self {
35        Self {
36            stream_id,
37            topic_id,
38            group_id: consumer_group_id,
39        }
40    }
41}
42
43#[async_trait]
44impl CliCommand for GetConsumerGroupCmd {
45    fn explain(&self) -> String {
46        format!(
47            "get consumer group with ID: {} for topic with ID: {} and stream with ID: {}",
48            self.group_id, self.topic_id, self.stream_id,
49        )
50    }
51
52    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
53        let consumer_group = client
54            .get_consumer_group(&self.stream_id, &self.topic_id, &self.group_id)
55            .await
56            .with_context(|| {
57                format!(
58                    "Problem getting consumer group with ID: {} for topic with ID: {} and stream with ID: {}",
59                    self.group_id, self.topic_id, self.stream_id
60                )
61            })?;
62
63        if consumer_group.is_none() {
64            event!(target: PRINT_TARGET, Level::INFO, "Consumer group with ID: {} was not found", self.group_id);
65            return Ok(());
66        }
67
68        let consumer_group = consumer_group.unwrap();
69        let mut table = Table::new();
70
71        table.set_header(vec!["Property", "Value"]);
72        table.add_row(vec![
73            "Consumer group id",
74            format!("{}", consumer_group.id).as_str(),
75        ]);
76        table.add_row(vec!["Consumer group name", consumer_group.name.as_str()]);
77        table.add_row(vec![
78            "Partitions count",
79            format!("{}", consumer_group.partitions_count).as_str(),
80        ]);
81        table.add_row(vec![
82            "Members count",
83            format!("{}", consumer_group.members_count).as_str(),
84        ]);
85
86        if consumer_group.members_count > 0 {
87            let mut members_table = Table::new();
88            members_table.load_preset(ASCII_NO_BORDERS);
89            members_table.set_header(vec!["Member id", "Partitions count", "Partitions"]);
90            for member in consumer_group.members {
91                members_table.add_row(vec![
92                    format!("{}", member.id).as_str(),
93                    format!("{}", member.partitions_count).as_str(),
94                    member
95                        .partitions
96                        .iter()
97                        .map(|i| format!("{i}"))
98                        .collect::<Vec<String>>()
99                        .join(", ")
100                        .as_str(),
101                ]);
102            }
103            table.add_row(vec!["Members", members_table.to_string().as_str()]);
104        }
105
106        event!(target: PRINT_TARGET, Level::INFO,"{table}");
107
108        Ok(())
109    }
110}