Skip to main content

openstack_cli_network/v2/segment/
create.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//! Create Segment command
19//!
20//! Wraps invoking of the `v2.0/segments` with `POST` 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::network::v2::segment::create;
33use openstack_types::network::v2::segment::response;
34
35/// Creates a segment.
36///
37/// Normal response codes: 201
38///
39/// Error response codes: 400, 401
40#[derive(Args)]
41#[command(about = "Create segment")]
42pub struct SegmentCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50
51    #[command(flatten)]
52    segment: Segment,
53}
54
55/// Query parameters
56#[derive(Args)]
57struct QueryParameters {}
58
59/// Path parameters
60#[derive(Args)]
61struct PathParameters {}
62/// Segment Body data
63#[derive(Args, Clone)]
64struct Segment {
65    /// A human-readable description for the resource. Default is an empty
66    /// string.
67    #[arg(help_heading = "Body parameters", long)]
68    description: Option<String>,
69
70    /// Human-readable name of the segment.
71    #[arg(help_heading = "Body parameters", long)]
72    name: Option<String>,
73
74    /// Set explicit NULL for the name
75    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "name")]
76    no_name: bool,
77
78    /// The ID of the attached network.
79    #[arg(help_heading = "Body parameters", long)]
80    network_id: Option<String>,
81
82    /// The type of physical network that maps to this network resource. For
83    /// example, `flat`, `vlan`, `vxlan`, or `gre`.
84    #[arg(help_heading = "Body parameters", long)]
85    network_type: Option<String>,
86
87    /// The physical network where this network/segment is implemented.
88    #[arg(help_heading = "Body parameters", long)]
89    physical_network: Option<String>,
90
91    /// The ID of the isolated segment on the physical network. The
92    /// `network_type` attribute defines the segmentation model. For example,
93    /// if the `network_type` value is vlan, this ID is a vlan identifier. If
94    /// the `network_type` value is gre, this ID is a gre key. `Note` that only
95    /// the segmentation-id of VLAN type networks can be changed!
96    #[arg(help_heading = "Body parameters", long)]
97    segmentation_id: Option<String>,
98
99    #[arg(help_heading = "Body parameters", long)]
100    tenant_id: Option<String>,
101}
102
103impl SegmentCommand {
104    /// Perform command action
105    pub async fn take_action<C: CliArgs>(
106        &self,
107        parsed_args: &C,
108        client: &mut AsyncOpenStack,
109    ) -> Result<(), OpenStackCliError> {
110        info!("Create Segment");
111
112        let op = OutputProcessor::from_args(parsed_args, Some("network.segment"), Some("create"));
113        op.validate_args(parsed_args)?;
114
115        let mut ep_builder = create::Request::builder();
116
117        // Set body parameters
118        // Set Request.segment data
119        let args = &self.segment;
120        let mut segment_builder = create::SegmentBuilder::default();
121        if let Some(val) = &args.description {
122            segment_builder.description(val);
123        }
124
125        if let Some(val) = &args.name {
126            segment_builder.name(Some(val.into()));
127        } else if args.no_name {
128            segment_builder.name(None);
129        }
130
131        if let Some(val) = &args.network_id {
132            segment_builder.network_id(val);
133        }
134
135        if let Some(val) = &args.network_type {
136            segment_builder.network_type(val);
137        }
138
139        if let Some(val) = &args.physical_network {
140            segment_builder.physical_network(val);
141        }
142
143        if let Some(val) = &args.segmentation_id {
144            segment_builder.segmentation_id(val);
145        }
146
147        if let Some(val) = &args.tenant_id {
148            segment_builder.tenant_id(val);
149        }
150
151        ep_builder.segment(
152            segment_builder
153                .build()
154                .wrap_err("error preparing the request data")?,
155        );
156
157        let ep = ep_builder
158            .build()
159            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
160
161        let data: serde_json::Value = ep.query_async(client).await?;
162
163        op.output_single::<response::create::SegmentResponse>(data.clone())?;
164        // Show command specific hints
165        op.show_command_hint()?;
166        Ok(())
167    }
168}