Skip to main content

openstack_cli_network/v2/security_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 SecurityGroup command
19//!
20//! Wraps invoking of the `v2.0/security-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 openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::network::v2::security_group::create;
33use openstack_types::network::v2::security_group::response;
34
35/// Creates an OpenStack Networking security group.
36///
37/// This operation creates a security group with default security group rules
38/// for the IPv4 and IPv6 ether types.
39///
40/// Normal response codes: 201
41///
42/// Error response codes: 400, 401, 409
43#[derive(Args)]
44#[command(about = "Create security group")]
45pub struct SecurityGroupCommand {
46    /// Request Query parameters
47    #[command(flatten)]
48    query: QueryParameters,
49
50    /// Path parameters
51    #[command(flatten)]
52    path: PathParameters,
53
54    /// A `security_group` object.
55    #[command(flatten)]
56    security_group: SecurityGroup,
57}
58
59/// Query parameters
60#[derive(Args)]
61struct QueryParameters {}
62
63/// Path parameters
64#[derive(Args)]
65struct PathParameters {}
66/// SecurityGroup Body data
67#[derive(Args, Clone)]
68struct SecurityGroup {
69    /// A human-readable description for the resource. Default is an empty
70    /// string.
71    #[arg(help_heading = "Body parameters", long)]
72    description: Option<String>,
73
74    /// Human-readable name of the resource.
75    #[arg(help_heading = "Body parameters", long)]
76    name: Option<String>,
77
78    /// Indicates if the security group is stateful or stateless.
79    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
80    stateful: Option<bool>,
81
82    /// The ID of the project.
83    #[arg(help_heading = "Body parameters", long)]
84    tenant_id: Option<String>,
85}
86
87impl SecurityGroupCommand {
88    /// Perform command action
89    pub async fn take_action<C: CliArgs>(
90        &self,
91        parsed_args: &C,
92        client: &mut AsyncOpenStack,
93    ) -> Result<(), OpenStackCliError> {
94        info!("Create SecurityGroup");
95
96        let op =
97            OutputProcessor::from_args(parsed_args, Some("network.security_group"), Some("create"));
98        op.validate_args(parsed_args)?;
99
100        let mut ep_builder = create::Request::builder();
101
102        // Set body parameters
103        // Set Request.security_group data
104        let args = &self.security_group;
105        let mut security_group_builder = create::SecurityGroupBuilder::default();
106        if let Some(val) = &args.description {
107            security_group_builder.description(val);
108        }
109
110        if let Some(val) = &args.name {
111            security_group_builder.name(val);
112        }
113
114        if let Some(val) = &args.stateful {
115            security_group_builder.stateful(*val);
116        }
117
118        if let Some(val) = &args.tenant_id {
119            security_group_builder.tenant_id(val);
120        }
121
122        ep_builder.security_group(
123            security_group_builder
124                .build()
125                .wrap_err("error preparing the request data")?,
126        );
127
128        let ep = ep_builder
129            .build()
130            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
131
132        let data: serde_json::Value = ep.query_async(client).await?;
133
134        op.output_single::<response::create::SecurityGroupResponse>(data.clone())?;
135        // Show command specific hints
136        op.show_command_hint()?;
137        Ok(())
138    }
139}