Skip to main content

openstack_cli_network/v2/network/
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 Network command
19//!
20//! Wraps invoking of the `v2.0/networks` 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::network::create;
33use openstack_types::network::v2::network::response;
34use serde_json::Value;
35
36/// Creates a network.
37///
38/// A request body is optional. An administrative user can specify another
39/// project ID, which is the project that owns the network, in the request
40/// body.
41///
42/// Normal response codes: 201
43///
44/// Error response codes: 400, 401
45#[derive(Args)]
46#[command(about = "Create network")]
47pub struct NetworkCommand {
48    /// Request Query parameters
49    #[command(flatten)]
50    query: QueryParameters,
51
52    /// Path parameters
53    #[command(flatten)]
54    path: PathParameters,
55
56    /// A `network` object.
57    #[command(flatten)]
58    network: Network,
59}
60
61/// Query parameters
62#[derive(Args)]
63struct QueryParameters {}
64
65/// Path parameters
66#[derive(Args)]
67struct PathParameters {}
68/// Network Body data
69#[derive(Args, Clone)]
70struct Network {
71    /// The administrative state of the network, which is up (`true`) or down
72    /// (`false`).
73    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
74    admin_state_up: Option<bool>,
75
76    /// The availability zone candidate for the network.
77    ///
78    /// Parameter is an array, may be provided multiple times.
79    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
80    availability_zone_hints: Option<Vec<String>>,
81
82    /// A human-readable description for the resource. Default is an empty
83    /// string.
84    #[arg(help_heading = "Body parameters", long)]
85    description: Option<String>,
86
87    /// A valid DNS domain.
88    #[arg(help_heading = "Body parameters", long)]
89    dns_domain: Option<String>,
90
91    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
92    ha: Option<bool>,
93
94    /// The network is default or not.
95    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
96    is_default: Option<bool>,
97
98    /// The maximum transmission unit (MTU) value to address fragmentation.
99    /// Minimum value is 68 for IPv4, and 1280 for IPv6.
100    #[arg(help_heading = "Body parameters", long)]
101    mtu: Option<u32>,
102
103    /// Human-readable name of the network.
104    #[arg(help_heading = "Body parameters", long)]
105    name: Option<String>,
106
107    /// The port security status of the network. Valid values are enabled
108    /// (`true`) and disabled (`false`). This value is used as the default
109    /// value of `port_security_enabled` field of a newly created port.
110    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
111    port_security_enabled: Option<bool>,
112
113    /// The type of physical network that this network should be mapped to. For
114    /// example, `flat`, `vlan`, `vxlan`, or `gre`. Valid values depend on a
115    /// networking back-end.
116    #[arg(help_heading = "Body parameters", long)]
117    provider_network_type: Option<String>,
118
119    /// The physical network where this network should be implemented. The
120    /// Networking API v2.0 does not provide a way to list available physical
121    /// networks. For example, the Open vSwitch plug-in configuration file
122    /// defines a symbolic name that maps to specific bridges on each compute
123    /// host.
124    #[arg(help_heading = "Body parameters", long)]
125    provider_physical_network: Option<String>,
126
127    /// The ID of the isolated segment on the physical network. The
128    /// `network_type` attribute defines the segmentation model. For example,
129    /// if the `network_type` value is vlan, this ID is a vlan identifier. If
130    /// the `network_type` value is gre, this ID is a gre key.
131    #[arg(help_heading = "Body parameters", long)]
132    provider_segmentation_id: Option<String>,
133
134    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
135    qinq: Option<bool>,
136
137    /// The ID of the QoS policy associated with the network.
138    #[arg(help_heading = "Body parameters", long)]
139    qos_policy_id: Option<String>,
140
141    /// Set explicit NULL for the qos_policy_id
142    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "qos_policy_id")]
143    no_qos_policy_id: bool,
144
145    /// Indicates whether the network has an external routing facility that’s
146    /// not managed by the networking service.
147    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
148    router_external: Option<bool>,
149
150    /// A list of provider `segment` objects.
151    ///
152    /// Parameter is an array, may be provided multiple times.
153    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
154    segments: Option<Vec<Value>>,
155
156    /// Indicates whether this resource is shared across all projects. By
157    /// default, only administrative users can change this value.
158    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
159    shared: Option<bool>,
160
161    /// The ID of the project that owns the resource. Only administrative and
162    /// users with advsvc role can specify a project ID other than their own.
163    /// You cannot change this value through authorization policies.
164    #[arg(help_heading = "Body parameters", long)]
165    tenant_id: Option<String>,
166
167    /// Indicates the VLAN transparency mode of the network, which is VLAN
168    /// transparent (`true`) or not VLAN transparent (`false`).
169    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
170    vlan_transparent: Option<bool>,
171}
172
173impl NetworkCommand {
174    /// Perform command action
175    pub async fn take_action<C: CliArgs>(
176        &self,
177        parsed_args: &C,
178        client: &mut AsyncOpenStack,
179    ) -> Result<(), OpenStackCliError> {
180        info!("Create Network");
181
182        let op = OutputProcessor::from_args(parsed_args, Some("network.network"), Some("create"));
183        op.validate_args(parsed_args)?;
184
185        let mut ep_builder = create::Request::builder();
186
187        // Set body parameters
188        // Set Request.network data
189        let args = &self.network;
190        let mut network_builder = create::NetworkBuilder::default();
191        if let Some(val) = &args.admin_state_up {
192            network_builder.admin_state_up(*val);
193        }
194
195        if let Some(val) = &args.availability_zone_hints {
196            network_builder.availability_zone_hints(val.iter().map(Into::into).collect::<Vec<_>>());
197        }
198
199        if let Some(val) = &args.description {
200            network_builder.description(val);
201        }
202
203        if let Some(val) = &args.dns_domain {
204            network_builder.dns_domain(val);
205        }
206
207        if let Some(val) = &args.ha {
208            network_builder.ha(*val);
209        }
210
211        if let Some(val) = &args.is_default {
212            network_builder.is_default(*val);
213        }
214
215        if let Some(val) = &args.mtu {
216            network_builder.mtu(*val);
217        }
218
219        if let Some(val) = &args.name {
220            network_builder.name(val);
221        }
222
223        if let Some(val) = &args.port_security_enabled {
224            network_builder.port_security_enabled(*val);
225        }
226
227        if let Some(val) = &args.provider_network_type {
228            network_builder.provider_network_type(val);
229        }
230
231        if let Some(val) = &args.provider_physical_network {
232            network_builder.provider_physical_network(val);
233        }
234
235        if let Some(val) = &args.provider_segmentation_id {
236            network_builder.provider_segmentation_id(val);
237        }
238
239        if let Some(val) = &args.qinq {
240            network_builder.qinq(*val);
241        }
242
243        if let Some(val) = &args.qos_policy_id {
244            network_builder.qos_policy_id(Some(val.into()));
245        } else if args.no_qos_policy_id {
246            network_builder.qos_policy_id(None);
247        }
248
249        if let Some(val) = &args.router_external {
250            network_builder.router_external(*val);
251        }
252
253        if let Some(val) = &args.segments {
254            let segments_builder: Vec<create::Segments> = val
255                .iter()
256                .flat_map(|v| serde_json::from_value::<create::Segments>(v.to_owned()))
257                .collect::<Vec<create::Segments>>();
258            network_builder.segments(segments_builder);
259        }
260
261        if let Some(val) = &args.shared {
262            network_builder.shared(*val);
263        }
264
265        if let Some(val) = &args.tenant_id {
266            network_builder.tenant_id(val);
267        }
268
269        if let Some(val) = &args.vlan_transparent {
270            network_builder.vlan_transparent(*val);
271        }
272
273        ep_builder.network(
274            network_builder
275                .build()
276                .wrap_err("error preparing the request data")?,
277        );
278
279        let ep = ep_builder
280            .build()
281            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
282
283        let data: serde_json::Value = ep.query_async(client).await?;
284
285        op.output_single::<response::create::NetworkResponse>(data.clone())?;
286        // Show command specific hints
287        op.show_command_hint()?;
288        Ok(())
289    }
290}