openstack_cli/network/v2/address_scope/
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 AddressScope command
19//!
20//! Wraps invoking of the `v2.0/address-scopes/{id}` with `PUT` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_sdk::AsyncOpenStack;
26
27use crate::Cli;
28use crate::OpenStackCliError;
29use crate::output::OutputProcessor;
30
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::find;
33use openstack_sdk::api::network::v2::address_scope::find;
34use openstack_sdk::api::network::v2::address_scope::set;
35use openstack_types::network::v2::address_scope::response::set::AddressScopeResponse;
36
37/// Updates an address scope.
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 scope")]
44pub struct AddressScopeCommand {
45    /// Request Query parameters
46    #[command(flatten)]
47    query: QueryParameters,
48
49    /// Path parameters
50    #[command(flatten)]
51    path: PathParameters,
52
53    /// An `address scope` object.
54    #[command(flatten)]
55    address_scope: AddressScope,
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-scopes/{id} API
66    #[arg(
67        help_heading = "Path parameters",
68        id = "path_param_id",
69        value_name = "ID"
70    )]
71    id: String,
72}
73/// AddressScope Body data
74#[derive(Args, Clone)]
75struct AddressScope {
76    /// Human-readable name of the resource. Default is an empty string.
77    #[arg(help_heading = "Body parameters", long)]
78    name: Option<String>,
79
80    /// Indicates whether this resource is shared across all projects. By
81    /// default, only administrative users can change this value.
82    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
83    shared: Option<bool>,
84}
85
86impl AddressScopeCommand {
87    /// Perform command action
88    pub async fn take_action(
89        &self,
90        parsed_args: &Cli,
91        client: &mut AsyncOpenStack,
92    ) -> Result<(), OpenStackCliError> {
93        info!("Set AddressScope");
94
95        let op =
96            OutputProcessor::from_args(parsed_args, Some("network.address_scope"), Some("set"));
97        op.validate_args(parsed_args)?;
98
99        let mut find_builder = find::Request::builder();
100
101        find_builder.id(&self.path.id);
102
103        let find_ep = find_builder
104            .build()
105            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
106        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
107
108        let mut ep_builder = set::Request::builder();
109
110        let resource_id = find_data["id"]
111            .as_str()
112            .expect("Resource ID is a string")
113            .to_string();
114        ep_builder.id(resource_id.clone());
115
116        // Set body parameters
117        // Set Request.address_scope data
118        let args = &self.address_scope;
119        let mut address_scope_builder = set::AddressScopeBuilder::default();
120        if let Some(val) = &args.name {
121            address_scope_builder.name(val);
122        }
123
124        if let Some(val) = &args.shared {
125            address_scope_builder.shared(*val);
126        }
127
128        ep_builder.address_scope(address_scope_builder.build().unwrap());
129
130        let ep = ep_builder
131            .build()
132            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
133
134        let data = ep.query_async(client).await?;
135        op.output_single::<AddressScopeResponse>(data)?;
136        // Show command specific hints
137        op.show_command_hint()?;
138        Ok(())
139    }
140}