Skip to main content

openstack_cli_block_storage/v3/backup/
create_30.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.0]
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_sdk::api::QueryAsync;
32use openstack_sdk::api::block_storage::v3::backup::create_30;
33use openstack_types::block_storage::v3::backup::response;
34
35/// Create a new backup.
36#[derive(Args)]
37pub struct BackupCommand {
38    /// Request Query parameters
39    #[command(flatten)]
40    query: QueryParameters,
41
42    /// Path parameters
43    #[command(flatten)]
44    path: PathParameters,
45
46    /// A `backup` object.
47    #[command(flatten)]
48    backup: Backup,
49}
50
51/// Query parameters
52#[derive(Args)]
53struct QueryParameters {}
54
55/// Path parameters
56#[derive(Args)]
57struct PathParameters {}
58/// Backup Body data
59#[derive(Args, Clone)]
60struct Backup {
61    /// The container name or null.
62    #[arg(help_heading = "Body parameters", long)]
63    container: Option<String>,
64
65    /// Set explicit NULL for the container
66    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "container")]
67    no_container: bool,
68
69    /// The backup description or null.
70    #[arg(help_heading = "Body parameters", long)]
71    description: Option<String>,
72
73    /// Set explicit NULL for the description
74    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
75    no_description: bool,
76
77    /// Indicates whether to backup, even if the volume is attached. Default is
78    /// `false`. See [valid boolean values](#valid-boolean-values)
79    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
80    force: Option<bool>,
81
82    /// Indicates whether to backup, even if the volume is attached. Default is
83    /// `false`. See [valid boolean values](#valid-boolean-values)
84    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
85    incremental: Option<bool>,
86
87    /// The name of the Volume Backup.
88    #[arg(help_heading = "Body parameters", long)]
89    name: Option<String>,
90
91    /// Set explicit NULL for the name
92    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
93    no_name: bool,
94
95    /// The UUID of the source snapshot that you want to back up.
96    #[arg(help_heading = "Body parameters", long)]
97    snapshot_id: Option<String>,
98
99    /// Set explicit NULL for the snapshot_id
100    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "snapshot_id")]
101    no_snapshot_id: bool,
102
103    /// The UUID of the volume that you want to back up.
104    #[arg(help_heading = "Body parameters", long)]
105    volume_id: String,
106}
107
108impl BackupCommand {
109    /// Perform command action
110    pub async fn take_action<C: CliArgs>(
111        &self,
112        parsed_args: &C,
113        client: &mut AsyncOpenStack,
114    ) -> Result<(), OpenStackCliError> {
115        info!("Create Backup");
116
117        let op =
118            OutputProcessor::from_args(parsed_args, Some("block-storage.backup"), Some("create"));
119        op.validate_args(parsed_args)?;
120
121        let mut ep_builder = create_30::Request::builder();
122        ep_builder.header(
123            http::header::HeaderName::from_static("openstack-api-version"),
124            http::header::HeaderValue::from_static("volume 3.0"),
125        );
126
127        // Set body parameters
128        // Set Request.backup data
129        let args = &self.backup;
130        let mut backup_builder = create_30::BackupBuilder::default();
131        if let Some(val) = &args.container {
132            backup_builder.container(Some(val.into()));
133        } else if args.no_container {
134            backup_builder.container(None);
135        }
136
137        if let Some(val) = &args.description {
138            backup_builder.description(Some(val.into()));
139        } else if args.no_description {
140            backup_builder.description(None);
141        }
142
143        if let Some(val) = &args.force {
144            backup_builder.force(*val);
145        }
146
147        if let Some(val) = &args.incremental {
148            backup_builder.incremental(*val);
149        }
150
151        if let Some(val) = &args.name {
152            backup_builder.name(Some(val.into()));
153        } else if args.no_name {
154            backup_builder.name(None);
155        }
156
157        if let Some(val) = &args.snapshot_id {
158            backup_builder.snapshot_id(Some(val.into()));
159        } else if args.no_snapshot_id {
160            backup_builder.snapshot_id(None);
161        }
162
163        backup_builder.volume_id(&args.volume_id);
164
165        ep_builder.backup(
166            backup_builder
167                .build()
168                .wrap_err("error preparing the request data")?,
169        );
170
171        let ep = ep_builder
172            .build()
173            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
174
175        let data: serde_json::Value = ep.query_async(client).await?;
176
177        op.output_single::<response::create::BackupResponse>(data.clone())?;
178        // Show command specific hints
179        op.show_command_hint()?;
180        Ok(())
181    }
182}