Skip to main content

openstack_cli_compute/v2/assisted_volume_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 AssistedVolumeSnapshot command
19//!
20//! Wraps invoking of the `v2.1/os-assisted-volume-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 clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::compute::v2::assisted_volume_snapshot::create;
34use openstack_types::compute::v2::assisted_volume_snapshot::response;
35
36/// Creates an assisted volume snapshot.
37///
38/// Normal response codes: 200
39///
40/// Error response codes: badRequest(400),unauthorized(401), forbidden(403)
41#[derive(Args)]
42#[command(about = "Create Assisted Volume Snapshots")]
43pub struct AssistedVolumeSnapshotCommand {
44    /// Request Query parameters
45    #[command(flatten)]
46    query: QueryParameters,
47
48    /// Path parameters
49    #[command(flatten)]
50    path: PathParameters,
51
52    /// A partial representation of a snapshot that is used to create a
53    /// snapshot.
54    #[command(flatten)]
55    snapshot: Snapshot,
56}
57
58/// Query parameters
59#[derive(Args)]
60struct QueryParameters {}
61
62/// Path parameters
63#[derive(Args)]
64struct PathParameters {}
65
66#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
67enum Type {
68    Qcow2,
69}
70
71/// CreateInfo Body data
72#[derive(Args, Clone)]
73#[group(required = true, multiple = true)]
74struct CreateInfo {
75    /// Its an arbitrary string that gets passed back to the user.
76    #[arg(help_heading = "Body parameters", long)]
77    id: Option<String>,
78
79    /// The name of the qcow2 file that Block Storage creates, which becomes
80    /// the active image for the VM.
81    #[arg(help_heading = "Body parameters", long, required = false)]
82    new_file: String,
83
84    /// The UUID for a snapshot.
85    #[arg(help_heading = "Body parameters", long, required = false)]
86    snapshot_id: String,
87
88    /// The snapshot type. A valid value is `qcow2`.
89    #[arg(help_heading = "Body parameters", long, required = false)]
90    _type: Type,
91}
92
93/// Snapshot Body data
94#[derive(Args, Clone)]
95struct Snapshot {
96    /// Information for snapshot creation.
97    #[command(flatten)]
98    create_info: CreateInfo,
99
100    /// The source volume ID.
101    #[arg(help_heading = "Body parameters", long)]
102    volume_id: String,
103}
104
105impl AssistedVolumeSnapshotCommand {
106    /// Perform command action
107    pub async fn take_action<C: CliArgs>(
108        &self,
109        parsed_args: &C,
110        client: &mut AsyncOpenStack,
111    ) -> Result<(), OpenStackCliError> {
112        info!("Create AssistedVolumeSnapshot");
113
114        let op = OutputProcessor::from_args(
115            parsed_args,
116            Some("compute.assisted_volume_snapshot"),
117            Some("create"),
118        );
119        op.validate_args(parsed_args)?;
120
121        let mut ep_builder = create::Request::builder();
122
123        // Set body parameters
124        // Set Request.snapshot data
125        let args = &self.snapshot;
126        let mut snapshot_builder = create::SnapshotBuilder::default();
127
128        let mut create_info_builder = create::CreateInfoBuilder::default();
129        if let Some(val) = &&args.create_info.id {
130            create_info_builder.id(val);
131        }
132
133        create_info_builder.new_file(&args.create_info.new_file);
134
135        create_info_builder.snapshot_id(&args.create_info.snapshot_id);
136
137        let tmp = match &&args.create_info._type {
138            Type::Qcow2 => create::Type::Qcow2,
139        };
140        create_info_builder._type(tmp);
141        snapshot_builder.create_info(
142            create_info_builder
143                .build()
144                .wrap_err("error preparing the request data")?,
145        );
146
147        snapshot_builder.volume_id(&args.volume_id);
148
149        ep_builder.snapshot(
150            snapshot_builder
151                .build()
152                .wrap_err("error preparing the request data")?,
153        );
154
155        let ep = ep_builder
156            .build()
157            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
158
159        let data: serde_json::Value = ep.query_async(client).await?;
160
161        op.output_single::<response::create::AssistedVolumeSnapshotResponse>(data.clone())?;
162        // Show command specific hints
163        op.show_command_hint()?;
164        Ok(())
165    }
166}