Skip to main content

openstack_cli_network/v2/vpn/vpnservice/
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 Vpnservice command
19//!
20//! Wraps invoking of the `v2.0/vpn/vpnservices` 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::vpn::vpnservice::create;
33use openstack_types::network::v2::vpn::vpnservice::response;
34
35/// Creates a VPN service.
36///
37/// The service is associated with a router. After you create the service, it
38/// can contain multiple VPN connections.
39///
40/// An optional `flavor_id` attribute can be passed to enable dynamic selection
41/// of an appropriate provider if configured by the operator. It is only
42/// available when `vpn-flavors` extension is enabled. The basic selection
43/// algorithm chooses the provider in the first service profile currently
44/// associated with flavor. This option can only be set in `POST` operation.
45///
46/// Normal response codes: 201
47///
48/// Error response codes: 400, 401
49#[derive(Args)]
50#[command(about = "Create VPN service")]
51pub struct VpnserviceCommand {
52    /// Request Query parameters
53    #[command(flatten)]
54    query: QueryParameters,
55
56    /// Path parameters
57    #[command(flatten)]
58    path: PathParameters,
59
60    /// A `vpnservice` object.
61    #[command(flatten)]
62    vpnservice: Vpnservice,
63}
64
65/// Query parameters
66#[derive(Args)]
67struct QueryParameters {}
68
69/// Path parameters
70#[derive(Args)]
71struct PathParameters {}
72/// Vpnservice Body data
73#[derive(Args, Clone)]
74struct Vpnservice {
75    /// The administrative state of the resource, which is up (`true`) or down
76    /// (`false`).
77    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
78    admin_state_up: Option<bool>,
79
80    /// A human-readable description for the resource. Default is an empty
81    /// string.
82    #[arg(help_heading = "Body parameters", long)]
83    description: Option<String>,
84
85    /// The ID of the flavor.
86    #[arg(help_heading = "Body parameters", long)]
87    flavor_id: Option<String>,
88
89    /// Set explicit NULL for the flavor_id
90    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "flavor_id")]
91    no_flavor_id: bool,
92
93    /// Human-readable name of the resource. Default is an empty string.
94    #[arg(help_heading = "Body parameters", long)]
95    name: Option<String>,
96
97    #[arg(help_heading = "Body parameters", long)]
98    router_id: Option<String>,
99
100    /// If you specify only a subnet UUID, OpenStack Networking allocates an
101    /// available IP from that subnet to the port. If you specify both a subnet
102    /// UUID and an IP address, OpenStack Networking tries to allocate the
103    /// address to the port.
104    #[arg(help_heading = "Body parameters", long)]
105    subnet_id: Option<String>,
106
107    /// Set explicit NULL for the subnet_id
108    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "subnet_id")]
109    no_subnet_id: bool,
110
111    /// The ID of the project.
112    #[arg(help_heading = "Body parameters", long)]
113    tenant_id: Option<String>,
114}
115
116impl VpnserviceCommand {
117    /// Perform command action
118    pub async fn take_action<C: CliArgs>(
119        &self,
120        parsed_args: &C,
121        client: &mut AsyncOpenStack,
122    ) -> Result<(), OpenStackCliError> {
123        info!("Create Vpnservice");
124
125        let op =
126            OutputProcessor::from_args(parsed_args, Some("network.vpn/vpnservice"), Some("create"));
127        op.validate_args(parsed_args)?;
128
129        let mut ep_builder = create::Request::builder();
130
131        // Set body parameters
132        // Set Request.vpnservice data
133        let args = &self.vpnservice;
134        let mut vpnservice_builder = create::VpnserviceBuilder::default();
135        if let Some(val) = &args.admin_state_up {
136            vpnservice_builder.admin_state_up(*val);
137        }
138
139        if let Some(val) = &args.description {
140            vpnservice_builder.description(val);
141        }
142
143        if let Some(val) = &args.flavor_id {
144            vpnservice_builder.flavor_id(Some(val.into()));
145        } else if args.no_flavor_id {
146            vpnservice_builder.flavor_id(None);
147        }
148
149        if let Some(val) = &args.name {
150            vpnservice_builder.name(val);
151        }
152
153        if let Some(val) = &args.router_id {
154            vpnservice_builder.router_id(val);
155        }
156
157        if let Some(val) = &args.subnet_id {
158            vpnservice_builder.subnet_id(Some(val.into()));
159        } else if args.no_subnet_id {
160            vpnservice_builder.subnet_id(None);
161        }
162
163        if let Some(val) = &args.tenant_id {
164            vpnservice_builder.tenant_id(val);
165        }
166
167        ep_builder.vpnservice(
168            vpnservice_builder
169                .build()
170                .wrap_err("error preparing the request data")?,
171        );
172
173        let ep = ep_builder
174            .build()
175            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
176
177        let data: serde_json::Value = ep.query_async(client).await?;
178
179        op.output_single::<response::create::VpnserviceResponse>(data.clone())?;
180        // Show command specific hints
181        op.show_command_hint()?;
182        Ok(())
183    }
184}