Skip to main content

openstack_cli_network/v2/router/
add_external_gateways.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//! Action Router command
19//!
20//! Wraps invoking of the `v2.0/routers/{id}/add_external_gateways` with `PUT` 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::router::add_external_gateways;
33use openstack_types::network::v2::router::response;
34use serde_json::Value;
35
36/// Request body
37#[derive(Args)]
38#[command(about = "Add external gateways to router")]
39pub struct RouterCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47
48    #[command(flatten)]
49    router: Router,
50}
51
52/// Query parameters
53#[derive(Args)]
54struct QueryParameters {}
55
56/// Path parameters
57#[derive(Args)]
58struct PathParameters {
59    /// id parameter for /v2.0/routers/{id} API
60    #[arg(
61        help_heading = "Path parameters",
62        id = "path_param_id",
63        value_name = "ID"
64    )]
65    id: String,
66}
67/// Router Body data
68#[derive(Args, Clone)]
69struct Router {
70    /// The list of external gateways of the router.
71    ///
72    /// Parameter is an array, may be provided multiple times.
73    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
74    external_gateways: Option<Vec<Value>>,
75}
76
77impl RouterCommand {
78    /// Perform command action
79    pub async fn take_action<C: CliArgs>(
80        &self,
81        parsed_args: &C,
82        client: &mut AsyncOpenStack,
83    ) -> Result<(), OpenStackCliError> {
84        info!("Action Router");
85
86        let op = OutputProcessor::from_args(
87            parsed_args,
88            Some("network.router"),
89            Some("add_external_gateways"),
90        );
91        op.validate_args(parsed_args)?;
92
93        let mut ep_builder = add_external_gateways::Request::builder();
94
95        ep_builder.id(&self.path.id);
96
97        // Set body parameters
98        // Set Request.router data
99        let args = &self.router;
100        let mut router_builder = add_external_gateways::RouterBuilder::default();
101        if let Some(val) = &args.external_gateways {
102            let external_gateways_builder: Vec<add_external_gateways::ExternalGateways> = val
103                .iter()
104                .flat_map(|v| {
105                    serde_json::from_value::<add_external_gateways::ExternalGateways>(v.to_owned())
106                })
107                .collect::<Vec<add_external_gateways::ExternalGateways>>();
108            router_builder.external_gateways(external_gateways_builder);
109        }
110
111        ep_builder.router(
112            router_builder
113                .build()
114                .wrap_err("error preparing the request data")?,
115        );
116
117        let ep = ep_builder
118            .build()
119            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
120
121        let data: serde_json::Value = ep.query_async(client).await?;
122
123        op.output_single::<response::add_external_gateways::RouterResponse>(data.clone())?;
124        // Show command specific hints
125        op.show_command_hint()?;
126        Ok(())
127    }
128}