Skip to main content

openstack_cli_block_storage/v3/backup/
create_351.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.51]
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_351;
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 backup availability zone key value pair.
63    ///
64    /// **New in version 3.51**
65    #[arg(help_heading = "Body parameters", long)]
66    availability_zone: Option<String>,
67
68    /// Set explicit NULL for the availability_zone
69    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "availability_zone")]
70    no_availability_zone: bool,
71
72    /// The container name or null.
73    #[arg(help_heading = "Body parameters", long)]
74    container: Option<String>,
75
76    /// Set explicit NULL for the container
77    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "container")]
78    no_container: bool,
79
80    /// The backup description or null.
81    #[arg(help_heading = "Body parameters", long)]
82    description: Option<String>,
83
84    /// Set explicit NULL for the description
85    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
86    no_description: bool,
87
88    /// Indicates whether to backup, even if the volume is attached. Default is
89    /// `false`. See [valid boolean values](#valid-boolean-values)
90    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
91    force: Option<bool>,
92
93    /// Indicates whether to backup, even if the volume is attached. Default is
94    /// `false`. See [valid boolean values](#valid-boolean-values)
95    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
96    incremental: Option<bool>,
97
98    /// The backup metadata key value pairs.
99    ///
100    /// **New in version 3.43**
101    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
102    metadata: Option<Vec<(String, String)>>,
103
104    /// The name of the Volume Backup.
105    #[arg(help_heading = "Body parameters", long)]
106    name: Option<String>,
107
108    /// Set explicit NULL for the name
109    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
110    no_name: bool,
111
112    /// The UUID of the source snapshot that you want to back up.
113    #[arg(help_heading = "Body parameters", long)]
114    snapshot_id: Option<String>,
115
116    /// Set explicit NULL for the snapshot_id
117    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "snapshot_id")]
118    no_snapshot_id: bool,
119
120    /// The UUID of the volume that you want to back up.
121    #[arg(help_heading = "Body parameters", long)]
122    volume_id: String,
123}
124
125impl BackupCommand {
126    /// Perform command action
127    pub async fn take_action<C: CliArgs>(
128        &self,
129        parsed_args: &C,
130        client: &mut AsyncOpenStack,
131    ) -> Result<(), OpenStackCliError> {
132        info!("Create Backup");
133
134        let op =
135            OutputProcessor::from_args(parsed_args, Some("block-storage.backup"), Some("create"));
136        op.validate_args(parsed_args)?;
137
138        let mut ep_builder = create_351::Request::builder();
139        ep_builder.header(
140            http::header::HeaderName::from_static("openstack-api-version"),
141            http::header::HeaderValue::from_static("volume 3.51"),
142        );
143
144        // Set body parameters
145        // Set Request.backup data
146        let args = &self.backup;
147        let mut backup_builder = create_351::BackupBuilder::default();
148        if let Some(val) = &args.availability_zone {
149            backup_builder.availability_zone(Some(val.into()));
150        } else if args.no_availability_zone {
151            backup_builder.availability_zone(None);
152        }
153
154        if let Some(val) = &args.container {
155            backup_builder.container(Some(val.into()));
156        } else if args.no_container {
157            backup_builder.container(None);
158        }
159
160        if let Some(val) = &args.description {
161            backup_builder.description(Some(val.into()));
162        } else if args.no_description {
163            backup_builder.description(None);
164        }
165
166        if let Some(val) = &args.force {
167            backup_builder.force(*val);
168        }
169
170        if let Some(val) = &args.incremental {
171            backup_builder.incremental(*val);
172        }
173
174        if let Some(val) = &args.metadata {
175            backup_builder.metadata(val.iter().cloned());
176        }
177
178        if let Some(val) = &args.name {
179            backup_builder.name(Some(val.into()));
180        } else if args.no_name {
181            backup_builder.name(None);
182        }
183
184        if let Some(val) = &args.snapshot_id {
185            backup_builder.snapshot_id(Some(val.into()));
186        } else if args.no_snapshot_id {
187            backup_builder.snapshot_id(None);
188        }
189
190        backup_builder.volume_id(&args.volume_id);
191
192        ep_builder.backup(
193            backup_builder
194                .build()
195                .wrap_err("error preparing the request data")?,
196        );
197
198        let ep = ep_builder
199            .build()
200            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
201
202        let data: serde_json::Value = ep.query_async(client).await?;
203
204        op.output_single::<response::create::BackupResponse>(data.clone())?;
205        // Show command specific hints
206        op.show_command_hint()?;
207        Ok(())
208    }
209}