Skip to main content

openstack_cli_block_storage/v3/backup/
set_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//! Set Backup command [microversion = 3.43]
19//!
20//! Wraps invoking of the `v3/backups/{id}` with `PUT` 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::find;
34use openstack_sdk::api::block_storage::v3::backup::set_343;
35use openstack_sdk::api::find;
36use openstack_types::block_storage::v3::backup::response;
37
38/// Update a backup.
39#[derive(Args)]
40pub struct BackupCommand {
41    /// Request Query parameters
42    #[command(flatten)]
43    query: QueryParameters,
44
45    /// Path parameters
46    #[command(flatten)]
47    path: PathParameters,
48
49    #[command(flatten)]
50    backup: Option<Backup>,
51}
52
53/// Query parameters
54#[derive(Args)]
55struct QueryParameters {}
56
57/// Path parameters
58#[derive(Args)]
59struct PathParameters {
60    /// id parameter for /v3/backups/{id} API
61    #[arg(
62        help_heading = "Path parameters",
63        id = "path_param_id",
64        value_name = "ID"
65    )]
66    id: String,
67}
68/// Backup Body data
69#[derive(Args, Clone)]
70struct Backup {
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    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
79    metadata: Option<Vec<(String, String)>>,
80
81    #[arg(help_heading = "Body parameters", long)]
82    name: Option<String>,
83
84    /// Set explicit NULL for the name
85    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
86    no_name: bool,
87}
88
89impl BackupCommand {
90    /// Perform command action
91    pub async fn take_action<C: CliArgs>(
92        &self,
93        parsed_args: &C,
94        client: &mut AsyncOpenStack,
95    ) -> Result<(), OpenStackCliError> {
96        info!("Set Backup");
97
98        let op = OutputProcessor::from_args(parsed_args, Some("block-storage.backup"), Some("set"));
99        op.validate_args(parsed_args)?;
100
101        let mut find_builder = find::Request::builder();
102
103        find_builder.id(&self.path.id);
104        find_builder.header(
105            http::header::HeaderName::from_static("openstack-api-version"),
106            http::header::HeaderValue::from_static("volume 3.43"),
107        );
108
109        let find_ep = find_builder
110            .build()
111            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
112        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
113
114        let mut ep_builder = set_343::Request::builder();
115        ep_builder.header(
116            http::header::HeaderName::from_static("openstack-api-version"),
117            http::header::HeaderValue::from_static("volume 3.43"),
118        );
119
120        let resource_id = find_data["id"]
121            .as_str()
122            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
123            .to_string();
124        ep_builder.id(resource_id.clone());
125
126        // Set body parameters
127        // Set Request.backup data
128        if let Some(lbackup) = &self.backup {
129            let mut backup_builder = set_343::BackupBuilder::default();
130            if let Some(val) = &lbackup.description {
131                backup_builder.description(Some(val.into()));
132            }
133            if let Some(val) = &lbackup.metadata {
134                backup_builder.metadata(val.iter().cloned());
135            }
136            if let Some(val) = &lbackup.name {
137                backup_builder.name(Some(val.into()));
138            }
139            ep_builder.backup(
140                backup_builder
141                    .build()
142                    .wrap_err("error preparing the request data")?,
143            );
144        }
145
146        let ep = ep_builder
147            .build()
148            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
149
150        let data: serde_json::Value = ep.query_async(client).await?;
151
152        op.output_single::<response::set::BackupResponse>(data.clone())?;
153        // Show command specific hints
154        op.show_command_hint()?;
155        Ok(())
156    }
157}