Skip to main content

iggy_cli/commands/binary_topics/
update_topic.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 core::fmt;
23use iggy_common::Client;
24use iggy_common::update_topic::UpdateTopic;
25use iggy_common::{CompressionAlgorithm, Identifier, IggyExpiry, MaxTopicSize};
26use tracing::{Level, event};
27
28pub struct UpdateTopicCmd {
29    update_topic: UpdateTopic,
30    message_expiry: IggyExpiry,
31    max_topic_size: MaxTopicSize,
32    replication_factor: u8,
33}
34
35impl UpdateTopicCmd {
36    pub fn new(
37        stream_id: Identifier,
38        topic_id: Identifier,
39        compression_algorithm: CompressionAlgorithm,
40        name: String,
41        message_expiry: IggyExpiry,
42        max_topic_size: MaxTopicSize,
43        replication_factor: u8,
44    ) -> Self {
45        Self {
46            update_topic: UpdateTopic {
47                stream_id,
48                topic_id,
49                name,
50                compression_algorithm,
51                message_expiry,
52                max_topic_size,
53                replication_factor: Some(replication_factor),
54            },
55            message_expiry,
56            max_topic_size,
57            replication_factor,
58        }
59    }
60}
61
62#[async_trait]
63impl CliCommand for UpdateTopicCmd {
64    fn explain(&self) -> String {
65        format!("{self}")
66    }
67
68    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
69        client
70            .update_topic(&self.update_topic.stream_id, &self.update_topic.topic_id, &self.update_topic.name, self.update_topic.compression_algorithm, self.replication_factor.into(), self.message_expiry, self.max_topic_size)
71            .await
72            .with_context(|| {
73                format!(
74                    "Problem updating topic (ID: {}, name: {}, message expiry: {}) in stream with ID: {}",
75                    self.update_topic.topic_id,
76                    self.update_topic.name,
77                    self.message_expiry,
78                    self.update_topic.stream_id
79                )
80            })?;
81
82        event!(target: PRINT_TARGET, Level::INFO,
83            "Topic with ID: {} updated name: {}, updated message expiry: {}, updated compression algorithm: {}, updated max topic size: {}, updated replication factor: {} in stream with ID: {}",
84            self.update_topic.topic_id,
85            self.update_topic.name,
86            self.message_expiry,
87            self.update_topic.compression_algorithm,
88            self.max_topic_size,
89            self.replication_factor,
90            self.update_topic.stream_id,
91        );
92
93        Ok(())
94    }
95}
96
97impl fmt::Display for UpdateTopicCmd {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        let topic_id = &self.update_topic.topic_id;
100        let topic_name = &self.update_topic.name;
101        let compression_algorithm = &self.update_topic.compression_algorithm;
102        let message_expiry = &self.message_expiry;
103        let max_topic_size = &self.max_topic_size;
104        let replication_factor = self.replication_factor;
105        let stream_id = &self.update_topic.stream_id;
106
107        write!(
108            f,
109            "update topic with ID: {topic_id}, name: {topic_name}, message expiry: \
110            {message_expiry}, compression algorithm: {compression_algorithm}, max topic size: {max_topic_size}, replication \
111            factor: {replication_factor}, in stream with ID: {stream_id}",
112        )
113    }
114}