Skip to main content

openstack_cli_block_storage/v3/snapshot/
create.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 Snapshot command
19//!
20//! Wraps invoking of the `v3/snapshots` 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::snapshot::create;
34use openstack_types::block_storage::v3::snapshot::response;
35
36/// Creates a new snapshot.
37#[derive(Args)]
38pub struct SnapshotCommand {
39    /// Request Query parameters
40    #[command(flatten)]
41    query: QueryParameters,
42
43    /// Path parameters
44    #[command(flatten)]
45    path: PathParameters,
46
47    /// A `snapshot` object.
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/// Snapshot Body data
60#[derive(Args, Clone)]
61struct Snapshot {
62    /// A description for the snapshot. Default is `None`.
63    #[arg(help_heading = "Body parameters", long)]
64    description: Option<String>,
65
66    /// Set explicit NULL for the description
67    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
68    no_description: bool,
69
70    /// The name of the snapshot.
71    #[arg(help_heading = "Body parameters", long)]
72    display_name: Option<String>,
73
74    /// Set explicit NULL for the display_name
75    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "display_name")]
76    no_display_name: bool,
77
78    /// Indicates whether to snapshot, even if the volume is attached. Default
79    /// is `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    /// One or more metadata key and value pairs for the snapshot.
84    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
85    metadata: Option<Vec<(String, String)>>,
86
87    /// The name of the snapshot.
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 volume.
96    #[arg(help_heading = "Body parameters", long)]
97    volume_id: String,
98}
99
100impl SnapshotCommand {
101    /// Perform command action
102    pub async fn take_action<C: CliArgs>(
103        &self,
104        parsed_args: &C,
105        client: &mut AsyncOpenStack,
106    ) -> Result<(), OpenStackCliError> {
107        info!("Create Snapshot");
108
109        let op =
110            OutputProcessor::from_args(parsed_args, Some("block-storage.snapshot"), Some("create"));
111        op.validate_args(parsed_args)?;
112
113        let mut ep_builder = create::Request::builder();
114
115        // Set body parameters
116        // Set Request.snapshot data
117        let args = &self.snapshot;
118        let mut snapshot_builder = create::SnapshotBuilder::default();
119        if let Some(val) = &args.description {
120            snapshot_builder.description(Some(val.into()));
121        } else if args.no_description {
122            snapshot_builder.description(None);
123        }
124
125        if let Some(val) = &args.display_name {
126            snapshot_builder.display_name(Some(val.into()));
127        } else if args.no_display_name {
128            snapshot_builder.display_name(None);
129        }
130
131        if let Some(val) = &args.force {
132            snapshot_builder.force(*val);
133        }
134
135        if let Some(val) = &args.metadata {
136            snapshot_builder.metadata(val.iter().cloned());
137        }
138
139        if let Some(val) = &args.name {
140            snapshot_builder.name(Some(val.into()));
141        } else if args.no_name {
142            snapshot_builder.name(None);
143        }
144
145        snapshot_builder.volume_id(args.volume_id.clone());
146
147        ep_builder.snapshot(
148            snapshot_builder
149                .build()
150                .wrap_err("error preparing the request data")?,
151        );
152
153        let ep = ep_builder
154            .build()
155            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
156
157        let data: serde_json::Value = ep.query_async(client).await?;
158
159        op.output_single::<response::create::SnapshotResponse>(data.clone())?;
160        // Show command specific hints
161        op.show_command_hint()?;
162        Ok(())
163    }
164}