Skip to main content

openstack_cli_network/v2/floatingip/
set.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//! Set Floatingip command
19//!
20//! Wraps invoking of the `v2.0/floatingips/{id}` with `PUT` 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::network::v2::floatingip::set;
33use openstack_types::network::v2::floatingip::response;
34
35/// Updates a floating IP and its association with an internal port.
36///
37/// The association process is the same as the process for the create floating
38/// IP operation.
39///
40/// To disassociate a floating IP from a port, set the `port_id` attribute to
41/// null or omit it from the request body.
42///
43/// This example updates a floating IP:
44///
45/// Depending on the request body that you submit, this request associates a
46/// port with or disassociates a port from a floating IP.
47///
48/// Normal response codes: 200
49///
50/// Error response codes: 400, 401, 404, 409, 412
51#[derive(Args)]
52#[command(about = "Update floating IP")]
53pub struct FloatingipCommand {
54    /// Request Query parameters
55    #[command(flatten)]
56    query: QueryParameters,
57
58    /// Path parameters
59    #[command(flatten)]
60    path: PathParameters,
61
62    /// A `floatingip` object. When you associate a floating IP address with a
63    /// VM, the instance has the same public IP address each time that it
64    /// boots, basically to maintain a consistent IP address for maintaining
65    /// DNS assignment.
66    #[command(flatten)]
67    floatingip: Floatingip,
68}
69
70/// Query parameters
71#[derive(Args)]
72struct QueryParameters {}
73
74/// Path parameters
75#[derive(Args)]
76struct PathParameters {
77    /// id parameter for /v2.0/floatingips/{id} API
78    #[arg(
79        help_heading = "Path parameters",
80        id = "path_param_id",
81        value_name = "ID"
82    )]
83    id: String,
84}
85/// Floatingip Body data
86#[derive(Args, Clone)]
87struct Floatingip {
88    /// A human-readable description for the resource. Default is an empty
89    /// string.
90    #[arg(help_heading = "Body parameters", long)]
91    description: Option<String>,
92
93    /// The fixed IP address that is associated with the floating IP. If an
94    /// internal port has multiple associated IP addresses, the service chooses
95    /// the first IP address unless you explicitly define a fixed IP address in
96    /// the `fixed_ip_address` parameter.
97    #[arg(help_heading = "Body parameters", long)]
98    fixed_ip_address: Option<String>,
99
100    /// Set explicit NULL for the fixed_ip_address
101    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "fixed_ip_address")]
102    no_fixed_ip_address: bool,
103
104    /// The ID of a port associated with the floating IP. To associate the
105    /// floating IP with a fixed IP, you must specify the ID of the internal
106    /// port. To disassociate the floating IP, `null` should be specified.
107    #[arg(help_heading = "Body parameters", long)]
108    port_id: Option<String>,
109
110    /// Set explicit NULL for the port_id
111    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "port_id")]
112    no_port_id: bool,
113
114    #[arg(help_heading = "Body parameters", long)]
115    qos_policy_id: Option<String>,
116
117    /// Set explicit NULL for the qos_policy_id
118    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "qos_policy_id")]
119    no_qos_policy_id: bool,
120}
121
122impl FloatingipCommand {
123    /// Perform command action
124    pub async fn take_action<C: CliArgs>(
125        &self,
126        parsed_args: &C,
127        client: &mut AsyncOpenStack,
128    ) -> Result<(), OpenStackCliError> {
129        info!("Set Floatingip");
130
131        let op = OutputProcessor::from_args(parsed_args, Some("network.floatingip"), Some("set"));
132        op.validate_args(parsed_args)?;
133
134        let mut ep_builder = set::Request::builder();
135
136        ep_builder.id(&self.path.id);
137
138        // Set body parameters
139        // Set Request.floatingip data
140        let args = &self.floatingip;
141        let mut floatingip_builder = set::FloatingipBuilder::default();
142        if let Some(val) = &args.description {
143            floatingip_builder.description(val);
144        }
145
146        if let Some(val) = &args.fixed_ip_address {
147            floatingip_builder.fixed_ip_address(Some(val.into()));
148        } else if args.no_fixed_ip_address {
149            floatingip_builder.fixed_ip_address(None);
150        }
151
152        if let Some(val) = &args.port_id {
153            floatingip_builder.port_id(Some(val.into()));
154        } else if args.no_port_id {
155            floatingip_builder.port_id(None);
156        }
157
158        if let Some(val) = &args.qos_policy_id {
159            floatingip_builder.qos_policy_id(Some(val.into()));
160        } else if args.no_qos_policy_id {
161            floatingip_builder.qos_policy_id(None);
162        }
163
164        ep_builder.floatingip(
165            floatingip_builder
166                .build()
167                .wrap_err("error preparing the request data")?,
168        );
169
170        let ep = ep_builder
171            .build()
172            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
173
174        let data: serde_json::Value = ep.query_async(client).await?;
175
176        op.output_single::<response::set::FloatingipResponse>(data.clone())?;
177        // Show command specific hints
178        op.show_command_hint()?;
179        Ok(())
180    }
181}