Skip to main content

openstack_cli_block_storage/v3/snapshot_manage/
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 SnapshotManage command
19//!
20//! Wraps invoking of the `v3/os-snapshot-manage` 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_manage::create;
34use openstack_types::block_storage::v3::snapshot_manage::response;
35use serde_json::Value;
36
37/// Instruct Cinder to manage a storage snapshot object.
38///
39/// Manages an existing backend storage snapshot object (e.g. a Linux logical
40/// volume or a SAN disk) by creating the Cinder objects required to manage it,
41/// and possibly renaming the backend storage snapshot object (driver
42/// dependent).
43///
44/// From an API perspective, this operation behaves very much like a snapshot
45/// creation operation.
46///
47/// Required HTTP Body:
48///
49/// ```text
50/// {
51///   "snapshot":
52///   {
53///     "volume_id": "<Cinder volume already exists in volume backend>",
54///     "ref":
55///        "<Driver-specific reference to the existing storage object>"
56///   }
57/// }
58/// ```
59///
60/// See the appropriate Cinder drivers' implementations of the manage_snapshot
61/// method to find out the accepted format of 'ref'. For example,in LVM driver,
62/// it will be the logic volume name of snapshot which you want to manage.
63///
64/// This API call will return with an error if any of the above elements are
65/// missing from the request, or if the 'volume_id' element refers to a cinder
66/// volume that could not be found.
67///
68/// The snapshot will later enter the error state if it is discovered that
69/// 'ref' is bad.
70///
71/// Optional elements to 'snapshot' are:
72///
73/// ```text
74/// name           A name for the new snapshot.
75/// description    A description for the new snapshot.
76/// metadata       Key/value pairs to be associated with the new snapshot.
77/// ```
78#[derive(Args)]
79pub struct SnapshotManageCommand {
80    /// Request Query parameters
81    #[command(flatten)]
82    query: QueryParameters,
83
84    /// Path parameters
85    #[command(flatten)]
86    path: PathParameters,
87
88    #[command(flatten)]
89    snapshot: Snapshot,
90}
91
92/// Query parameters
93#[derive(Args)]
94struct QueryParameters {}
95
96/// Path parameters
97#[derive(Args)]
98struct PathParameters {}
99/// Snapshot Body data
100#[derive(Args, Clone)]
101struct Snapshot {
102    #[arg(help_heading = "Body parameters", long)]
103    description: Option<String>,
104
105    /// Set explicit NULL for the description
106    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
107    no_description: bool,
108
109    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
110    metadata: Option<Vec<(String, String)>>,
111
112    #[arg(help_heading = "Body parameters", long)]
113    name: Option<String>,
114
115    /// Set explicit NULL for the name
116    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
117    no_name: bool,
118
119    #[arg(help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
120    _ref: Option<Value>,
121
122    #[arg(help_heading = "Body parameters", long)]
123    volume_id: String,
124}
125
126impl SnapshotManageCommand {
127    /// Perform command action
128    pub async fn take_action<C: CliArgs>(
129        &self,
130        parsed_args: &C,
131        client: &mut AsyncOpenStack,
132    ) -> Result<(), OpenStackCliError> {
133        info!("Create SnapshotManage");
134
135        let op = OutputProcessor::from_args(
136            parsed_args,
137            Some("block-storage.snapshot_manage"),
138            Some("create"),
139        );
140        op.validate_args(parsed_args)?;
141
142        let mut ep_builder = create::Request::builder();
143
144        // Set body parameters
145        // Set Request.snapshot data
146        let args = &self.snapshot;
147        let mut snapshot_builder = create::SnapshotBuilder::default();
148        if let Some(val) = &args.description {
149            snapshot_builder.description(Some(val.into()));
150        } else if args.no_description {
151            snapshot_builder.description(None);
152        }
153
154        if let Some(val) = &args.metadata {
155            snapshot_builder.metadata(val.iter().cloned());
156        }
157
158        if let Some(val) = &args.name {
159            snapshot_builder.name(Some(val.into()));
160        } else if args.no_name {
161            snapshot_builder.name(None);
162        }
163
164        if let Some(val) = &args._ref {
165            snapshot_builder._ref(val.clone());
166        }
167
168        snapshot_builder.volume_id(&args.volume_id);
169
170        ep_builder.snapshot(
171            snapshot_builder
172                .build()
173                .wrap_err("error preparing the request data")?,
174        );
175
176        let ep = ep_builder
177            .build()
178            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
179
180        let data: serde_json::Value = ep.query_async(client).await?;
181
182        op.output_single::<response::create::SnapshotManageResponse>(data.clone())?;
183        // Show command specific hints
184        op.show_command_hint()?;
185        Ok(())
186    }
187}