Skip to main content

openstack_cli_network/v2/address_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 AddressGroup command
19//!
20//! Wraps invoking of the `v2.0/address-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::address_group::create;
33use openstack_types::network::v2::address_group::response;
34
35/// Creates an address group.
36///
37/// Normal response codes: 201
38///
39/// Error response codes: 400, 401, 403, 404
40#[derive(Args)]
41#[command(about = "Create address group")]
42pub struct AddressGroupCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50
51    /// An `address group` object.
52    #[command(flatten)]
53    address_group: AddressGroup,
54}
55
56/// Query parameters
57#[derive(Args)]
58struct QueryParameters {}
59
60/// Path parameters
61#[derive(Args)]
62struct PathParameters {}
63/// AddressGroup Body data
64#[derive(Args, Clone)]
65struct AddressGroup {
66    /// A list of IP addresses.
67    ///
68    /// Parameter is an array, may be provided multiple times.
69    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
70    addresses: Option<Vec<String>>,
71
72    /// A human-readable description for the resource.
73    #[arg(help_heading = "Body parameters", long)]
74    description: Option<String>,
75
76    /// Human-readable name of the resource. Default is an empty string.
77    #[arg(help_heading = "Body parameters", long)]
78    name: Option<String>,
79
80    #[arg(help_heading = "Body parameters", long)]
81    project_id: Option<String>,
82}
83
84impl AddressGroupCommand {
85    /// Perform command action
86    pub async fn take_action<C: CliArgs>(
87        &self,
88        parsed_args: &C,
89        client: &mut AsyncOpenStack,
90    ) -> Result<(), OpenStackCliError> {
91        info!("Create AddressGroup");
92
93        let op =
94            OutputProcessor::from_args(parsed_args, Some("network.address_group"), Some("create"));
95        op.validate_args(parsed_args)?;
96
97        let mut ep_builder = create::Request::builder();
98
99        // Set body parameters
100        // Set Request.address_group data
101        let args = &self.address_group;
102        let mut address_group_builder = create::AddressGroupBuilder::default();
103        if let Some(val) = &args.addresses {
104            address_group_builder.addresses(val.iter().map(Into::into).collect::<Vec<_>>());
105        }
106
107        if let Some(val) = &args.description {
108            address_group_builder.description(val);
109        }
110
111        if let Some(val) = &args.name {
112            address_group_builder.name(val);
113        }
114
115        if let Some(val) = &args.project_id {
116            address_group_builder.project_id(val);
117        }
118
119        ep_builder.address_group(
120            address_group_builder
121                .build()
122                .wrap_err("error preparing the request data")?,
123        );
124
125        let ep = ep_builder
126            .build()
127            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
128
129        let data: serde_json::Value = ep.query_async(client).await?;
130
131        op.output_single::<response::create::AddressGroupResponse>(data.clone())?;
132        // Show command specific hints
133        op.show_command_hint()?;
134        Ok(())
135    }
136}