Skip to main content

openstack_cli_compute/v2/keypair/
create_20.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 Keypair command [microversion = 2.0]
19//!
20//! Wraps invoking of the `v2.1/os-keypairs` 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_sdk::api::QueryAsync;
32use openstack_sdk::api::compute::v2::keypair::create_20;
33use openstack_types::compute::v2::keypair::response;
34
35/// Imports (or generates) a keypair.
36///
37/// Normal response codes: 200, 201
38///
39/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
40/// conflict(409)
41#[derive(Args)]
42#[command(about = "Import (or create) Keypair (microversion = 2.0)")]
43pub struct KeypairCommand {
44    /// Request Query parameters
45    #[command(flatten)]
46    query: QueryParameters,
47
48    /// Path parameters
49    #[command(flatten)]
50    path: PathParameters,
51
52    /// Keypair object
53    #[command(flatten)]
54    keypair: Keypair,
55}
56
57/// Query parameters
58#[derive(Args)]
59struct QueryParameters {}
60
61/// Path parameters
62#[derive(Args)]
63struct PathParameters {}
64/// Keypair Body data
65#[derive(Args, Clone)]
66struct Keypair {
67    /// A name for the keypair which will be used to reference it later.
68    ///
69    /// Note
70    ///
71    /// Since microversion 2.92, allowed characters are ASCII letters
72    /// `[a-zA-Z]`, digits `[0-9]` and the following special characters:
73    /// `[@._- ]`.
74    #[arg(help_heading = "Body parameters", long)]
75    name: String,
76
77    /// The public ssh key to import. Was optional before microversion 2.92 :
78    /// if you were omitting this value, a keypair was generated for you.
79    #[arg(help_heading = "Body parameters", long)]
80    public_key: Option<String>,
81}
82
83impl KeypairCommand {
84    /// Perform command action
85    pub async fn take_action<C: CliArgs>(
86        &self,
87        parsed_args: &C,
88        client: &mut AsyncOpenStack,
89    ) -> Result<(), OpenStackCliError> {
90        info!("Create Keypair");
91
92        let op = OutputProcessor::from_args(parsed_args, Some("compute.keypair"), Some("create"));
93        op.validate_args(parsed_args)?;
94
95        let mut ep_builder = create_20::Request::builder();
96        ep_builder.header(
97            http::header::HeaderName::from_static("openstack-api-version"),
98            http::header::HeaderValue::from_static("compute 2.0"),
99        );
100
101        // Set body parameters
102        // Set Request.keypair data
103        let args = &self.keypair;
104        let mut keypair_builder = create_20::KeypairBuilder::default();
105
106        keypair_builder.name(&args.name);
107
108        if let Some(val) = &args.public_key {
109            keypair_builder.public_key(val);
110        }
111
112        ep_builder.keypair(
113            keypair_builder
114                .build()
115                .wrap_err("error preparing the request data")?,
116        );
117
118        let ep = ep_builder
119            .build()
120            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
121
122        let data: serde_json::Value = ep.query_async(client).await?;
123
124        op.output_single::<response::create_20::KeypairResponse>(data.clone())?;
125        // Show command specific hints
126        op.show_command_hint()?;
127        Ok(())
128    }
129}