Skip to main content

openstack_cli_block_storage/v3/volume/
create_30.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 Volume command [microversion = 3.0]
19//!
20//! Wraps invoking of the `v3/volumes` 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::volume::create_30;
34use openstack_types::block_storage::v3::volume::response;
35use serde_json::Value;
36
37/// Creates a new volume.
38///
39/// | | | | --- | --- | | param req: | the request | | param body: | the
40/// request body | | returns: | dict -- the new volume dictionary | | raises
41/// HTTPNotFound, HTTPBadRequest: | | | | |
42#[derive(Args)]
43pub struct VolumeCommand {
44    /// Request Query parameters
45    #[command(flatten)]
46    query: QueryParameters,
47
48    /// Path parameters
49    #[command(flatten)]
50    path: PathParameters,
51
52    /// The dictionary of data to send to the scheduler.
53    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
54    os_sch_hnt_scheduler_hints: Option<Vec<(String, Value)>>,
55
56    /// A `volume` object.
57    #[command(flatten)]
58    volume: Volume,
59}
60
61/// Query parameters
62#[derive(Args)]
63struct QueryParameters {}
64
65/// Path parameters
66#[derive(Args)]
67struct PathParameters {}
68/// Volume Body data
69#[derive(Args, Clone)]
70struct Volume {
71    /// The name of the availability zone.
72    #[arg(help_heading = "Body parameters", long)]
73    availability_zone: Option<String>,
74
75    /// Set explicit NULL for the availability_zone
76    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "availability_zone")]
77    no_availability_zone: bool,
78
79    /// The UUID of the consistency group.
80    #[arg(help_heading = "Body parameters", long)]
81    consistencygroup_id: Option<String>,
82
83    /// Set explicit NULL for the consistencygroup_id
84    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "consistencygroup_id")]
85    no_consistencygroup_id: bool,
86
87    /// The volume description.
88    #[arg(help_heading = "Body parameters", long)]
89    description: Option<String>,
90
91    /// Set explicit NULL for the description
92    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
93    no_description: bool,
94
95    #[arg(help_heading = "Body parameters", long)]
96    display_description: Option<String>,
97
98    /// Set explicit NULL for the display_description
99    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "display_description")]
100    no_display_description: bool,
101
102    #[arg(help_heading = "Body parameters", long)]
103    display_name: Option<String>,
104
105    /// Set explicit NULL for the display_name
106    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "display_name")]
107    no_display_name: bool,
108
109    #[arg(help_heading = "Body parameters", long)]
110    image_id: Option<String>,
111
112    /// Set explicit NULL for the image_id
113    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "image_id")]
114    no_image_id: bool,
115
116    /// The UUID of the image from which you want to create the volume.
117    /// Required to create a bootable volume.
118    ///
119    /// **New in version 3.46**: Instead of directly consuming a zero-byte
120    /// image that has been created by the Compute service when an instance
121    /// snapshot was requested, the Block Storage service will use the
122    /// `snapshot_id` contained in the `block_device_mapping` image property to
123    /// locate the volume snapshot, and will use that to create the volume
124    /// instead.
125    #[arg(help_heading = "Body parameters", long)]
126    image_ref: Option<String>,
127
128    /// Set explicit NULL for the image_ref
129    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "image_ref")]
130    no_image_ref: bool,
131
132    /// One or more metadata key and value pairs to be associated with the new
133    /// volume.
134    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
135    metadata: Option<Vec<(String, String)>>,
136
137    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
138    multiattach: Option<Option<bool>>,
139
140    /// The volume name.
141    #[arg(help_heading = "Body parameters", long)]
142    name: Option<String>,
143
144    /// Set explicit NULL for the name
145    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
146    no_name: bool,
147
148    /// The size of the volume, in gibibytes (GiB).
149    #[arg(help_heading = "Body parameters", long)]
150    size: Option<Option<i32>>,
151
152    /// The UUID of the consistency group.
153    #[arg(help_heading = "Body parameters", long)]
154    snapshot_id: Option<String>,
155
156    /// Set explicit NULL for the snapshot_id
157    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "snapshot_id")]
158    no_snapshot_id: bool,
159
160    /// The UUID of the consistency group.
161    #[arg(help_heading = "Body parameters", long)]
162    source_volid: Option<String>,
163
164    /// Set explicit NULL for the source_volid
165    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "source_volid")]
166    no_source_volid: bool,
167
168    /// The volume type (either name or ID). To create an environment with
169    /// multiple-storage back ends, you must specify a volume type. Block
170    /// Storage volume back ends are spawned as children to `cinder- volume`,
171    /// and they are keyed from a unique queue. They are named
172    /// `cinder- volume.HOST.BACKEND`. For example,
173    /// `cinder- volume.ubuntu.lvmdriver`. When a volume is created, the
174    /// scheduler chooses an appropriate back end to handle the request based
175    /// on the volume type. Default is `None`. For information about how to use
176    /// volume types to create multiple- storage back ends, see
177    /// [Configure multiple-storage back ends](https://docs.openstack.org/cinder/latest/admin/blockstorage-multi-backend.html).
178    #[arg(help_heading = "Body parameters", long)]
179    volume_type: Option<String>,
180
181    /// Set explicit NULL for the volume_type
182    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "volume_type")]
183    no_volume_type: bool,
184}
185
186impl VolumeCommand {
187    /// Perform command action
188    pub async fn take_action<C: CliArgs>(
189        &self,
190        parsed_args: &C,
191        client: &mut AsyncOpenStack,
192    ) -> Result<(), OpenStackCliError> {
193        info!("Create Volume");
194
195        let op =
196            OutputProcessor::from_args(parsed_args, Some("block-storage.volume"), Some("create"));
197        op.validate_args(parsed_args)?;
198
199        let mut ep_builder = create_30::Request::builder();
200        ep_builder.header(
201            http::header::HeaderName::from_static("openstack-api-version"),
202            http::header::HeaderValue::from_static("volume 3.0"),
203        );
204
205        // Set body parameters
206        // Set Request.os_sch_hnt_scheduler_hints data
207        if let Some(arg) = &self.os_sch_hnt_scheduler_hints {
208            ep_builder.os_sch_hnt_scheduler_hints(arg.iter().cloned());
209        }
210
211        // Set Request.volume data
212        let args = &self.volume;
213        let mut volume_builder = create_30::VolumeBuilder::default();
214        if let Some(val) = &args.availability_zone {
215            volume_builder.availability_zone(Some(val.into()));
216        } else if args.no_availability_zone {
217            volume_builder.availability_zone(None);
218        }
219
220        if let Some(val) = &args.consistencygroup_id {
221            volume_builder.consistencygroup_id(Some(val.into()));
222        } else if args.no_consistencygroup_id {
223            volume_builder.consistencygroup_id(None);
224        }
225
226        if let Some(val) = &args.description {
227            volume_builder.description(Some(val.into()));
228        } else if args.no_description {
229            volume_builder.description(None);
230        }
231
232        if let Some(val) = &args.display_description {
233            volume_builder.display_description(Some(val.into()));
234        } else if args.no_display_description {
235            volume_builder.display_description(None);
236        }
237
238        if let Some(val) = &args.display_name {
239            volume_builder.display_name(Some(val.into()));
240        } else if args.no_display_name {
241            volume_builder.display_name(None);
242        }
243
244        if let Some(val) = &args.image_ref {
245            volume_builder.image_ref(Some(val.into()));
246        } else if args.no_image_ref {
247            volume_builder.image_ref(None);
248        }
249
250        if let Some(val) = &args.image_id {
251            volume_builder.image_id(Some(val.into()));
252        } else if args.no_image_id {
253            volume_builder.image_id(None);
254        }
255
256        if let Some(val) = &args.metadata {
257            volume_builder.metadata(val.iter().cloned());
258        }
259
260        if let Some(val) = &args.multiattach {
261            volume_builder.multiattach(*val);
262        }
263
264        if let Some(val) = &args.name {
265            volume_builder.name(Some(val.into()));
266        } else if args.no_name {
267            volume_builder.name(None);
268        }
269
270        if let Some(val) = &args.size {
271            volume_builder.size(*val);
272        }
273
274        if let Some(val) = &args.snapshot_id {
275            volume_builder.snapshot_id(Some(val.into()));
276        } else if args.no_snapshot_id {
277            volume_builder.snapshot_id(None);
278        }
279
280        if let Some(val) = &args.source_volid {
281            volume_builder.source_volid(Some(val.into()));
282        } else if args.no_source_volid {
283            volume_builder.source_volid(None);
284        }
285
286        if let Some(val) = &args.volume_type {
287            volume_builder.volume_type(Some(val.into()));
288        } else if args.no_volume_type {
289            volume_builder.volume_type(None);
290        }
291
292        ep_builder.volume(
293            volume_builder
294                .build()
295                .wrap_err("error preparing the request data")?,
296        );
297
298        let ep = ep_builder
299            .build()
300            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
301
302        let data: serde_json::Value = ep.query_async(client).await?;
303
304        op.output_single::<response::create::VolumeResponse>(data.clone())?;
305        // Show command specific hints
306        op.show_command_hint()?;
307        Ok(())
308    }
309}