Skip to main content

openstack_cli_network/v2/local_ip/
create.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 LocalIp command
19//!
20//! Wraps invoking of the `v2.0/local-ips` 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::network::v2::local_ip::create;
34use openstack_types::network::v2::local_ip::response;
35
36/// Command without description in OpenAPI
37#[derive(Args)]
38pub struct LocalIpCommand {
39    /// Request Query parameters
40    #[command(flatten)]
41    query: QueryParameters,
42
43    /// Path parameters
44    #[command(flatten)]
45    path: PathParameters,
46
47    #[command(flatten)]
48    local_ip: LocalIp,
49}
50
51/// Query parameters
52#[derive(Args)]
53struct QueryParameters {}
54
55/// Path parameters
56#[derive(Args)]
57struct PathParameters {}
58
59#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
60enum IpMode {
61    Passthrough,
62    Translate,
63}
64
65/// LocalIp Body data
66#[derive(Args, Clone)]
67struct LocalIp {
68    #[arg(help_heading = "Body parameters", long)]
69    description: Option<String>,
70
71    #[arg(help_heading = "Body parameters", long)]
72    ip_mode: Option<IpMode>,
73
74    #[arg(help_heading = "Body parameters", long)]
75    local_ip_address: Option<String>,
76
77    /// Set explicit NULL for the local_ip_address
78    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "local_ip_address")]
79    no_local_ip_address: bool,
80
81    #[arg(help_heading = "Body parameters", long)]
82    local_port_id: Option<String>,
83
84    #[arg(help_heading = "Body parameters", long)]
85    name: Option<String>,
86
87    #[arg(help_heading = "Body parameters", long)]
88    network_id: Option<String>,
89
90    #[arg(help_heading = "Body parameters", long)]
91    project_id: Option<String>,
92}
93
94impl LocalIpCommand {
95    /// Perform command action
96    pub async fn take_action<C: CliArgs>(
97        &self,
98        parsed_args: &C,
99        client: &mut AsyncOpenStack,
100    ) -> Result<(), OpenStackCliError> {
101        info!("Create LocalIp");
102
103        let op = OutputProcessor::from_args(parsed_args, Some("network.local_ip"), Some("create"));
104        op.validate_args(parsed_args)?;
105
106        let mut ep_builder = create::Request::builder();
107
108        // Set body parameters
109        // Set Request.local_ip data
110        let args = &self.local_ip;
111        let mut local_ip_builder = create::LocalIpBuilder::default();
112        if let Some(val) = &args.description {
113            local_ip_builder.description(val);
114        }
115
116        if let Some(val) = &args.ip_mode {
117            let tmp = match val {
118                IpMode::Passthrough => create::IpMode::Passthrough,
119                IpMode::Translate => create::IpMode::Translate,
120            };
121            local_ip_builder.ip_mode(tmp);
122        }
123
124        if let Some(val) = &args.local_ip_address {
125            local_ip_builder.local_ip_address(Some(val.into()));
126        } else if args.no_local_ip_address {
127            local_ip_builder.local_ip_address(None);
128        }
129
130        if let Some(val) = &args.local_port_id {
131            local_ip_builder.local_port_id(val);
132        }
133
134        if let Some(val) = &args.name {
135            local_ip_builder.name(val);
136        }
137
138        if let Some(val) = &args.network_id {
139            local_ip_builder.network_id(val);
140        }
141
142        if let Some(val) = &args.project_id {
143            local_ip_builder.project_id(val);
144        }
145
146        ep_builder.local_ip(
147            local_ip_builder
148                .build()
149                .wrap_err("error preparing the request data")?,
150        );
151
152        let ep = ep_builder
153            .build()
154            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
155
156        let data: serde_json::Value = ep.query_async(client).await?;
157
158        op.output_single::<response::create::LocalIpResponse>(data.clone())?;
159        // Show command specific hints
160        op.show_command_hint()?;
161        Ok(())
162    }
163}