openstack_cli 0.13.5

OpenStack client rewritten in Rust
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`.

//! Create Zone command
//!
//! Wraps invoking of the `v2/zones` with `POST` method

use clap::Args;
use tracing::info;

use openstack_sdk::AsyncOpenStack;

use crate::Cli;
use crate::OpenStackCliError;
use crate::output::OutputProcessor;

use crate::common::parse_key_val;
use clap::ValueEnum;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::dns::v2::zone::create;
use openstack_types::dns::v2::zone::response::create::ZoneResponse;

/// Create a zone
#[derive(Args)]
#[command(about = "Create Zone")]
pub struct ZoneCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// Key:Value pairs of information about this zone, and the pool the user
    /// would like to place the zone in. This information can be used by the
    /// scheduler to place zones on the correct pool.
    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
    attributes: Option<Vec<(String, String)>>,

    /// Description for this zone
    #[arg(help_heading = "Body parameters", long)]
    description: Option<String>,

    /// e-mail for the zone. Used in SOA records for the zone
    #[arg(help_heading = "Body parameters", long)]
    email: Option<String>,

    /// Mandatory for secondary zones. The servers to slave from to get DNS
    /// information
    ///
    /// Parameter is an array, may be provided multiple times.
    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
    masters: Option<Vec<String>>,

    /// DNS Name for the zone
    #[arg(help_heading = "Body parameters", long)]
    name: Option<String>,

    /// TTL (Time to Live) for the zone.
    #[arg(help_heading = "Body parameters", long)]
    ttl: Option<i32>,

    /// Type of zone. PRIMARY is controlled by Designate, SECONDARY zones are
    /// slaved from another DNS Server. Defaults to PRIMARY
    #[arg(help_heading = "Body parameters", long)]
    _type: Option<Type>,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Type {
    Catalog,
    Primary,
    Secondary,
}

impl ZoneCommand {
    /// Perform command action
    pub async fn take_action(
        &self,
        parsed_args: &Cli,
        client: &mut AsyncOpenStack,
    ) -> Result<(), OpenStackCliError> {
        info!("Create Zone");

        let op = OutputProcessor::from_args(parsed_args, Some("dns.zone"), Some("create"));
        op.validate_args(parsed_args)?;

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

        // Set body parameters
        // Set Request.attributes data
        if let Some(arg) = &self.attributes {
            ep_builder.attributes(arg.iter().cloned());
        }

        // Set Request.description data
        if let Some(arg) = &self.description {
            ep_builder.description(arg);
        }

        // Set Request.email data
        if let Some(arg) = &self.email {
            ep_builder.email(arg);
        }

        // Set Request.masters data
        if let Some(arg) = &self.masters {
            ep_builder.masters(arg.iter().map(Into::into).collect::<Vec<_>>());
        }

        // Set Request.name data
        if let Some(arg) = &self.name {
            ep_builder.name(arg);
        }

        // Set Request.ttl data
        if let Some(arg) = &self.ttl {
            ep_builder.ttl(*arg);
        }

        // Set Request._type data
        if let Some(arg) = &self._type {
            let tmp = match arg {
                Type::Catalog => create::Type::Catalog,
                Type::Primary => create::Type::Primary,
                Type::Secondary => create::Type::Secondary,
            };
            ep_builder._type(tmp);
        }

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

        let data = ep.query_async(client).await?;
        op.output_single::<ZoneResponse>(data)?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}