Skip to main content

openstack_cli_network/v2/vpn/ipsecpolicy/
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 Ipsecpolicy command
19//!
20//! Wraps invoking of the `v2.0/vpn/ipsecpolicies` 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::ipsecpolicy::create;
34use openstack_types::network::v2::vpn::ipsecpolicy::response;
35
36/// Creates an IP security (IPsec) policy.
37///
38/// The IPsec policy specifies the authentication and encryption algorithms and
39/// encapsulation mode to use for the established VPN connection.
40///
41/// Normal response codes: 201
42///
43/// Error response codes: 400, 401
44#[derive(Args)]
45#[command(about = "Create IPsec policy")]
46pub struct IpsecpolicyCommand {
47    /// Request Query parameters
48    #[command(flatten)]
49    query: QueryParameters,
50
51    /// Path parameters
52    #[command(flatten)]
53    path: PathParameters,
54
55    /// An `ipsecpolicy` object.
56    #[command(flatten)]
57    ipsecpolicy: Ipsecpolicy,
58}
59
60/// Query parameters
61#[derive(Args)]
62struct QueryParameters {}
63
64/// Path parameters
65#[derive(Args)]
66struct PathParameters {}
67
68#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
69enum AuthAlgorithm {
70    AesCmac,
71    AesXcbc,
72    Sha1,
73    Sha256,
74    Sha384,
75    Sha512,
76}
77
78#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
79enum EncapsulationMode {
80    Transport,
81    Tunnel,
82}
83
84#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
85enum EncryptionAlgorithm {
86    _3des,
87    Aes128,
88    Aes128Ccm12,
89    Aes128Ccm16,
90    Aes128Ccm8,
91    Aes128Ctr,
92    Aes128Gcm12,
93    Aes128Gcm16,
94    Aes128Gcm8,
95    Aes192,
96    Aes192Ccm12,
97    Aes192Ccm16,
98    Aes192Ccm8,
99    Aes192Ctr,
100    Aes192Gcm12,
101    Aes192Gcm16,
102    Aes192Gcm8,
103    Aes256,
104    Aes256Ccm12,
105    Aes256Ccm16,
106    Aes256Ccm8,
107    Aes256Ctr,
108    Aes256Gcm12,
109    Aes256Gcm16,
110    Aes256Gcm8,
111}
112
113#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
114enum Pfs {
115    Group14,
116    Group15,
117    Group16,
118    Group17,
119    Group18,
120    Group19,
121    Group2,
122    Group20,
123    Group21,
124    Group22,
125    Group23,
126    Group24,
127    Group25,
128    Group26,
129    Group27,
130    Group28,
131    Group29,
132    Group30,
133    Group31,
134    Group5,
135}
136
137#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
138enum TransformProtocol {
139    Ah,
140    AhEsp,
141    Esp,
142}
143
144/// Ipsecpolicy Body data
145#[derive(Args, Clone)]
146struct Ipsecpolicy {
147    /// The authentication hash algorithm. Valid values are `sha1`, `sha256`,
148    /// `sha384`, `sha512`, `aes-xcbc`, `aes-cmac`. The default is `sha1`.
149    #[arg(help_heading = "Body parameters", long)]
150    auth_algorithm: Option<AuthAlgorithm>,
151
152    /// A human-readable description for the resource. Default is an empty
153    /// string.
154    #[arg(help_heading = "Body parameters", long)]
155    description: Option<String>,
156
157    /// The encapsulation mode. A valid value is `tunnel` or `transport`.
158    /// Default is `tunnel`.
159    #[arg(help_heading = "Body parameters", long)]
160    encapsulation_mode: Option<EncapsulationMode>,
161
162    /// The encryption algorithm. A valid value is `3des`, `aes-128`,
163    /// `aes-192`, `aes-256`, `aes-128-ctr`, `aes-192-ctr`, `aes-256-ctr`.
164    /// Additional values for AES CCM and GCM modes are defined (e.g.
165    /// `aes-256-ccm-16`, `aes-256-gcm-16`) for all combinations of key length
166    /// 128, 192, 256 bits and ICV length 8, 12, 16 octets. Default is
167    /// `aes-128`.
168    #[arg(help_heading = "Body parameters", long)]
169    encryption_algorithm: Option<EncryptionAlgorithm>,
170
171    /// The lifetime of the security association. The lifetime consists of a
172    /// unit and integer value. You can omit either the unit or value portion
173    /// of the lifetime. Default unit is seconds and default value is 3600.
174    #[arg(help_heading = "Body parameters", long)]
175    lifetime: Option<String>,
176
177    /// Human-readable name of the resource. Default is an empty string.
178    #[arg(help_heading = "Body parameters", long)]
179    name: Option<String>,
180
181    /// Perfect forward secrecy (PFS). A valid value is `Group2`, `Group5`,
182    /// `Group14` to `Group31`. Default is `Group5`.
183    #[arg(help_heading = "Body parameters", long)]
184    pfs: Option<Pfs>,
185
186    /// The ID of the project.
187    #[arg(help_heading = "Body parameters", long)]
188    tenant_id: Option<String>,
189
190    /// The transform protocol. A valid value is `ESP`, `AH`, or `AH- ESP`.
191    /// Default is `ESP`.
192    #[arg(help_heading = "Body parameters", long)]
193    transform_protocol: Option<TransformProtocol>,
194}
195
196impl IpsecpolicyCommand {
197    /// Perform command action
198    pub async fn take_action<C: CliArgs>(
199        &self,
200        parsed_args: &C,
201        client: &mut AsyncOpenStack,
202    ) -> Result<(), OpenStackCliError> {
203        info!("Create Ipsecpolicy");
204
205        let op = OutputProcessor::from_args(
206            parsed_args,
207            Some("network.vpn/ipsecpolicy"),
208            Some("create"),
209        );
210        op.validate_args(parsed_args)?;
211
212        let mut ep_builder = create::Request::builder();
213
214        // Set body parameters
215        // Set Request.ipsecpolicy data
216        let args = &self.ipsecpolicy;
217        let mut ipsecpolicy_builder = create::IpsecpolicyBuilder::default();
218        if let Some(val) = &args.auth_algorithm {
219            let tmp = match val {
220                AuthAlgorithm::AesCmac => create::AuthAlgorithm::AesCmac,
221                AuthAlgorithm::AesXcbc => create::AuthAlgorithm::AesXcbc,
222                AuthAlgorithm::Sha1 => create::AuthAlgorithm::Sha1,
223                AuthAlgorithm::Sha256 => create::AuthAlgorithm::Sha256,
224                AuthAlgorithm::Sha384 => create::AuthAlgorithm::Sha384,
225                AuthAlgorithm::Sha512 => create::AuthAlgorithm::Sha512,
226            };
227            ipsecpolicy_builder.auth_algorithm(tmp);
228        }
229
230        if let Some(val) = &args.description {
231            ipsecpolicy_builder.description(val);
232        }
233
234        if let Some(val) = &args.encapsulation_mode {
235            let tmp = match val {
236                EncapsulationMode::Transport => create::EncapsulationMode::Transport,
237                EncapsulationMode::Tunnel => create::EncapsulationMode::Tunnel,
238            };
239            ipsecpolicy_builder.encapsulation_mode(tmp);
240        }
241
242        if let Some(val) = &args.encryption_algorithm {
243            let tmp = match val {
244                EncryptionAlgorithm::_3des => create::EncryptionAlgorithm::_3des,
245                EncryptionAlgorithm::Aes128 => create::EncryptionAlgorithm::Aes128,
246                EncryptionAlgorithm::Aes128Ccm12 => create::EncryptionAlgorithm::Aes128Ccm12,
247                EncryptionAlgorithm::Aes128Ccm16 => create::EncryptionAlgorithm::Aes128Ccm16,
248                EncryptionAlgorithm::Aes128Ccm8 => create::EncryptionAlgorithm::Aes128Ccm8,
249                EncryptionAlgorithm::Aes128Ctr => create::EncryptionAlgorithm::Aes128Ctr,
250                EncryptionAlgorithm::Aes128Gcm12 => create::EncryptionAlgorithm::Aes128Gcm12,
251                EncryptionAlgorithm::Aes128Gcm16 => create::EncryptionAlgorithm::Aes128Gcm16,
252                EncryptionAlgorithm::Aes128Gcm8 => create::EncryptionAlgorithm::Aes128Gcm8,
253                EncryptionAlgorithm::Aes192 => create::EncryptionAlgorithm::Aes192,
254                EncryptionAlgorithm::Aes192Ccm12 => create::EncryptionAlgorithm::Aes192Ccm12,
255                EncryptionAlgorithm::Aes192Ccm16 => create::EncryptionAlgorithm::Aes192Ccm16,
256                EncryptionAlgorithm::Aes192Ccm8 => create::EncryptionAlgorithm::Aes192Ccm8,
257                EncryptionAlgorithm::Aes192Ctr => create::EncryptionAlgorithm::Aes192Ctr,
258                EncryptionAlgorithm::Aes192Gcm12 => create::EncryptionAlgorithm::Aes192Gcm12,
259                EncryptionAlgorithm::Aes192Gcm16 => create::EncryptionAlgorithm::Aes192Gcm16,
260                EncryptionAlgorithm::Aes192Gcm8 => create::EncryptionAlgorithm::Aes192Gcm8,
261                EncryptionAlgorithm::Aes256 => create::EncryptionAlgorithm::Aes256,
262                EncryptionAlgorithm::Aes256Ccm12 => create::EncryptionAlgorithm::Aes256Ccm12,
263                EncryptionAlgorithm::Aes256Ccm16 => create::EncryptionAlgorithm::Aes256Ccm16,
264                EncryptionAlgorithm::Aes256Ccm8 => create::EncryptionAlgorithm::Aes256Ccm8,
265                EncryptionAlgorithm::Aes256Ctr => create::EncryptionAlgorithm::Aes256Ctr,
266                EncryptionAlgorithm::Aes256Gcm12 => create::EncryptionAlgorithm::Aes256Gcm12,
267                EncryptionAlgorithm::Aes256Gcm16 => create::EncryptionAlgorithm::Aes256Gcm16,
268                EncryptionAlgorithm::Aes256Gcm8 => create::EncryptionAlgorithm::Aes256Gcm8,
269            };
270            ipsecpolicy_builder.encryption_algorithm(tmp);
271        }
272
273        if let Some(val) = &args.lifetime {
274            ipsecpolicy_builder.lifetime(val);
275        }
276
277        if let Some(val) = &args.name {
278            ipsecpolicy_builder.name(val);
279        }
280
281        if let Some(val) = &args.pfs {
282            let tmp = match val {
283                Pfs::Group14 => create::Pfs::Group14,
284                Pfs::Group15 => create::Pfs::Group15,
285                Pfs::Group16 => create::Pfs::Group16,
286                Pfs::Group17 => create::Pfs::Group17,
287                Pfs::Group18 => create::Pfs::Group18,
288                Pfs::Group19 => create::Pfs::Group19,
289                Pfs::Group2 => create::Pfs::Group2,
290                Pfs::Group20 => create::Pfs::Group20,
291                Pfs::Group21 => create::Pfs::Group21,
292                Pfs::Group22 => create::Pfs::Group22,
293                Pfs::Group23 => create::Pfs::Group23,
294                Pfs::Group24 => create::Pfs::Group24,
295                Pfs::Group25 => create::Pfs::Group25,
296                Pfs::Group26 => create::Pfs::Group26,
297                Pfs::Group27 => create::Pfs::Group27,
298                Pfs::Group28 => create::Pfs::Group28,
299                Pfs::Group29 => create::Pfs::Group29,
300                Pfs::Group30 => create::Pfs::Group30,
301                Pfs::Group31 => create::Pfs::Group31,
302                Pfs::Group5 => create::Pfs::Group5,
303            };
304            ipsecpolicy_builder.pfs(tmp);
305        }
306
307        if let Some(val) = &args.tenant_id {
308            ipsecpolicy_builder.tenant_id(val);
309        }
310
311        if let Some(val) = &args.transform_protocol {
312            let tmp = match val {
313                TransformProtocol::Ah => create::TransformProtocol::Ah,
314                TransformProtocol::AhEsp => create::TransformProtocol::AhEsp,
315                TransformProtocol::Esp => create::TransformProtocol::Esp,
316            };
317            ipsecpolicy_builder.transform_protocol(tmp);
318        }
319
320        ep_builder.ipsecpolicy(
321            ipsecpolicy_builder
322                .build()
323                .wrap_err("error preparing the request data")?,
324        );
325
326        let ep = ep_builder
327            .build()
328            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
329
330        let data: serde_json::Value = ep.query_async(client).await?;
331
332        op.output_single::<response::create::IpsecpolicyResponse>(data.clone())?;
333        // Show command specific hints
334        op.show_command_hint()?;
335        Ok(())
336    }
337}