Skip to main content

openstack_cli_block_storage/v3/snapshot/
set.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 Snapshot command
19//!
20//! Wraps invoking of the `v3/snapshots/{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_sdk::api::QueryAsync;
32use openstack_sdk::api::block_storage::v3::snapshot::find;
33use openstack_sdk::api::block_storage::v3::snapshot::set;
34use openstack_sdk::api::find;
35use openstack_types::block_storage::v3::snapshot::response;
36
37/// Update a snapshot.
38#[derive(Args)]
39pub struct SnapshotCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47
48    #[command(flatten)]
49    snapshot: Snapshot,
50}
51
52/// Query parameters
53#[derive(Args)]
54struct QueryParameters {}
55
56/// Path parameters
57#[derive(Args)]
58struct PathParameters {
59    /// id parameter for /v3/snapshots/{id} API
60    #[arg(
61        help_heading = "Path parameters",
62        id = "path_param_id",
63        value_name = "ID"
64    )]
65    id: String,
66}
67/// Snapshot Body data
68#[derive(Args, Clone)]
69struct Snapshot {
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    #[arg(help_heading = "Body parameters", long)]
78    display_description: Option<String>,
79
80    /// Set explicit NULL for the display_description
81    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "display_description")]
82    no_display_description: bool,
83
84    #[arg(help_heading = "Body parameters", long)]
85    display_name: Option<String>,
86
87    /// Set explicit NULL for the display_name
88    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "display_name")]
89    no_display_name: bool,
90
91    #[arg(help_heading = "Body parameters", long)]
92    name: Option<String>,
93
94    /// Set explicit NULL for the name
95    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
96    no_name: bool,
97}
98
99impl SnapshotCommand {
100    /// Perform command action
101    pub async fn take_action<C: CliArgs>(
102        &self,
103        parsed_args: &C,
104        client: &mut AsyncOpenStack,
105    ) -> Result<(), OpenStackCliError> {
106        info!("Set Snapshot");
107
108        let op =
109            OutputProcessor::from_args(parsed_args, Some("block-storage.snapshot"), Some("set"));
110        op.validate_args(parsed_args)?;
111
112        let mut find_builder = find::Request::builder();
113
114        find_builder.id(&self.path.id);
115
116        let find_ep = find_builder
117            .build()
118            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
119        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
120
121        let mut ep_builder = set::Request::builder();
122
123        let resource_id = find_data["id"]
124            .as_str()
125            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
126            .to_string();
127        ep_builder.id(resource_id.clone());
128
129        // Set body parameters
130        // Set Request.snapshot data
131        let args = &self.snapshot;
132        let mut snapshot_builder = set::SnapshotBuilder::default();
133        if let Some(val) = &args.description {
134            snapshot_builder.description(Some(val.into()));
135        } else if args.no_description {
136            snapshot_builder.description(None);
137        }
138
139        if let Some(val) = &args.display_description {
140            snapshot_builder.display_description(Some(val.into()));
141        } else if args.no_display_description {
142            snapshot_builder.display_description(None);
143        }
144
145        if let Some(val) = &args.display_name {
146            snapshot_builder.display_name(Some(val.into()));
147        } else if args.no_display_name {
148            snapshot_builder.display_name(None);
149        }
150
151        if let Some(val) = &args.name {
152            snapshot_builder.name(Some(val.into()));
153        } else if args.no_name {
154            snapshot_builder.name(None);
155        }
156
157        ep_builder.snapshot(
158            snapshot_builder
159                .build()
160                .wrap_err("error preparing the request data")?,
161        );
162
163        let ep = ep_builder
164            .build()
165            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
166
167        let data: serde_json::Value = ep.query_async(client).await?;
168
169        op.output_single::<response::set::SnapshotResponse>(data.clone())?;
170        // Show command specific hints
171        op.show_command_hint()?;
172        Ok(())
173    }
174}