Skip to main content

iggy_cli/commands/binary_users/
create_user.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 iggy_common::Client;
23use iggy_common::Permissions;
24use iggy_common::UserStatus;
25use iggy_common::create_user::CreateUser;
26use secrecy::{ExposeSecret, SecretString};
27use tracing::{Level, event};
28
29pub struct CreateUserCmd {
30    create_user: CreateUser,
31}
32
33impl CreateUserCmd {
34    pub fn new(
35        username: String,
36        password: String,
37        status: UserStatus,
38        permissions: Option<Permissions>,
39    ) -> Self {
40        Self {
41            create_user: CreateUser {
42                username,
43                password: SecretString::from(password),
44                status,
45                permissions,
46            },
47        }
48    }
49}
50
51#[async_trait]
52impl CliCommand for CreateUserCmd {
53    fn explain(&self) -> String {
54        format!("create user with username: {}", self.create_user.username)
55    }
56
57    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
58        client
59            .create_user(
60                &self.create_user.username,
61                self.create_user.password.expose_secret(),
62                self.create_user.status,
63                self.create_user.permissions.clone(),
64            )
65            .await
66            .with_context(|| {
67                format!(
68                    "Problem creating user (username: {})",
69                    self.create_user.username
70                )
71            })?;
72
73        event!(target: PRINT_TARGET, Level::INFO,
74            "User with username: {} created",
75            self.create_user.username
76        );
77
78        Ok(())
79    }
80}