Skip to main content

openstack_cli_block_storage/v3/backup/
create_343.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Create Backup command [microversion = 3.43]
19//!
20//! Wraps invoking of the `v3/backups` with `POST` method
21
22use clap::Args;
23use eyre::WrapErr;
24use tracing::info;
25
26use openstack_cli_core::cli::CliArgs;
27use openstack_cli_core::error::OpenStackCliError;
28use openstack_cli_core::output::OutputProcessor;
29use openstack_sdk::AsyncOpenStack;
30
31use openstack_cli_core::common::parse_key_val;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::block_storage::v3::backup::create_343;
34use openstack_types::block_storage::v3::backup::response;
35
36/// Create a new backup.
37#[derive(Args)]
38pub struct BackupCommand {
39    /// Request Query parameters
40    #[command(flatten)]
41    query: QueryParameters,
42
43    /// Path parameters
44    #[command(flatten)]
45    path: PathParameters,
46
47    /// A `backup` object.
48    #[command(flatten)]
49    backup: Backup,
50}
51
52/// Query parameters
53#[derive(Args)]
54struct QueryParameters {}
55
56/// Path parameters
57#[derive(Args)]
58struct PathParameters {}
59/// Backup Body data
60#[derive(Args, Clone)]
61struct Backup {
62    /// The container name or null.
63    #[arg(help_heading = "Body parameters", long)]
64    container: Option<String>,
65
66    /// Set explicit NULL for the container
67    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "container")]
68    no_container: bool,
69
70    /// The backup description or null.
71    #[arg(help_heading = "Body parameters", long)]
72    description: Option<String>,
73
74    /// Set explicit NULL for the description
75    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
76    no_description: bool,
77
78    /// Indicates whether to backup, even if the volume is attached. Default is
79    /// `false`. See [valid boolean values](#valid-boolean-values)
80    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
81    force: Option<bool>,
82
83    /// Indicates whether to backup, even if the volume is attached. Default is
84    /// `false`. See [valid boolean values](#valid-boolean-values)
85    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
86    incremental: Option<bool>,
87
88    /// The backup metadata key value pairs.
89    ///
90    /// **New in version 3.43**
91    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
92    metadata: Option<Vec<(String, String)>>,
93
94    /// The name of the Volume Backup.
95    #[arg(help_heading = "Body parameters", long)]
96    name: Option<String>,
97
98    /// Set explicit NULL for the name
99    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
100    no_name: bool,
101
102    /// The UUID of the source snapshot that you want to back up.
103    #[arg(help_heading = "Body parameters", long)]
104    snapshot_id: Option<String>,
105
106    /// Set explicit NULL for the snapshot_id
107    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "snapshot_id")]
108    no_snapshot_id: bool,
109
110    /// The UUID of the volume that you want to back up.
111    #[arg(help_heading = "Body parameters", long)]
112    volume_id: String,
113}
114
115impl BackupCommand {
116    /// Perform command action
117    pub async fn take_action<C: CliArgs>(
118        &self,
119        parsed_args: &C,
120        client: &mut AsyncOpenStack,
121    ) -> Result<(), OpenStackCliError> {
122        info!("Create Backup");
123
124        let op =
125            OutputProcessor::from_args(parsed_args, Some("block-storage.backup"), Some("create"));
126        op.validate_args(parsed_args)?;
127
128        let mut ep_builder = create_343::Request::builder();
129        ep_builder.header(
130            http::header::HeaderName::from_static("openstack-api-version"),
131            http::header::HeaderValue::from_static("volume 3.43"),
132        );
133
134        // Set body parameters
135        // Set Request.backup data
136        let args = &self.backup;
137        let mut backup_builder = create_343::BackupBuilder::default();
138        if let Some(val) = &args.container {
139            backup_builder.container(Some(val.into()));
140        } else if args.no_container {
141            backup_builder.container(None);
142        }
143
144        if let Some(val) = &args.description {
145            backup_builder.description(Some(val.into()));
146        } else if args.no_description {
147            backup_builder.description(None);
148        }
149
150        if let Some(val) = &args.force {
151            backup_builder.force(*val);
152        }
153
154        if let Some(val) = &args.incremental {
155            backup_builder.incremental(*val);
156        }
157
158        if let Some(val) = &args.metadata {
159            backup_builder.metadata(val.iter().cloned());
160        }
161
162        if let Some(val) = &args.name {
163            backup_builder.name(Some(val.into()));
164        } else if args.no_name {
165            backup_builder.name(None);
166        }
167
168        if let Some(val) = &args.snapshot_id {
169            backup_builder.snapshot_id(Some(val.into()));
170        } else if args.no_snapshot_id {
171            backup_builder.snapshot_id(None);
172        }
173
174        backup_builder.volume_id(&args.volume_id);
175
176        ep_builder.backup(
177            backup_builder
178                .build()
179                .wrap_err("error preparing the request data")?,
180        );
181
182        let ep = ep_builder
183            .build()
184            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
185
186        let data: serde_json::Value = ep.query_async(client).await?;
187
188        op.output_single::<response::create::BackupResponse>(data.clone())?;
189        // Show command specific hints
190        op.show_command_hint()?;
191        Ok(())
192    }
193}