Skip to main content

iggy_cli/commands/binary_cluster/
get_cluster_metadata.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;
23use iggy_common::Client;
24use tracing::{Level, event};
25
26pub enum GetClusterMetadataOutput {
27    Table,
28    List,
29}
30
31pub struct GetClusterMetadataCmd {
32    output: GetClusterMetadataOutput,
33}
34
35impl GetClusterMetadataCmd {
36    pub fn new(output: GetClusterMetadataOutput) -> Self {
37        GetClusterMetadataCmd { output }
38    }
39}
40
41impl Default for GetClusterMetadataCmd {
42    fn default() -> Self {
43        GetClusterMetadataCmd {
44            output: GetClusterMetadataOutput::Table,
45        }
46    }
47}
48
49#[async_trait]
50impl CliCommand for GetClusterMetadataCmd {
51    fn explain(&self) -> String {
52        let mode = match self.output {
53            GetClusterMetadataOutput::Table => "table",
54            GetClusterMetadataOutput::List => "list",
55        };
56        format!("get cluster metadata in {mode} mode")
57    }
58
59    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
60        let cluster_metadata = client
61            .get_cluster_metadata()
62            .await
63            .with_context(|| String::from("Problem getting cluster metadata"))?;
64
65        if cluster_metadata.nodes.is_empty() {
66            event!(target: PRINT_TARGET, Level::INFO, "No cluster nodes found!");
67            return Ok(());
68        }
69
70        event!(target: PRINT_TARGET, Level::INFO, "Cluster name: {}", cluster_metadata.name);
71
72        match self.output {
73            GetClusterMetadataOutput::Table => {
74                let mut table = Table::new();
75
76                table.set_header(vec![
77                    "Name",
78                    "IP",
79                    "TCP",
80                    "QUIC",
81                    "HTTP",
82                    "WebSocket",
83                    "Role",
84                    "Status",
85                ]);
86
87                cluster_metadata.nodes.iter().for_each(|node| {
88                    table.add_row(vec![
89                        node.name.to_string(),
90                        node.ip.to_string(),
91                        node.endpoints.tcp.to_string(),
92                        node.endpoints.quic.to_string(),
93                        node.endpoints.http.to_string(),
94                        node.endpoints.websocket.to_string(),
95                        node.role.to_string(),
96                        node.status.to_string(),
97                    ]);
98                });
99
100                event!(target: PRINT_TARGET, Level::INFO, "{table}");
101            }
102            GetClusterMetadataOutput::List => {
103                cluster_metadata.nodes.iter().for_each(|node| {
104                    event!(target: PRINT_TARGET, Level::INFO,
105                        "{}|{}|{}|{}|{}|{}|{}|{}",
106                        node.name,
107                        node.ip,
108                        node.endpoints.tcp,
109                        node.endpoints.quic,
110                        node.endpoints.http,
111                        node.endpoints.websocket,
112                        node.role,
113                        node.status
114                    );
115                });
116            }
117        }
118
119        Ok(())
120    }
121}