Skip to main content

openstack_cli_load_balancer/v2/pool/member/
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 Member command
19//!
20//! Wraps invoking of the `v2/lbaas/pools/{pool_id}/members/{member_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::pool::member::find;
34use openstack_sdk::api::load_balancer::v2::pool::member::set;
35use openstack_types::load_balancer::v2::pool::member::response;
36
37/// Update an existing member.
38///
39/// If the request is valid, the service returns the `Accepted (202)` response
40/// code. To confirm the update, check that the member provisioning status is
41/// `ACTIVE`. If the status is `PENDING_UPDATE`, use a GET operation to poll
42/// the member object for changes.
43///
44/// Setting the member weight to `0` means that the member will not receive new
45/// requests but will finish any existing connections. This “drains” the
46/// backend member of active connections.
47///
48/// This operation returns the updated member object with the `ACTIVE`,
49/// `PENDING_UPDATE`, or `ERROR` provisioning status.
50#[derive(Args)]
51#[command(about = "Update a Member")]
52pub struct MemberCommand {
53    /// Request Query parameters
54    #[command(flatten)]
55    query: QueryParameters,
56
57    /// Path parameters
58    #[command(flatten)]
59    path: PathParameters,
60
61    /// Defines attributes that are acceptable of a PUT request.
62    #[command(flatten)]
63    member: Member,
64}
65
66/// Query parameters
67#[derive(Args)]
68struct QueryParameters {}
69
70/// Path parameters
71#[derive(Args)]
72struct PathParameters {
73    /// member_id parameter for /v2/lbaas/pools/{pool_id}/members/{member_id}
74    /// API
75    #[arg(
76        help_heading = "Path parameters",
77        id = "path_param_id",
78        value_name = "ID"
79    )]
80    id: String,
81
82    /// pool_id parameter for /v2/lbaas/pools/{pool_id}/members/{member_id} API
83    #[arg(
84        help_heading = "Path parameters",
85        id = "path_param_pool_id",
86        value_name = "POOL_ID"
87    )]
88    pool_id: String,
89}
90/// Member Body data
91#[derive(Args, Clone)]
92struct Member {
93    /// The administrative state of the resource, which is up (`true`) or down
94    /// (`false`). Default is `true`.
95    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
96    admin_state_up: Option<bool>,
97
98    /// Is the member a backup? Backup members only receive traffic when all
99    /// non-backup members are down.
100    ///
101    /// **New in version 2.1**
102    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
103    backup: Option<bool>,
104
105    /// An alternate IP address used for health monitoring a backend member.
106    /// Default is `null` which monitors the member `address`.
107    #[arg(help_heading = "Body parameters", long)]
108    monitor_address: Option<String>,
109
110    /// An alternate protocol port used for health monitoring a backend member.
111    /// Default is `null` which monitors the member `protocol_port`.
112    #[arg(help_heading = "Body parameters", long)]
113    monitor_port: Option<i32>,
114
115    /// Human-readable name of the resource.
116    #[arg(help_heading = "Body parameters", long)]
117    name: Option<String>,
118
119    /// A list of simple strings assigned to the resource.
120    ///
121    /// **New in version 2.5**
122    ///
123    /// Parameter is an array, may be provided multiple times.
124    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
125    tags: Option<Vec<String>>,
126
127    /// The weight of a member determines the portion of requests or
128    /// connections it services compared to the other members of the pool. For
129    /// example, a member with a weight of 10 receives five times as many
130    /// requests as a member with a weight of 2. A value of 0 means the member
131    /// does not receive new connections but continues to service existing
132    /// connections. A valid value is from `0` to `256`. Default is `1`.
133    #[arg(help_heading = "Body parameters", long)]
134    weight: Option<i32>,
135}
136
137impl MemberCommand {
138    /// Perform command action
139    pub async fn take_action<C: CliArgs>(
140        &self,
141        parsed_args: &C,
142        client: &mut AsyncOpenStack,
143    ) -> Result<(), OpenStackCliError> {
144        info!("Set Member");
145
146        let op =
147            OutputProcessor::from_args(parsed_args, Some("load-balancer.pool/member"), Some("set"));
148        op.validate_args(parsed_args)?;
149
150        let mut find_builder = find::Request::builder();
151
152        find_builder.id(&self.path.id);
153        find_builder.pool_id(&self.path.pool_id);
154
155        let find_ep = find_builder
156            .build()
157            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
158        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
159
160        let mut ep_builder = set::Request::builder();
161
162        let resource_id = find_data["id"]
163            .as_str()
164            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
165            .to_string();
166        ep_builder.id(resource_id.clone());
167        let resource_id = find_data["id"]
168            .as_str()
169            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
170            .to_string();
171        ep_builder.pool_id(resource_id.clone());
172
173        // Set body parameters
174        // Set Request.member data
175        let args = &self.member;
176        let mut member_builder = set::MemberBuilder::default();
177        if let Some(val) = &args.admin_state_up {
178            member_builder.admin_state_up(*val);
179        }
180
181        if let Some(val) = &args.backup {
182            member_builder.backup(*val);
183        }
184
185        if let Some(val) = &args.monitor_address {
186            member_builder.monitor_address(val);
187        }
188
189        if let Some(val) = &args.monitor_port {
190            member_builder.monitor_port(*val);
191        }
192
193        if let Some(val) = &args.name {
194            member_builder.name(val);
195        }
196
197        if let Some(val) = &args.tags {
198            member_builder.tags(val.iter().map(Into::into).collect::<Vec<_>>());
199        }
200
201        if let Some(val) = &args.weight {
202            member_builder.weight(*val);
203        }
204
205        ep_builder.member(
206            member_builder
207                .build()
208                .wrap_err("error preparing the request data")?,
209        );
210
211        let ep = ep_builder
212            .build()
213            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
214
215        let data: serde_json::Value = ep.query_async(client).await?;
216
217        op.output_single::<response::set::MemberResponse>(data.clone())?;
218        // Show command specific hints
219        op.show_command_hint()?;
220        Ok(())
221    }
222}