Skip to main content

openstack_cli_network/v2/router/
add_extraroutes.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_extraroutes` 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_extraroutes;
33use openstack_types::network::v2::router::response;
34use serde_json::Value;
35
36/// Request body
37#[derive(Args)]
38#[command(about = "Add extra routes 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 extra routes configuration for L3 router. A list of dictionaries
71    /// with `destination` and `nexthop` parameters. It is available when
72    /// `extraroute` extension is enabled.
73    ///
74    /// Parameter is an array, may be provided multiple times.
75    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
76    routes: Option<Vec<Value>>,
77}
78
79impl RouterCommand {
80    /// Perform command action
81    pub async fn take_action<C: CliArgs>(
82        &self,
83        parsed_args: &C,
84        client: &mut AsyncOpenStack,
85    ) -> Result<(), OpenStackCliError> {
86        info!("Action Router");
87
88        let op = OutputProcessor::from_args(
89            parsed_args,
90            Some("network.router"),
91            Some("add_extraroutes"),
92        );
93        op.validate_args(parsed_args)?;
94
95        let mut ep_builder = add_extraroutes::Request::builder();
96
97        ep_builder.id(&self.path.id);
98
99        // Set body parameters
100        // Set Request.router data
101        let args = &self.router;
102        let mut router_builder = add_extraroutes::RouterBuilder::default();
103        if let Some(val) = &args.routes {
104            let routes_builder: Vec<add_extraroutes::Routes> = val
105                .iter()
106                .flat_map(|v| serde_json::from_value::<add_extraroutes::Routes>(v.to_owned()))
107                .collect::<Vec<add_extraroutes::Routes>>();
108            router_builder.routes(routes_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_extraroutes::RouterResponse>(data.clone())?;
124        // Show command specific hints
125        op.show_command_hint()?;
126        Ok(())
127    }
128}