Skip to main content

openstack_cli_network/
lib.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//! Networking commands
16use clap::error::{Error, ErrorKind};
17use clap::{Arg, ArgAction, ArgMatches, Args, Command, FromArgMatches};
18
19use openstack_cli_core::{cli::CliArgs, error::OpenStackCliError};
20use openstack_sdk::AsyncOpenStack;
21
22pub mod v2;
23
24const API_VERSION_ARG_ID: &str = "os_network_api_version";
25const API_VERSION_LONG: &str = "os-network-api-version";
26const API_VERSION_ENV: &str = "OS_NETWORK_API_VERSION";
27const DEFAULT_NETWORK_API_VERSION: &str = "2";
28
29/// Network (Neutron) commands
30pub enum NetworkCommand {
31    V2(v2::NetworkCommand),
32}
33
34impl Args for NetworkCommand {
35    fn augment_args(cmd: Command) -> Command {
36        // Only one version currently exists, so there's nothing to branch
37        // on here; `from_arg_matches` below still validates the flag's
38        // actual value and reports a proper "unsupported version" error
39        // for anything else.
40        let cmd = cmd.arg(
41            Arg::new(API_VERSION_ARG_ID)
42                .long(API_VERSION_LONG)
43                .env(API_VERSION_ENV)
44                .global(true)
45                .action(ArgAction::Set)
46                .default_value(DEFAULT_NETWORK_API_VERSION)
47                .help("Network API version to use (default: 2)"),
48        );
49        v2::NetworkCommand::augment_args(cmd)
50    }
51
52    fn augment_args_for_update(cmd: Command) -> Command {
53        Self::augment_args(cmd)
54    }
55}
56
57impl FromArgMatches for NetworkCommand {
58    fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
59        let version = matches
60            .get_one::<String>(API_VERSION_ARG_ID)
61            .map(String::as_str)
62            .unwrap_or(DEFAULT_NETWORK_API_VERSION);
63        match version {
64            "2" => Ok(Self::V2(v2::NetworkCommand::from_arg_matches(matches)?)),
65            other => Err(Error::raw(
66                ErrorKind::InvalidValue,
67                format!("unsupported Network API version: {other}. Supported: 2\n"),
68            )),
69        }
70    }
71
72    fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
73        *self = Self::from_arg_matches(matches)?;
74        Ok(())
75    }
76}
77
78impl NetworkCommand {
79    /// Perform command action
80    pub async fn take_action<C: CliArgs>(
81        &self,
82        parsed_args: &C,
83        client: &mut AsyncOpenStack,
84    ) -> Result<(), OpenStackCliError> {
85        match self {
86            Self::V2(cmd) => cmd.take_action(parsed_args, client).await,
87        }
88    }
89}