use clap::Args;
use eyre::eyre;
use serde::{Deserialize, Serialize};
use tracing::info;
use crate::Cli;
use crate::OpenStackCliError;
use crate::output::OutputProcessor;
use structable::{StructTable, StructTableOptions};
use openstack_sdk::{
AsyncOpenStack,
api::RestClient,
types::{ApiVersion, ServiceType},
};
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::object_store::v1::container::get::Request;
use openstack_sdk::api::{Pagination, paged};
#[derive(Args, Clone, Debug)]
pub struct ObjectsCommand {
#[arg()]
container: String,
#[arg(long)]
limit: Option<u32>,
#[arg(long)]
marker: Option<String>,
#[arg(long)]
end_marker: Option<String>,
#[arg(long)]
format: Option<String>,
#[arg(long)]
prefix: Option<String>,
#[arg(long)]
delimiter: Option<String>,
#[arg(long, action=clap::ArgAction::Set)]
reverse: Option<bool>,
#[arg(long, default_value_t = 10000)]
max_items: usize,
}
#[derive(Deserialize, Debug, Clone, Serialize, StructTable)]
pub struct Object {
#[structable(optional)]
name: Option<String>,
#[structable(optional, wide)]
content_type: Option<String>,
#[structable(optional, wide)]
bytes: Option<u64>,
#[structable(optional, wide)]
hash: Option<String>,
#[structable(optional, wide)]
last_modified: Option<String>,
#[structable(optional, wide)]
symlink_path: Option<String>,
}
impl ObjectsCommand {
pub async fn take_action(
&self,
parsed_args: &Cli,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Get Objects with {:?}", self);
let op = OutputProcessor::from_args(parsed_args, Some("object-store.object"), Some("list"));
op.validate_args(parsed_args)?;
let mut ep_builder = Request::builder();
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());
if let Some(account) = account {
ep_builder.account(account);
}
ep_builder.container(&self.container);
if let Some(val) = &self.limit {
ep_builder.limit(*val);
}
if let Some(val) = &self.marker {
ep_builder.marker(val);
}
if let Some(val) = &self.end_marker {
ep_builder.end_marker(val);
}
if let Some(val) = &self.format {
ep_builder.format(val);
}
if let Some(val) = &self.prefix {
ep_builder.prefix(val);
}
if let Some(val) = &self.delimiter {
ep_builder.delimiter(val);
}
if let Some(val) = &self.reverse {
ep_builder.reverse(*val);
}
let ep = ep_builder
.build()
.map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
.query_async(client)
.await?;
op.output_list::<Object>(data)?;
op.show_command_hint()?;
Ok(())
}
}