Skip to main content

openstack_cli_network/v2/address_group/
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 AddressGroup command
19//!
20//! Wraps invoking of the `v2.0/address-groups/{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::network::v2::address_group::find;
34use openstack_sdk::api::network::v2::address_group::set;
35use openstack_types::network::v2::address_group::response;
36
37/// Updates an address group.
38///
39/// Normal response codes: 200
40///
41/// Error response codes: 400, 401, 403, 404, 412
42#[derive(Args)]
43#[command(about = "Update an address group")]
44pub struct AddressGroupCommand {
45    /// Request Query parameters
46    #[command(flatten)]
47    query: QueryParameters,
48
49    /// Path parameters
50    #[command(flatten)]
51    path: PathParameters,
52
53    /// An `address group` object.
54    #[command(flatten)]
55    address_group: AddressGroup,
56}
57
58/// Query parameters
59#[derive(Args)]
60struct QueryParameters {}
61
62/// Path parameters
63#[derive(Args)]
64struct PathParameters {
65    /// id parameter for /v2.0/address-groups/{id} API
66    #[arg(
67        help_heading = "Path parameters",
68        id = "path_param_id",
69        value_name = "ID"
70    )]
71    id: String,
72}
73/// AddressGroup Body data
74#[derive(Args, Clone)]
75struct AddressGroup {
76    /// A human-readable description for the resource.
77    #[arg(help_heading = "Body parameters", long)]
78    description: Option<String>,
79
80    /// Human-readable name of the resource. Default is an empty string.
81    #[arg(help_heading = "Body parameters", long)]
82    name: Option<String>,
83}
84
85impl AddressGroupCommand {
86    /// Perform command action
87    pub async fn take_action<C: CliArgs>(
88        &self,
89        parsed_args: &C,
90        client: &mut AsyncOpenStack,
91    ) -> Result<(), OpenStackCliError> {
92        info!("Set AddressGroup");
93
94        let op =
95            OutputProcessor::from_args(parsed_args, Some("network.address_group"), Some("set"));
96        op.validate_args(parsed_args)?;
97
98        let mut find_builder = find::Request::builder();
99
100        find_builder.id(&self.path.id);
101
102        let find_ep = find_builder
103            .build()
104            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
105        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
106
107        let mut ep_builder = set::Request::builder();
108
109        let resource_id = find_data["id"]
110            .as_str()
111            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
112            .to_string();
113        ep_builder.id(resource_id.clone());
114
115        // Set body parameters
116        // Set Request.address_group data
117        let args = &self.address_group;
118        let mut address_group_builder = set::AddressGroupBuilder::default();
119        if let Some(val) = &args.description {
120            address_group_builder.description(val);
121        }
122
123        if let Some(val) = &args.name {
124            address_group_builder.name(val);
125        }
126
127        ep_builder.address_group(
128            address_group_builder
129                .build()
130                .wrap_err("error preparing the request data")?,
131        );
132
133        let ep = ep_builder
134            .build()
135            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
136
137        let data: serde_json::Value = ep.query_async(client).await?;
138
139        op.output_single::<response::set::AddressGroupResponse>(data.clone())?;
140        // Show command specific hints
141        op.show_command_hint()?;
142        Ok(())
143    }
144}