Skip to main content

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