Skip to main content

openstack_cli_block_storage/v3/attachment/
create_354.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 Attachment command [microversion = 3.54]
19//!
20//! Wraps invoking of the `v3/attachments` 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_cli_core::common::parse_key_val;
33use openstack_sdk::api::QueryAsync;
34use openstack_sdk::api::block_storage::v3::attachment::create_354;
35use openstack_types::block_storage::v3::attachment::response;
36use serde_json::Value;
37
38/// Create an attachment.
39///
40/// This method can be used to create an empty attachment (reserve) or to
41/// create and initialize a volume attachment based on the provided input
42/// parameters.
43///
44/// If the caller does not yet have the connector information but needs to
45/// reserve an attachment for the volume (ie Nova BootFromVolume) the create
46/// can be called with just the volume-uuid and the server identifier. This
47/// will reserve an attachment, mark the volume as reserved and prevent any new
48/// attachment_create calls from being made until the attachment is updated
49/// (completed).
50///
51/// The alternative is that the connection can be reserved and initialized all
52/// at once with a single call if the caller has all of the required
53/// information (connector data) at the time of the call.
54///
55/// NOTE: In Nova terms server == instance, the server_id parameter referenced
56/// below is the UUID of the Instance, for non-nova consumers this can be a
57/// server UUID or some other arbitrary unique identifier.
58///
59/// Starting from microversion 3.54, we can pass the attach mode as argument in
60/// the request body.
61///
62/// Expected format of the input parameter 'body':
63///
64/// ```text
65/// {
66///     "attachment":
67///     {
68///         "volume_uuid": "volume-uuid",
69///         "instance_uuid": "null|nova-server-uuid",
70///         "connector": "null|<connector-object>",
71///         "mode": "null|rw|ro"
72///     }
73/// }
74/// ```
75///
76/// Example connector:
77///
78/// ```text
79/// {
80///     "connector":
81///     {
82///         "initiator": "iqn.1993-08.org.debian:01:cad181614cec",
83///         "ip": "192.168.1.20",
84///         "platform": "x86_64",
85///         "host": "tempest-1",
86///         "os_type": "linux2",
87///         "multipath": false,
88///         "mountpoint": "/dev/vdb",
89///         "mode": "null|rw|ro"
90///     }
91/// }
92/// ```
93///
94/// NOTE all that's required for a reserve is volume_uuid and an instance_uuid.
95///
96/// returns: A summary view of the attachment object
97#[derive(Args)]
98pub struct AttachmentCommand {
99    /// Request Query parameters
100    #[command(flatten)]
101    query: QueryParameters,
102
103    /// Path parameters
104    #[command(flatten)]
105    path: PathParameters,
106
107    /// An attachment object.
108    #[command(flatten)]
109    attachment: Attachment,
110}
111
112/// Query parameters
113#[derive(Args)]
114struct QueryParameters {}
115
116/// Path parameters
117#[derive(Args)]
118struct PathParameters {}
119
120#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
121enum Mode {
122    Ro,
123    Rw,
124}
125
126/// Attachment Body data
127#[derive(Args, Clone)]
128struct Attachment {
129    /// The `connector` object.
130    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
131    connector: Option<Vec<(String, Value)>>,
132
133    /// The UUID of the volume which the attachment belongs to.
134    #[arg(help_heading = "Body parameters", long)]
135    instance_uuid: Option<String>,
136
137    /// The attach mode of attachment, acceptable values are read-only (‘ro’)
138    /// and read-and-write (‘rw’).
139    ///
140    /// **New in version 3.54**
141    #[arg(help_heading = "Body parameters", long)]
142    mode: Option<Mode>,
143
144    /// The UUID of the volume which the attachment belongs to.
145    #[arg(help_heading = "Body parameters", long)]
146    volume_uuid: String,
147}
148
149impl AttachmentCommand {
150    /// Perform command action
151    pub async fn take_action<C: CliArgs>(
152        &self,
153        parsed_args: &C,
154        client: &mut AsyncOpenStack,
155    ) -> Result<(), OpenStackCliError> {
156        info!("Create Attachment");
157
158        let op = OutputProcessor::from_args(
159            parsed_args,
160            Some("block-storage.attachment"),
161            Some("create"),
162        );
163        op.validate_args(parsed_args)?;
164
165        let mut ep_builder = create_354::Request::builder();
166        ep_builder.header(
167            http::header::HeaderName::from_static("openstack-api-version"),
168            http::header::HeaderValue::from_static("volume 3.54"),
169        );
170
171        // Set body parameters
172        // Set Request.attachment data
173        let args = &self.attachment;
174        let mut attachment_builder = create_354::AttachmentBuilder::default();
175        if let Some(val) = &args.connector {
176            attachment_builder.connector(val.iter().cloned());
177        }
178
179        if let Some(val) = &args.instance_uuid {
180            attachment_builder.instance_uuid(val);
181        }
182
183        if let Some(val) = &args.mode {
184            let tmp = match val {
185                Mode::Ro => create_354::Mode::Ro,
186                Mode::Rw => create_354::Mode::Rw,
187            };
188            attachment_builder.mode(tmp);
189        }
190
191        attachment_builder.volume_uuid(&args.volume_uuid);
192
193        ep_builder.attachment(
194            attachment_builder
195                .build()
196                .wrap_err("error preparing the request data")?,
197        );
198
199        let ep = ep_builder
200            .build()
201            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
202
203        let data: serde_json::Value = ep.query_async(client).await?;
204
205        op.output_single::<response::create::AttachmentResponse>(data.clone())?;
206        // Show command specific hints
207        op.show_command_hint()?;
208        Ok(())
209    }
210}