openstack-cli-network 0.2.0

OpenStack CLI Network commands
Documentation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
//
// WARNING: This file is automatically generated from OpenAPI schema using
// `openstack-codegenerator`.

//! Set Network command
//!
//! Wraps invoking of the `v2.0/networks/{network_id}` with `PUT` method

use clap::Args;
use eyre::{OptionExt, WrapErr};
use tracing::info;

use openstack_cli_core::cli::CliArgs;
use openstack_cli_core::error::OpenStackCliError;
use openstack_cli_core::output::OutputProcessor;
use openstack_sdk::AsyncOpenStack;

use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::find;
use openstack_sdk::api::network::v2::network::find;
use openstack_sdk::api::network::v2::network::set;
use openstack_types::network::v2::network::response;
use serde_json::Value;

/// Updates a network.
///
/// Normal response codes: 200
///
/// Error response codes: 400, 401, 403, 404, 412
#[derive(Args)]
#[command(about = "Update network")]
pub struct NetworkCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

    /// Path parameters
    #[command(flatten)]
    path: PathParameters,

    /// A `network` object.
    #[command(flatten)]
    network: Network,
}

/// Query parameters
#[derive(Args)]
struct QueryParameters {}

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// network_id parameter for /v2.0/networks/{network_id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_id",
        value_name = "ID"
    )]
    id: String,
}
/// Network Body data
#[derive(Args, Clone)]
struct Network {
    /// The administrative state of the network, which is up (`true`) or down
    /// (`false`).
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    admin_state_up: Option<bool>,

    /// A human-readable description for the resource. Default is an empty
    /// string.
    #[arg(help_heading = "Body parameters", long)]
    description: Option<String>,

    /// A valid DNS domain.
    #[arg(help_heading = "Body parameters", long)]
    dns_domain: Option<String>,

    /// The network is default or not.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    is_default: Option<bool>,

    /// The maximum transmission unit (MTU) value to address fragmentation.
    /// Minimum value is 68 for IPv4, and 1280 for IPv6.
    #[arg(help_heading = "Body parameters", long)]
    mtu: Option<u32>,

    /// Human-readable name of the network.
    #[arg(help_heading = "Body parameters", long)]
    name: Option<String>,

    /// The port security status of the network. Valid values are enabled
    /// (`true`) and disabled (`false`). This value is used as the default
    /// value of `port_security_enabled` field of a newly created port.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    port_security_enabled: Option<bool>,

    /// The type of physical network that this network is mapped to. For
    /// example, `flat`, `vlan`, `vxlan`, or `gre`. Valid values depend on a
    /// networking back-end.
    #[arg(help_heading = "Body parameters", long)]
    provider_network_type: Option<String>,

    /// The physical network where this network/segment is implemented.
    #[arg(help_heading = "Body parameters", long)]
    provider_physical_network: Option<String>,

    /// The ID of the isolated segment on the physical network. The
    /// `network_type` attribute defines the segmentation model. For example,
    /// if the `network_type` value is vlan, this ID is a vlan identifier. If
    /// the `network_type` value is gre, this ID is a gre key. `Note` that only
    /// the segmentation-id of VLAN type networks can be changed!
    #[arg(help_heading = "Body parameters", long)]
    provider_segmentation_id: Option<String>,

    /// The ID of the QoS policy associated with the network.
    #[arg(help_heading = "Body parameters", long)]
    qos_policy_id: Option<String>,

    /// Set explicit NULL for the qos_policy_id
    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "qos_policy_id")]
    no_qos_policy_id: bool,

    /// Indicates whether the network has an external routing facility that’s
    /// not managed by the networking service.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    router_external: Option<bool>,

    /// A list of provider `segment` objects.
    ///
    /// Parameter is an array, may be provided multiple times.
    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
    segments: Option<Vec<Value>>,

    /// Indicates whether this resource is shared across all projects. By
    /// default, only administrative users can change this value.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    shared: Option<bool>,
}

impl NetworkCommand {
    /// Perform command action
    pub async fn take_action<C: CliArgs>(
        &self,
        parsed_args: &C,
        client: &mut AsyncOpenStack,
    ) -> Result<(), OpenStackCliError> {
        info!("Set Network");

        let op = OutputProcessor::from_args(parsed_args, Some("network.network"), Some("set"));
        op.validate_args(parsed_args)?;

        let mut find_builder = find::Request::builder();

        find_builder.id(&self.path.id);

        let find_ep = find_builder
            .build()
            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;

        let mut ep_builder = set::Request::builder();

        let resource_id = find_data["id"]
            .as_str()
            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
            .to_string();
        ep_builder.id(resource_id.clone());

        // Set body parameters
        // Set Request.network data
        let args = &self.network;
        let mut network_builder = set::NetworkBuilder::default();
        if let Some(val) = &args.admin_state_up {
            network_builder.admin_state_up(*val);
        }

        if let Some(val) = &args.description {
            network_builder.description(val);
        }

        if let Some(val) = &args.dns_domain {
            network_builder.dns_domain(val);
        }

        if let Some(val) = &args.is_default {
            network_builder.is_default(*val);
        }

        if let Some(val) = &args.mtu {
            network_builder.mtu(*val);
        }

        if let Some(val) = &args.name {
            network_builder.name(val);
        }

        if let Some(val) = &args.port_security_enabled {
            network_builder.port_security_enabled(*val);
        }

        if let Some(val) = &args.provider_network_type {
            network_builder.provider_network_type(val);
        }

        if let Some(val) = &args.provider_physical_network {
            network_builder.provider_physical_network(val);
        }

        if let Some(val) = &args.provider_segmentation_id {
            network_builder.provider_segmentation_id(val);
        }

        if let Some(val) = &args.qos_policy_id {
            network_builder.qos_policy_id(Some(val.into()));
        } else if args.no_qos_policy_id {
            network_builder.qos_policy_id(None);
        }

        if let Some(val) = &args.router_external {
            network_builder.router_external(*val);
        }

        if let Some(val) = &args.segments {
            let segments_builder: Vec<set::Segments> = val
                .iter()
                .flat_map(|v| serde_json::from_value::<set::Segments>(v.to_owned()))
                .collect::<Vec<set::Segments>>();
            network_builder.segments(segments_builder);
        }

        if let Some(val) = &args.shared {
            network_builder.shared(*val);
        }

        ep_builder.network(
            network_builder
                .build()
                .wrap_err("error preparing the request data")?,
        );

        let ep = ep_builder
            .build()
            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;

        let data: serde_json::Value = ep.query_async(client).await?;

        op.output_single::<response::set::NetworkResponse>(data.clone())?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}