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    /// Set explicit NULL for the name
85    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
86    no_name: bool,
87}
88
89impl SegmentCommand {
90    /// Perform command action
91    pub async fn take_action<C: CliArgs>(
92        &self,
93        parsed_args: &C,
94        client: &mut AsyncOpenStack,
95    ) -> Result<(), OpenStackCliError> {
96        info!("Set Segment");
97
98        let op = OutputProcessor::from_args(parsed_args, Some("network.segment"), Some("set"));
99        op.validate_args(parsed_args)?;
100
101        let mut find_builder = find::Request::builder();
102
103        find_builder.id(&self.path.id);
104
105        let find_ep = find_builder
106            .build()
107            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
108        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
109
110        let mut ep_builder = set::Request::builder();
111
112        let resource_id = find_data["id"]
113            .as_str()
114            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
115            .to_string();
116        ep_builder.id(resource_id.clone());
117
118        // Set body parameters
119        // Set Request.segment data
120        let args = &self.segment;
121        let mut segment_builder = set::SegmentBuilder::default();
122        if let Some(val) = &args.description {
123            segment_builder.description(val);
124        }
125
126        if let Some(val) = &args.name {
127            segment_builder.name(Some(val.into()));
128        } else if args.no_name {
129            segment_builder.name(None);
130        }
131
132        ep_builder.segment(
133            segment_builder
134                .build()
135                .wrap_err("error preparing the request data")?,
136        );
137
138        let ep = ep_builder
139            .build()
140            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
141
142        let data: serde_json::Value = ep.query_async(client).await?;
143
144        op.output_single::<response::set::SegmentResponse>(data.clone())?;
145        // Show command specific hints
146        op.show_command_hint()?;
147        Ok(())
148    }
149}