Skip to main content

openstack_cli_network/v2/agent/dhcp_network/
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 DhcpNetwork command
19//!
20//! Wraps invoking of the `v2.0/agents/{agent_id}/dhcp-networks` with `POST` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_cli_core::cli::CliArgs;
26use openstack_cli_core::error::OpenStackCliError;
27use openstack_cli_core::output::OutputProcessor;
28use openstack_sdk::AsyncOpenStack;
29
30use openstack_cli_core::common::parse_key_val;
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::network::v2::agent::dhcp_network::create;
33use openstack_types::network::v2::agent::dhcp_network::response;
34use serde_json::Value;
35
36/// Add a network to a DHCP agent
37///
38/// Normal response codes: 201
39///
40/// Error response codes: 400, 403, 409, 404
41#[derive(Args)]
42#[command(about = "Schedule a network to a DHCP agent")]
43pub struct DhcpNetworkCommand {
44    /// Request Query parameters
45    #[command(flatten)]
46    query: QueryParameters,
47
48    /// Path parameters
49    #[command(flatten)]
50    path: PathParameters,
51
52    #[arg(long="property", value_name="key=value", value_parser=parse_key_val::<String, Value>)]
53    #[arg(help_heading = "Body parameters")]
54    properties: Option<Vec<(String, Value)>>,
55}
56
57/// Query parameters
58#[derive(Args)]
59struct QueryParameters {}
60
61/// Path parameters
62#[derive(Args)]
63struct PathParameters {
64    /// agent_id parameter for /v2.0/agents/{agent_id}/dhcp-networks/{id} API
65    #[arg(
66        help_heading = "Path parameters",
67        id = "path_param_agent_id",
68        value_name = "AGENT_ID"
69    )]
70    agent_id: String,
71}
72
73impl DhcpNetworkCommand {
74    /// Perform command action
75    pub async fn take_action<C: CliArgs>(
76        &self,
77        parsed_args: &C,
78        client: &mut AsyncOpenStack,
79    ) -> Result<(), OpenStackCliError> {
80        info!("Create DhcpNetwork");
81
82        let op = OutputProcessor::from_args(
83            parsed_args,
84            Some("network.agent/dhcp_network"),
85            Some("create"),
86        );
87        op.validate_args(parsed_args)?;
88
89        let mut ep_builder = create::Request::builder();
90
91        ep_builder.agent_id(&self.path.agent_id);
92
93        // Set body parameters
94        if let Some(properties) = &self.properties {
95            ep_builder.properties(properties.iter().cloned());
96        }
97
98        let ep = ep_builder
99            .build()
100            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
101
102        let data: serde_json::Value = ep.query_async(client).await?;
103
104        op.output_single::<response::create::DhcpNetworkResponse>(data.clone())?;
105        // Show command specific hints
106        op.show_command_hint()?;
107        Ok(())
108    }
109}