mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-21 11:56:38 +00:00
refactor(storage): extract ListBuckets response assembly (#1879)
This commit is contained in:
@@ -24,7 +24,7 @@ use crate::storage::helper::OperationHelper;
|
|||||||
use crate::storage::options::{filter_object_metadata, get_content_sha256};
|
use crate::storage::options::{filter_object_metadata, get_content_sha256};
|
||||||
use crate::storage::readers::InMemoryAsyncReader;
|
use crate::storage::readers::InMemoryAsyncReader;
|
||||||
use crate::storage::s3_api::bucket::{
|
use crate::storage::s3_api::bucket::{
|
||||||
build_list_object_versions_output, build_list_objects_output, build_list_objects_v2_output,
|
build_list_buckets_output, build_list_object_versions_output, build_list_objects_output, build_list_objects_v2_output,
|
||||||
parse_list_object_versions_params, parse_list_objects_v2_params,
|
parse_list_object_versions_params, parse_list_objects_v2_params,
|
||||||
};
|
};
|
||||||
use crate::storage::s3_api::common::rustfs_owner;
|
use crate::storage::s3_api::common::rustfs_owner;
|
||||||
@@ -3539,20 +3539,7 @@ impl S3 for FS {
|
|||||||
store.list_bucket(&BucketOptions::default()).await.map_err(ApiError::from)?
|
store.list_bucket(&BucketOptions::default()).await.map_err(ApiError::from)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let buckets: Vec<Bucket> = bucket_infos
|
let output = build_list_buckets_output(&bucket_infos);
|
||||||
.iter()
|
|
||||||
.map(|v| Bucket {
|
|
||||||
creation_date: v.created.map(Timestamp::from),
|
|
||||||
name: Some(v.name.clone()),
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let output = ListBucketsOutput {
|
|
||||||
buckets: Some(buckets),
|
|
||||||
owner: Some(RUSTFS_OWNER.to_owned()),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
Ok(s3_response(output))
|
Ok(s3_response(output))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,12 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
use crate::storage::s3_api::common::rustfs_owner;
|
||||||
use rustfs_ecstore::client::object_api_utils::to_s3s_etag;
|
use rustfs_ecstore::client::object_api_utils::to_s3s_etag;
|
||||||
use rustfs_ecstore::store_api::{ListObjectVersionsInfo, ListObjectsV2Info};
|
use rustfs_ecstore::store_api::{BucketInfo, ListObjectVersionsInfo, ListObjectsV2Info};
|
||||||
use s3s::dto::{
|
use s3s::dto::{
|
||||||
CommonPrefix, DeleteMarkerEntry, EncodingType, ListObjectVersionsOutput, ListObjectsOutput, ListObjectsV2Output, Object,
|
Bucket, CommonPrefix, DeleteMarkerEntry, EncodingType, ListBucketsOutput, ListObjectVersionsOutput, ListObjectsOutput,
|
||||||
ObjectStorageClass, ObjectVersion, ObjectVersionStorageClass, Owner, Timestamp,
|
ListObjectsV2Output, Object, ObjectStorageClass, ObjectVersion, ObjectVersionStorageClass, Owner, Timestamp,
|
||||||
};
|
};
|
||||||
use s3s::{S3Error, S3ErrorCode};
|
use s3s::{S3Error, S3ErrorCode};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
@@ -63,6 +64,23 @@ pub(crate) fn build_list_objects_output(v2: ListObjectsV2Output, request_marker:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build_list_buckets_output(bucket_infos: &[BucketInfo]) -> ListBucketsOutput {
|
||||||
|
let buckets: Vec<Bucket> = bucket_infos
|
||||||
|
.iter()
|
||||||
|
.map(|bucket_info| Bucket {
|
||||||
|
creation_date: bucket_info.created.map(Timestamp::from),
|
||||||
|
name: Some(bucket_info.name.clone()),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
ListBucketsOutput {
|
||||||
|
buckets: Some(buckets),
|
||||||
|
owner: Some(rustfs_owner()),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn parse_list_object_versions_params(
|
pub(crate) fn parse_list_object_versions_params(
|
||||||
prefix: Option<String>,
|
prefix: Option<String>,
|
||||||
delimiter: Option<String>,
|
delimiter: Option<String>,
|
||||||
@@ -332,14 +350,46 @@ fn calculate_next_marker(v2: &ListObjectsV2Output) -> Option<String> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{
|
use super::{
|
||||||
build_list_object_versions_output, build_list_objects_output, build_list_objects_v2_output,
|
build_list_buckets_output, build_list_object_versions_output, build_list_objects_output, build_list_objects_v2_output,
|
||||||
parse_list_object_versions_params, parse_list_objects_v2_params,
|
parse_list_object_versions_params, parse_list_objects_v2_params,
|
||||||
};
|
};
|
||||||
use rustfs_ecstore::store_api::{ListObjectVersionsInfo, ListObjectsV2Info, ObjectInfo};
|
use crate::storage::s3_api::common::rustfs_owner;
|
||||||
|
use rustfs_ecstore::store_api::{BucketInfo, ListObjectVersionsInfo, ListObjectsV2Info, ObjectInfo};
|
||||||
use s3s::S3ErrorCode;
|
use s3s::S3ErrorCode;
|
||||||
use s3s::dto::{CommonPrefix, EncodingType, ListObjectsV2Output, Object};
|
use s3s::dto::{CommonPrefix, EncodingType, ListObjectsV2Output, Object};
|
||||||
|
use time::OffsetDateTime;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_list_buckets_output_maps_bucket_infos_and_owner() {
|
||||||
|
let bucket_infos = vec![
|
||||||
|
BucketInfo {
|
||||||
|
name: "bucket-a".to_string(),
|
||||||
|
created: Some(OffsetDateTime::UNIX_EPOCH),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
BucketInfo {
|
||||||
|
name: "bucket-b".to_string(),
|
||||||
|
created: None,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let output = build_list_buckets_output(&bucket_infos);
|
||||||
|
let buckets = output.buckets.as_ref().expect("buckets should be present");
|
||||||
|
let owner = output.owner.as_ref().expect("owner should be present");
|
||||||
|
|
||||||
|
assert_eq!(buckets.len(), 2);
|
||||||
|
assert_eq!(buckets[0].name.as_deref(), Some("bucket-a"));
|
||||||
|
assert_eq!(buckets[0].creation_date, Some(s3s::dto::Timestamp::from(OffsetDateTime::UNIX_EPOCH)));
|
||||||
|
assert_eq!(buckets[1].name.as_deref(), Some("bucket-b"));
|
||||||
|
assert_eq!(buckets[1].creation_date, None);
|
||||||
|
|
||||||
|
let expected_owner = rustfs_owner();
|
||||||
|
assert_eq!(owner.display_name, expected_owner.display_name);
|
||||||
|
assert_eq!(owner.id, expected_owner.id);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_list_objects_marker_echoes_request_value() {
|
fn test_list_objects_marker_echoes_request_value() {
|
||||||
let output = build_list_objects_output(ListObjectsV2Output::default(), Some("m-1".to_string()));
|
let output = build_list_objects_output(ListObjectsV2Output::default(), Some("m-1".to_string()));
|
||||||
|
|||||||
Reference in New Issue
Block a user