Skip to main content

openstack_sdk_object_store/v1/container/
prune.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//! Prunes objects from the container.
16#[cfg(feature = "async")]
17use async_trait::async_trait;
18use futures::stream::{StreamExt, TryStreamExt};
19use serde::Deserialize;
20use tracing::debug;
21
22use openstack_sdk_core::{OpenStackError, RestError};
23
24#[cfg(feature = "async")]
25use openstack_sdk_core::api::{AsyncClient, QueryAsync};
26// #[cfg(feature = "sync")]
27// use crate::api::Client;
28
29use crate::v1::{container::get::Request as ListRequest, object::delete::Request as DeleteRequest};
30use openstack_sdk_core::api::{Pagination, ignore, paged};
31
32/// Delete concurrency
33const DELETE_CONCURRENCY: usize = 4;
34
35#[derive(Deserialize, Debug, Clone)]
36pub struct Object {
37    name: String,
38}
39
40#[cfg(feature = "async")]
41#[async_trait]
42pub trait PruneAsyncExt {
43    async fn object_store_container_prune_async<
44        S1: AsRef<str> + Send + Sync,
45        S2: AsRef<str> + Send + Sync,
46        S3: AsRef<str> + Send + Sync,
47    >(
48        &self,
49        account: S1,
50        container: S2,
51        prefix: Option<S3>,
52    ) -> Result<(), OpenStackError>;
53}
54
55#[cfg(feature = "async")]
56#[async_trait]
57impl<C> PruneAsyncExt for C
58where
59    C: AsyncClient<Error = RestError> + Sync,
60{
61    async fn object_store_container_prune_async<
62        S1: AsRef<str> + Send + Sync,
63        S2: AsRef<str> + Send + Sync,
64        S3: AsRef<str> + Send + Sync,
65    >(
66        &self,
67        account: S1,
68        container: S2,
69        prefix: Option<S3>,
70    ) -> Result<(), OpenStackError> {
71        let mut list_builder = ListRequest::builder();
72        // Set path parameters
73        list_builder.account(account.as_ref());
74        list_builder.container(container.as_ref());
75        // Set query filter parameters
76        if let Some(pref) = prefix {
77            list_builder.prefix(pref.as_ref().to_owned());
78        }
79        let list_ep = list_builder
80            .build()
81            .map_err(|x| OpenStackError::EndpointBuild(x.to_string()))?;
82
83        paged(list_ep, Pagination::All)
84            .iter_async::<C, Object>(self)
85            .map(Ok)
86            .try_for_each_concurrent(DELETE_CONCURRENCY, |item| async {
87                if let Ok(object) = item {
88                    let object_name = object.name.clone();
89                    debug!("Deleting object {:?}", object_name);
90                    let mut delete_builder = DeleteRequest::builder();
91                    delete_builder.account(account.as_ref());
92                    delete_builder.container(container.as_ref());
93                    delete_builder.object(object.name);
94                    let delete_ep = delete_builder
95                        .build()
96                        .map_err(|x| OpenStackError::EndpointBuild(x.to_string()))?;
97                    ignore(delete_ep).query_async(self).await?;
98                }
99                Ok::<(), OpenStackError>(())
100            })
101            .await?;
102        Ok(())
103    }
104}
105
106#[cfg(test)]
107mod tests {
108
109    #[cfg(feature = "async")]
110    #[tokio::test]
111    async fn test_prune() {}
112}