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    /// The ID of the attached network.
75    #[arg(help_heading = "Body parameters", long)]
76    network_id: Option<String>,
77
78    /// The type of physical network that maps to this network resource. For
79    /// example, `flat`, `vlan`, `vxlan`, or `gre`.
80    #[arg(help_heading = "Body parameters", long)]
81    network_type: Option<String>,
82
83    /// The physical network where this network/segment is implemented.
84    #[arg(help_heading = "Body parameters", long)]
85    physical_network: Option<String>,
86
87    /// The ID of the isolated segment on the physical network. The
88    /// `network_type` attribute defines the segmentation model. For example,
89    /// if the `network_type` value is vlan, this ID is a vlan identifier. If
90    /// the `network_type` value is gre, this ID is a gre key. `Note` that only
91    /// the segmentation-id of VLAN type networks can be changed!
92    #[arg(help_heading = "Body parameters", long)]
93    segmentation_id: Option<String>,
94
95    #[arg(help_heading = "Body parameters", long)]
96    tenant_id: Option<String>,
97}
98
99impl SegmentCommand {
100    /// Perform command action
101    pub async fn take_action<C: CliArgs>(
102        &self,
103        parsed_args: &C,
104        client: &mut AsyncOpenStack,
105    ) -> Result<(), OpenStackCliError> {
106        info!("Create Segment");
107
108        let op = OutputProcessor::from_args(parsed_args, Some("network.segment"), Some("create"));
109        op.validate_args(parsed_args)?;
110
111        let mut ep_builder = create::Request::builder();
112
113        // Set body parameters
114        // Set Request.segment data
115        let args = &self.segment;
116        let mut segment_builder = create::SegmentBuilder::default();
117        if let Some(val) = &args.description {
118            segment_builder.description(val);
119        }
120
121        if let Some(val) = &args.name {
122            segment_builder.name(val);
123        }
124
125        if let Some(val) = &args.network_id {
126            segment_builder.network_id(val);
127        }
128
129        if let Some(val) = &args.network_type {
130            segment_builder.network_type(val);
131        }
132
133        if let Some(val) = &args.physical_network {
134            segment_builder.physical_network(val);
135        }
136
137        if let Some(val) = &args.segmentation_id {
138            segment_builder.segmentation_id(val);
139        }
140
141        if let Some(val) = &args.tenant_id {
142            segment_builder.tenant_id(val);
143        }
144
145        ep_builder.segment(
146            segment_builder
147                .build()
148                .wrap_err("error preparing the request data")?,
149        );
150
151        let ep = ep_builder
152            .build()
153            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
154
155        let data: serde_json::Value = ep.query_async(client).await?;
156
157        op.output_single::<response::create::SegmentResponse>(data.clone())?;
158        // Show command specific hints
159        op.show_command_hint()?;
160        Ok(())
161    }
162}