Skip to main content

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