Skip to main content

openstack_cli_compute/v2/keypair/
create_22.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.2]
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_22;
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.2)")]
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
97impl KeypairCommand {
98    /// Perform command action
99    pub async fn take_action<C: CliArgs>(
100        &self,
101        parsed_args: &C,
102        client: &mut AsyncOpenStack,
103    ) -> Result<(), OpenStackCliError> {
104        info!("Create Keypair");
105
106        let op = OutputProcessor::from_args(parsed_args, Some("compute.keypair"), Some("create"));
107        op.validate_args(parsed_args)?;
108
109        let mut ep_builder = create_22::Request::builder();
110        ep_builder.header(
111            http::header::HeaderName::from_static("openstack-api-version"),
112            http::header::HeaderValue::from_static("compute 2.2"),
113        );
114
115        // Set body parameters
116        // Set Request.keypair data
117        let args = &self.keypair;
118        let mut keypair_builder = create_22::KeypairBuilder::default();
119
120        keypair_builder.name(&args.name);
121
122        if let Some(val) = &args.public_key {
123            keypair_builder.public_key(val);
124        }
125
126        if let Some(val) = &args._type {
127            let tmp = match val {
128                Type::Ssh => create_22::Type::Ssh,
129                Type::X509 => create_22::Type::X509,
130            };
131            keypair_builder._type(tmp);
132        }
133
134        ep_builder.keypair(
135            keypair_builder
136                .build()
137                .wrap_err("error preparing the request data")?,
138        );
139
140        let ep = ep_builder
141            .build()
142            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
143
144        let data: serde_json::Value = ep.query_async(client).await?;
145
146        op.output_single::<response::create_22::KeypairResponse>(data.clone())?;
147        // Show command specific hints
148        op.show_command_hint()?;
149        Ok(())
150    }
151}