Skip to main content

openstack_cli_load_balancer/v2/loadbalancer/
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 Loadbalancer command
19//!
20//! Wraps invoking of the `v2/lbaas/loadbalancers/{loadbalancer_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::find;
33use openstack_sdk::api::load_balancer::v2::loadbalancer::find;
34use openstack_sdk::api::load_balancer::v2::loadbalancer::set;
35use openstack_types::load_balancer::v2::loadbalancer::response;
36
37/// Updates a load balancer.
38///
39/// If the request is valid, the service returns the `Accepted (202)` response
40/// code. To confirm the update, check that the load balancer provisioning
41/// status is `ACTIVE`. If the status is `PENDING_UPDATE`, use a GET operation
42/// to poll the load balancer object for changes.
43///
44/// This operation returns the updated load balancer object with the `ACTIVE`,
45/// `PENDING_UPDATE`, or `ERROR` provisioning status.
46#[derive(Args)]
47#[command(about = "Update a Load Balancer")]
48pub struct LoadbalancerCommand {
49    /// Request Query parameters
50    #[command(flatten)]
51    query: QueryParameters,
52
53    /// Path parameters
54    #[command(flatten)]
55    path: PathParameters,
56
57    /// A load balancer object.
58    #[command(flatten)]
59    loadbalancer: Loadbalancer,
60}
61
62/// Query parameters
63#[derive(Args)]
64struct QueryParameters {}
65
66/// Path parameters
67#[derive(Args)]
68struct PathParameters {
69    /// loadbalancer_id parameter for /v2/lbaas/loadbalancers/{loadbalancer_id}
70    /// API
71    #[arg(
72        help_heading = "Path parameters",
73        id = "path_param_id",
74        value_name = "ID"
75    )]
76    id: String,
77}
78/// Loadbalancer Body data
79#[derive(Args, Clone)]
80struct Loadbalancer {
81    /// The administrative state of the resource, which is up (`true`) or down
82    /// (`false`).
83    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
84    admin_state_up: Option<bool>,
85
86    /// A human-readable description for the resource.
87    #[arg(help_heading = "Body parameters", long)]
88    description: Option<String>,
89
90    /// Human-readable name of the resource.
91    #[arg(help_heading = "Body parameters", long)]
92    name: Option<String>,
93
94    /// A list of simple strings assigned to the resource.
95    ///
96    /// **New in version 2.5**
97    ///
98    /// Parameter is an array, may be provided multiple times.
99    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
100    tags: Option<Vec<String>>,
101
102    /// The ID of the QoS Policy which will apply to the Virtual IP (VIP).
103    #[arg(help_heading = "Body parameters", long)]
104    vip_qos_policy_id: Option<String>,
105
106    /// Parameter is an array, may be provided multiple times.
107    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
108    vip_sg_ids: Option<Vec<String>>,
109}
110
111impl LoadbalancerCommand {
112    /// Perform command action
113    pub async fn take_action<C: CliArgs>(
114        &self,
115        parsed_args: &C,
116        client: &mut AsyncOpenStack,
117    ) -> Result<(), OpenStackCliError> {
118        info!("Set Loadbalancer");
119
120        let op = OutputProcessor::from_args(
121            parsed_args,
122            Some("load-balancer.loadbalancer"),
123            Some("set"),
124        );
125        op.validate_args(parsed_args)?;
126
127        let mut find_builder = find::Request::builder();
128
129        find_builder.id(&self.path.id);
130
131        let find_ep = find_builder
132            .build()
133            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
134        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
135
136        let mut ep_builder = set::Request::builder();
137
138        let resource_id = find_data["id"]
139            .as_str()
140            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
141            .to_string();
142        ep_builder.id(resource_id.clone());
143
144        // Set body parameters
145        // Set Request.loadbalancer data
146        let args = &self.loadbalancer;
147        let mut loadbalancer_builder = set::LoadbalancerBuilder::default();
148        if let Some(val) = &args.admin_state_up {
149            loadbalancer_builder.admin_state_up(*val);
150        }
151
152        if let Some(val) = &args.description {
153            loadbalancer_builder.description(val);
154        }
155
156        if let Some(val) = &args.name {
157            loadbalancer_builder.name(val);
158        }
159
160        if let Some(val) = &args.tags {
161            loadbalancer_builder.tags(val.iter().map(Into::into).collect::<Vec<_>>());
162        }
163
164        if let Some(val) = &args.vip_qos_policy_id {
165            loadbalancer_builder.vip_qos_policy_id(val);
166        }
167
168        if let Some(val) = &args.vip_sg_ids {
169            loadbalancer_builder.vip_sg_ids(val.iter().map(Into::into).collect::<Vec<_>>());
170        }
171
172        ep_builder.loadbalancer(
173            loadbalancer_builder
174                .build()
175                .wrap_err("error preparing the request data")?,
176        );
177
178        let ep = ep_builder
179            .build()
180            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
181
182        let data: serde_json::Value = ep.query_async(client).await?;
183
184        op.output_single::<response::set::LoadbalancerResponse>(data.clone())?;
185        // Show command specific hints
186        op.show_command_hint()?;
187        Ok(())
188    }
189}