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

//! Prune objects in a container.
use clap::Args;
use eyre::eyre;
use tracing::info;

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

use openstack_sdk::{
    AsyncOpenStack,
    api::RestClient,
    types::{ApiVersion, ServiceType},
};

use openstack_sdk::api::object_store::v1::container::prune::PruneAsyncExt;

/// Prune objects in a container.
#[derive(Args, Clone, Debug)]
pub struct ContainerCommand {
    /// The unique (within an account) name for the container. The container
    /// name must be from 1 to 256 characters long and can start with any
    /// character and contain any pattern. Character set must be UTF-8. The
    /// container name cannot contain a slash (/) character because this
    /// character delimits the container and object name.
    #[arg()]
    container: String,

    /// Only objects with this prefix will be deleted.
    #[arg(long)]
    prefix: Option<String>,
}

impl ContainerCommand {
    /// Perform command action
    pub async fn take_action(
        &self,
        parsed_args: &Cli,
        client: &mut AsyncOpenStack,
    ) -> Result<(), OpenStackCliError> {
        info!("Prune Container with {:?}", self);

        let op =
            OutputProcessor::from_args(parsed_args, Some("object-store.container"), Some("prune"));
        op.validate_args(parsed_args)?;

        let ep = client.get_service_endpoint(
            &ServiceType::ObjectStore,
            Some(ApiVersion::new(1, 0)).as_ref(),
        )?;
        let account = ep
            .url()
            .path_segments()
            .ok_or_else(|| eyre!("Object Store endpoint must not point to a bare domain"))?
            .rfind(|x| !x.is_empty())
            .ok_or_else(|| eyre!("Object Store endpoint must end with project id"))?;
        client
            .object_store_container_prune_async(account, &self.container, self.prefix.as_ref())
            .await?;
        op.show_command_hint()?;
        Ok(())
    }
}