Skip to main content

openstack_cli_compute/v2/keypair/
create_210.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.10]
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 clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::compute::v2::keypair::create_210;
34use openstack_types::compute::v2::keypair::response;
35
36/// Imports (or generates) a keypair.
37///
38/// Normal response codes: 200, 201
39///
40/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
41/// conflict(409)
42#[derive(Args)]
43#[command(about = "Import (or create) Keypair (microversion = 2.10)")]
44pub struct KeypairCommand {
45    /// Request Query parameters
46    #[command(flatten)]
47    query: QueryParameters,
48
49    /// Path parameters
50    #[command(flatten)]
51    path: PathParameters,
52
53    /// Keypair object
54    #[command(flatten)]
55    keypair: Keypair,
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    Ssh,
69    X509,
70}
71
72/// Keypair Body data
73#[derive(Args, Clone)]
74struct Keypair {
75    /// A name for the keypair which will be used to reference it later.
76    ///
77    /// Note
78    ///
79    /// Since microversion 2.92, allowed characters are ASCII letters
80    /// `[a-zA-Z]`, digits `[0-9]` and the following special characters:
81    /// `[@._- ]`.
82    #[arg(help_heading = "Body parameters", long)]
83    name: String,
84
85    /// The public ssh key to import. Was optional before microversion 2.92 :
86    /// if you were omitting this value, a keypair was generated for you.
87    #[arg(help_heading = "Body parameters", long)]
88    public_key: Option<String>,
89
90    /// The type of the keypair. Allowed values are `ssh` or `x509`.
91    ///
92    /// **New in version 2.2**
93    #[arg(help_heading = "Body parameters", long)]
94    _type: Option<Type>,
95
96    /// The user_id for a keypair. This allows administrative users to upload
97    /// keys for other users than themselves.
98    ///
99    /// **New in version 2.10**
100    #[arg(help_heading = "Body parameters", long)]
101    user_id: Option<String>,
102}
103
104impl KeypairCommand {
105    /// Perform command action
106    pub async fn take_action<C: CliArgs>(
107        &self,
108        parsed_args: &C,
109        client: &mut AsyncOpenStack,
110    ) -> Result<(), OpenStackCliError> {
111        info!("Create Keypair");
112
113        let op = OutputProcessor::from_args(parsed_args, Some("compute.keypair"), Some("create"));
114        op.validate_args(parsed_args)?;
115
116        let mut ep_builder = create_210::Request::builder();
117        ep_builder.header(
118            http::header::HeaderName::from_static("openstack-api-version"),
119            http::header::HeaderValue::from_static("compute 2.10"),
120        );
121
122        // Set body parameters
123        // Set Request.keypair data
124        let args = &self.keypair;
125        let mut keypair_builder = create_210::KeypairBuilder::default();
126
127        keypair_builder.name(&args.name);
128
129        if let Some(val) = &args.public_key {
130            keypair_builder.public_key(val);
131        }
132
133        if let Some(val) = &args._type {
134            let tmp = match val {
135                Type::Ssh => create_210::Type::Ssh,
136                Type::X509 => create_210::Type::X509,
137            };
138            keypair_builder._type(tmp);
139        }
140
141        if let Some(val) = &args.user_id {
142            keypair_builder.user_id(val);
143        }
144
145        ep_builder.keypair(
146            keypair_builder
147                .build()
148                .wrap_err("error preparing the request data")?,
149        );
150
151        let ep = ep_builder
152            .build()
153            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
154
155        let data: serde_json::Value = ep.query_async(client).await?;
156
157        op.output_single::<response::create_22::KeypairResponse>(data.clone())?;
158        // Show command specific hints
159        op.show_command_hint()?;
160        Ok(())
161    }
162}