Skip to main content

openstack_cli_network/v2/vpn/endpoint_group/
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 EndpointGroup command
19//!
20//! Wraps invoking of the `v2.0/vpn/endpoint-groups` 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 clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::network::v2::vpn::endpoint_group::create;
34use openstack_types::network::v2::vpn::endpoint_group::response;
35
36/// Creates a VPN endpoint group.
37///
38/// The endpoint group contains one or more endpoints of a specific type that
39/// you can use to create a VPN connections.
40///
41/// Normal response codes: 201
42///
43/// Error response codes: 400, 401
44#[derive(Args)]
45#[command(about = "Create VPN endpoint group")]
46pub struct EndpointGroupCommand {
47    /// Request Query parameters
48    #[command(flatten)]
49    query: QueryParameters,
50
51    /// Path parameters
52    #[command(flatten)]
53    path: PathParameters,
54
55    #[command(flatten)]
56    endpoint_group: EndpointGroup,
57}
58
59/// Query parameters
60#[derive(Args)]
61struct QueryParameters {}
62
63/// Path parameters
64#[derive(Args)]
65struct PathParameters {}
66
67#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
68enum Type {
69    Cidr,
70    Network,
71    Router,
72    Subnet,
73    Vlan,
74}
75
76/// EndpointGroup Body data
77#[derive(Args, Clone)]
78struct EndpointGroup {
79    /// A human-readable description for the resource. Default is an empty
80    /// string.
81    #[arg(help_heading = "Body parameters", long)]
82    description: Option<String>,
83
84    /// List of endpoints of the same type, for the endpoint group. The values
85    /// will depend on type.
86    ///
87    /// Parameter is an array, may be provided multiple times.
88    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
89    endpoints: Option<Vec<String>>,
90
91    /// Human-readable name of the resource. Default is an empty string.
92    #[arg(help_heading = "Body parameters", long)]
93    name: Option<String>,
94
95    /// The ID of the project.
96    #[arg(help_heading = "Body parameters", long)]
97    tenant_id: Option<String>,
98
99    /// The type of the endpoints in the group. A valid value is `subnet`,
100    /// `cidr`, `network`, `router`, or `vlan`. Only `subnet` and `cidr` are
101    /// supported at this moment.
102    #[arg(help_heading = "Body parameters", long)]
103    _type: Option<Type>,
104}
105
106impl EndpointGroupCommand {
107    /// Perform command action
108    pub async fn take_action<C: CliArgs>(
109        &self,
110        parsed_args: &C,
111        client: &mut AsyncOpenStack,
112    ) -> Result<(), OpenStackCliError> {
113        info!("Create EndpointGroup");
114
115        let op = OutputProcessor::from_args(
116            parsed_args,
117            Some("network.vpn/endpoint_group"),
118            Some("create"),
119        );
120        op.validate_args(parsed_args)?;
121
122        let mut ep_builder = create::Request::builder();
123
124        // Set body parameters
125        // Set Request.endpoint_group data
126        let args = &self.endpoint_group;
127        let mut endpoint_group_builder = create::EndpointGroupBuilder::default();
128        if let Some(val) = &args.description {
129            endpoint_group_builder.description(val);
130        }
131
132        if let Some(val) = &args.endpoints {
133            endpoint_group_builder.endpoints(val.iter().map(Into::into).collect::<Vec<_>>());
134        }
135
136        if let Some(val) = &args.name {
137            endpoint_group_builder.name(val);
138        }
139
140        if let Some(val) = &args.tenant_id {
141            endpoint_group_builder.tenant_id(val);
142        }
143
144        if let Some(val) = &args._type {
145            let tmp = match val {
146                Type::Cidr => create::Type::Cidr,
147                Type::Network => create::Type::Network,
148                Type::Router => create::Type::Router,
149                Type::Subnet => create::Type::Subnet,
150                Type::Vlan => create::Type::Vlan,
151            };
152            endpoint_group_builder._type(tmp);
153        }
154
155        ep_builder.endpoint_group(
156            endpoint_group_builder
157                .build()
158                .wrap_err("error preparing the request data")?,
159        );
160
161        let ep = ep_builder
162            .build()
163            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
164
165        let data: serde_json::Value = ep.query_async(client).await?;
166
167        op.output_single::<response::create::EndpointGroupResponse>(data.clone())?;
168        // Show command specific hints
169        op.show_command_hint()?;
170        Ok(())
171    }
172}