use quick_xml::se::to_string; use serde::{Deserialize, Serialize, Serializer}; use crate::Error as ApiError; pub fn to_xml_with_header(x: &T) -> Result { let mut xml = r#""#.to_string(); xml.push_str(&to_string(x)?); Ok(xml) } pub fn xmlns_tag(_v: &(), s: S) -> Result { s.serialize_str("http://s3.amazonaws.com/doc/2006-03-01/") } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub struct Value(#[serde(rename = "$value")] pub String); impl From<&str> for Value { fn from(s: &str) -> Value { Value(s.to_string()) } } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub struct IntValue(#[serde(rename = "$value")] pub i64); #[derive(Debug, Serialize, PartialEq)] pub struct Bucket { #[serde(rename = "CreationDate")] pub creation_date: Value, #[serde(rename = "Name")] pub name: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct Owner { #[serde(rename = "DisplayName")] pub display_name: Value, #[serde(rename = "ID")] pub id: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct BucketList { #[serde(rename = "Bucket")] pub entries: Vec, } #[derive(Debug, Serialize, PartialEq)] pub struct ListAllMyBucketsResult { #[serde(rename = "Buckets")] pub buckets: BucketList, #[serde(rename = "Owner")] pub owner: Owner, } #[derive(Debug, Serialize, PartialEq)] pub struct LocationConstraint { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "$value")] pub region: String, } #[derive(Debug, Serialize, PartialEq)] pub struct Deleted { #[serde(rename = "Key")] pub key: Value, #[serde(rename = "VersionId")] pub version_id: Value, #[serde(rename = "DeleteMarkerVersionId")] pub delete_marker_version_id: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct Error { #[serde(rename = "Code")] pub code: Value, #[serde(rename = "Message")] pub message: Value, #[serde(rename = "Resource")] pub resource: Option, #[serde(rename = "Region")] pub region: Option, } #[derive(Debug, Serialize, PartialEq)] pub struct DeleteError { #[serde(rename = "Code")] pub code: Value, #[serde(rename = "Key")] pub key: Option, #[serde(rename = "Message")] pub message: Value, #[serde(rename = "VersionId")] pub version_id: Option, } #[derive(Debug, Serialize, PartialEq)] pub struct DeleteResult { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Deleted")] pub deleted: Vec, #[serde(rename = "Error")] pub errors: Vec, } #[derive(Debug, Serialize, PartialEq)] pub struct InitiateMultipartUploadResult { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Bucket")] pub bucket: Value, #[serde(rename = "Key")] pub key: Value, #[serde(rename = "UploadId")] pub upload_id: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct CompleteMultipartUploadResult { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Location")] pub location: Option, #[serde(rename = "Bucket")] pub bucket: Value, #[serde(rename = "Key")] pub key: Value, #[serde(rename = "ETag")] pub etag: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct Initiator { #[serde(rename = "DisplayName")] pub display_name: Value, #[serde(rename = "ID")] pub id: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct ListMultipartItem { #[serde(rename = "Initiated")] pub initiated: Value, #[serde(rename = "Initiator")] pub initiator: Initiator, #[serde(rename = "Key")] pub key: Value, #[serde(rename = "UploadId")] pub upload_id: Value, #[serde(rename = "Owner")] pub owner: Owner, #[serde(rename = "StorageClass")] pub storage_class: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct ListMultipartUploadsResult { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Bucket")] pub bucket: Value, #[serde(rename = "KeyMarker")] pub key_marker: Option, #[serde(rename = "UploadIdMarker")] pub upload_id_marker: Option, #[serde(rename = "NextKeyMarker")] pub next_key_marker: Option, #[serde(rename = "NextUploadIdMarker")] pub next_upload_id_marker: Option, #[serde(rename = "Prefix")] pub prefix: Value, #[serde(rename = "Delimiter")] pub delimiter: Option, #[serde(rename = "MaxUploads")] pub max_uploads: IntValue, #[serde(rename = "IsTruncated")] pub is_truncated: Value, #[serde(rename = "Upload")] pub upload: Vec, #[serde(rename = "CommonPrefixes")] pub common_prefixes: Vec, #[serde(rename = "EncodingType")] pub encoding_type: Option, } #[derive(Debug, Serialize, PartialEq)] pub struct PartItem { #[serde(rename = "ETag")] pub etag: Value, #[serde(rename = "LastModified")] pub last_modified: Value, #[serde(rename = "PartNumber")] pub part_number: IntValue, #[serde(rename = "Size")] pub size: IntValue, } #[derive(Debug, Serialize, PartialEq)] pub struct ListPartsResult { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Bucket")] pub bucket: Value, #[serde(rename = "Key")] pub key: Value, #[serde(rename = "UploadId")] pub upload_id: Value, #[serde(rename = "PartNumberMarker")] pub part_number_marker: Option, #[serde(rename = "NextPartNumberMarker")] pub next_part_number_marker: Option, #[serde(rename = "MaxParts")] pub max_parts: IntValue, #[serde(rename = "IsTruncated")] pub is_truncated: Value, #[serde(rename = "Part", default)] pub parts: Vec, #[serde(rename = "Initiator")] pub initiator: Initiator, #[serde(rename = "Owner")] pub owner: Owner, #[serde(rename = "StorageClass")] pub storage_class: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct ListBucketItem { #[serde(rename = "Key")] pub key: Value, #[serde(rename = "LastModified")] pub last_modified: Value, #[serde(rename = "ETag")] pub etag: Value, #[serde(rename = "Size")] pub size: IntValue, #[serde(rename = "StorageClass")] pub storage_class: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct CommonPrefix { #[serde(rename = "Prefix")] pub prefix: Value, } #[derive(Debug, Serialize, PartialEq)] pub struct ListBucketResult { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Name")] pub name: Value, #[serde(rename = "Prefix")] pub prefix: Value, #[serde(rename = "Marker")] pub marker: Option, #[serde(rename = "NextMarker")] pub next_marker: Option, #[serde(rename = "StartAfter")] pub start_after: Option, #[serde(rename = "ContinuationToken")] pub continuation_token: Option, #[serde(rename = "NextContinuationToken")] pub next_continuation_token: Option, #[serde(rename = "KeyCount")] pub key_count: Option, #[serde(rename = "MaxKeys")] pub max_keys: IntValue, #[serde(rename = "Delimiter")] pub delimiter: Option, #[serde(rename = "EncodingType")] pub encoding_type: Option, #[serde(rename = "IsTruncated")] pub is_truncated: Value, #[serde(rename = "Contents")] pub contents: Vec, #[serde(rename = "CommonPrefixes")] pub common_prefixes: Vec, } #[derive(Debug, Serialize, PartialEq)] pub struct VersioningConfiguration { #[serde(serialize_with = "xmlns_tag")] pub xmlns: (), #[serde(rename = "Status")] pub status: Option, } #[cfg(test)] mod tests { use super::*; use garage_util::time::*; #[test] fn error_message() -> Result<(), ApiError> { let error = Error { code: Value("TestError".to_string()), message: Value("A dummy error message".to_string()), resource: Some(Value("/bucket/a/plop".to_string())), region: Some(Value("garage".to_string())), }; assert_eq!( to_xml_with_header(&error)?, "\ \ TestError\ A dummy error message\ /bucket/a/plop\ garage\ " ); Ok(()) } #[test] fn list_all_my_buckets_result() -> Result<(), ApiError> { let list_buckets = ListAllMyBucketsResult { owner: Owner { display_name: Value("owner_name".to_string()), id: Value("qsdfjklm".to_string()), }, buckets: BucketList { entries: vec![ Bucket { creation_date: Value(msec_to_rfc3339(0)), name: Value("bucket_A".to_string()), }, Bucket { creation_date: Value(msec_to_rfc3339(3600 * 24 * 1000)), name: Value("bucket_B".to_string()), }, ], }, }; assert_eq!( to_xml_with_header(&list_buckets)?, "\ \ \ \ 1970-01-01T00:00:00.000Z\ bucket_A\ \ \ 1970-01-02T00:00:00.000Z\ bucket_B\ \ \ \ owner_name\ qsdfjklm\ \ " ); Ok(()) } #[test] fn get_bucket_location_result() -> Result<(), ApiError> { let get_bucket_location = LocationConstraint { xmlns: (), region: "garage".to_string(), }; assert_eq!( to_xml_with_header(&get_bucket_location)?, "\ garage" ); Ok(()) } #[test] fn get_bucket_versioning_result() -> Result<(), ApiError> { let get_bucket_versioning = VersioningConfiguration { xmlns: (), status: None, }; assert_eq!( to_xml_with_header(&get_bucket_versioning)?, "\ " ); let get_bucket_versioning2 = VersioningConfiguration { xmlns: (), status: Some(Value("Suspended".to_string())), }; assert_eq!( to_xml_with_header(&get_bucket_versioning2)?, "\ Suspended" ); Ok(()) } #[test] fn delete_result() -> Result<(), ApiError> { let delete_result = DeleteResult { xmlns: (), deleted: vec![ Deleted { key: Value("a/plop".to_string()), version_id: Value("qsdfjklm".to_string()), delete_marker_version_id: Value("wxcvbn".to_string()), }, Deleted { key: Value("b/plip".to_string()), version_id: Value("1234".to_string()), delete_marker_version_id: Value("4321".to_string()), }, ], errors: vec![ DeleteError { code: Value("NotFound".to_string()), key: Some(Value("c/plap".to_string())), message: Value("Object c/plap not found".to_string()), version_id: None, }, DeleteError { code: Value("Forbidden".to_string()), key: Some(Value("d/plep".to_string())), message: Value("Not authorized".to_string()), version_id: Some(Value("789".to_string())), }, ], }; assert_eq!( to_xml_with_header(&delete_result)?, "\ \ \ a/plop\ qsdfjklm\ wxcvbn\ \ \ b/plip\ 1234\ 4321\ \ \ NotFound\ c/plap\ Object c/plap not found\ \ \ Forbidden\ d/plep\ Not authorized\ 789\ \ " ); Ok(()) } #[test] fn initiate_multipart_upload_result() -> Result<(), ApiError> { let result = InitiateMultipartUploadResult { xmlns: (), bucket: Value("mybucket".to_string()), key: Value("a/plop".to_string()), upload_id: Value("azerty".to_string()), }; assert_eq!( to_xml_with_header(&result)?, "\ \ mybucket\ a/plop\ azerty\ " ); Ok(()) } #[test] fn complete_multipart_upload_result() -> Result<(), ApiError> { let result = CompleteMultipartUploadResult { xmlns: (), location: Some(Value("https://garage.tld/mybucket/a/plop".to_string())), bucket: Value("mybucket".to_string()), key: Value("a/plop".to_string()), etag: Value("\"3858f62230ac3c915f300c664312c11f-9\"".to_string()), }; assert_eq!( to_xml_with_header(&result)?, "\ \ https://garage.tld/mybucket/a/plop\ mybucket\ a/plop\ "3858f62230ac3c915f300c664312c11f-9"\ " ); Ok(()) } #[test] fn list_multipart_uploads_result() -> Result<(), ApiError> { let result = ListMultipartUploadsResult { xmlns: (), bucket: Value("example-bucket".to_string()), key_marker: None, next_key_marker: None, upload_id_marker: None, encoding_type: None, next_upload_id_marker: None, upload: vec![], delimiter: Some(Value("/".to_string())), prefix: Value("photos/2006/".to_string()), max_uploads: IntValue(1000), is_truncated: Value("false".to_string()), common_prefixes: vec![ CommonPrefix { prefix: Value("photos/2006/February/".to_string()), }, CommonPrefix { prefix: Value("photos/2006/January/".to_string()), }, CommonPrefix { prefix: Value("photos/2006/March/".to_string()), }, ], }; assert_eq!( to_xml_with_header(&result)?, "\ \ example-bucket\ photos/2006/\ /\ 1000\ false\ \ photos/2006/February/\ \ \ photos/2006/January/\ \ \ photos/2006/March/\ \ " ); Ok(()) } #[test] fn list_objects_v1_1() -> Result<(), ApiError> { let result = ListBucketResult { xmlns: (), name: Value("example-bucket".to_string()), prefix: Value("".to_string()), marker: Some(Value("".to_string())), next_marker: None, start_after: None, continuation_token: None, next_continuation_token: None, key_count: None, max_keys: IntValue(1000), encoding_type: None, delimiter: Some(Value("/".to_string())), is_truncated: Value("false".to_string()), contents: vec![ListBucketItem { key: Value("sample.jpg".to_string()), last_modified: Value(msec_to_rfc3339(0)), etag: Value("\"bf1d737a4d46a19f3bced6905cc8b902\"".to_string()), size: IntValue(142863), storage_class: Value("STANDARD".to_string()), }], common_prefixes: vec![CommonPrefix { prefix: Value("photos/".to_string()), }], }; assert_eq!( to_xml_with_header(&result)?, "\ \ example-bucket\ \ \ 1000\ /\ false\ \ sample.jpg\ 1970-01-01T00:00:00.000Z\ "bf1d737a4d46a19f3bced6905cc8b902"\ 142863\ STANDARD\ \ \ photos/\ \ " ); Ok(()) } #[test] fn list_objects_v1_2() -> Result<(), ApiError> { let result = ListBucketResult { xmlns: (), name: Value("example-bucket".to_string()), prefix: Value("photos/2006/".to_string()), marker: Some(Value("".to_string())), next_marker: None, start_after: None, continuation_token: None, next_continuation_token: None, key_count: None, max_keys: IntValue(1000), delimiter: Some(Value("/".to_string())), encoding_type: None, is_truncated: Value("false".to_string()), contents: vec![], common_prefixes: vec![ CommonPrefix { prefix: Value("photos/2006/February/".to_string()), }, CommonPrefix { prefix: Value("photos/2006/January/".to_string()), }, ], }; assert_eq!( to_xml_with_header(&result)?, "\ \ example-bucket\ photos/2006/\ \ 1000\ /\ false\ \ photos/2006/February/\ \ \ photos/2006/January/\ \ " ); Ok(()) } #[test] fn list_objects_v2_1() -> Result<(), ApiError> { let result = ListBucketResult { xmlns: (), name: Value("quotes".to_string()), prefix: Value("E".to_string()), marker: None, next_marker: None, start_after: Some(Value("ExampleGuide.pdf".to_string())), continuation_token: None, next_continuation_token: None, key_count: None, max_keys: IntValue(3), delimiter: None, encoding_type: None, is_truncated: Value("false".to_string()), contents: vec![ListBucketItem { key: Value("ExampleObject.txt".to_string()), last_modified: Value(msec_to_rfc3339(0)), etag: Value("\"599bab3ed2c697f1d26842727561fd94\"".to_string()), size: IntValue(857), storage_class: Value("REDUCED_REDUNDANCY".to_string()), }], common_prefixes: vec![], }; assert_eq!( to_xml_with_header(&result)?, "\ \ quotes\ E\ ExampleGuide.pdf\ 3\ false\ \ ExampleObject.txt\ 1970-01-01T00:00:00.000Z\ "599bab3ed2c697f1d26842727561fd94"\ 857\ REDUCED_REDUNDANCY\ \ " ); Ok(()) } #[test] fn list_objects_v2_2() -> Result<(), ApiError> { let result = ListBucketResult { xmlns: (), name: Value("bucket".to_string()), prefix: Value("".to_string()), marker: None, next_marker: None, start_after: None, continuation_token: Some(Value( "1ueGcxLPRx1Tr/XYExHnhbYLgveDs2J/wm36Hy4vbOwM=".to_string(), )), next_continuation_token: Some(Value("qsdfjklm".to_string())), key_count: Some(IntValue(112)), max_keys: IntValue(1000), delimiter: None, encoding_type: None, is_truncated: Value("false".to_string()), contents: vec![ListBucketItem { key: Value("happyfacex.jpg".to_string()), last_modified: Value(msec_to_rfc3339(0)), etag: Value("\"70ee1738b6b21e2c8a43f3a5ab0eee71\"".to_string()), size: IntValue(1111), storage_class: Value("STANDARD".to_string()), }], common_prefixes: vec![], }; assert_eq!( to_xml_with_header(&result)?, "\ \ bucket\ \ 1ueGcxLPRx1Tr/XYExHnhbYLgveDs2J/wm36Hy4vbOwM=\ qsdfjklm\ 112\ 1000\ false\ \ happyfacex.jpg\ 1970-01-01T00:00:00.000Z\ "70ee1738b6b21e2c8a43f3a5ab0eee71"\ 1111\ STANDARD\ \ " ); Ok(()) } #[test] fn list_parts() -> Result<(), ApiError> { let result = ListPartsResult { xmlns: (), bucket: Value("example-bucket".to_string()), key: Value("example-object".to_string()), upload_id: Value( "XXBsb2FkIElEIGZvciBlbHZpbmcncyVcdS1tb3ZpZS5tMnRzEEEwbG9hZA".to_string(), ), part_number_marker: Some(IntValue(1)), next_part_number_marker: Some(IntValue(3)), max_parts: IntValue(2), is_truncated: Value("true".to_string()), parts: vec![ PartItem { etag: Value("\"7778aef83f66abc1fa1e8477f296d394\"".to_string()), last_modified: Value("2010-11-10T20:48:34.000Z".to_string()), part_number: IntValue(2), size: IntValue(10485760), }, PartItem { etag: Value("\"aaaa18db4cc2f85cedef654fccc4a4x8\"".to_string()), last_modified: Value("2010-11-10T20:48:33.000Z".to_string()), part_number: IntValue(3), size: IntValue(10485760), }, ], initiator: Initiator { display_name: Value("umat-user-11116a31-17b5-4fb7-9df5-b288870f11xx".to_string()), id: Value( "arn:aws:iam::111122223333:user/some-user-11116a31-17b5-4fb7-9df5-b288870f11xx" .to_string(), ), }, owner: Owner { display_name: Value("someName".to_string()), id: Value( "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a".to_string(), ), }, storage_class: Value("STANDARD".to_string()), }; assert_eq!( to_xml_with_header(&result)?, "\ \ example-bucket\ example-object\ XXBsb2FkIElEIGZvciBlbHZpbmcncyVcdS1tb3ZpZS5tMnRzEEEwbG9hZA\ 1\ 3\ 2\ true\ \ "7778aef83f66abc1fa1e8477f296d394"\ 2010-11-10T20:48:34.000Z\ 2\ 10485760\ \ \ "aaaa18db4cc2f85cedef654fccc4a4x8"\ 2010-11-10T20:48:33.000Z\ 3\ 10485760\ \ \ umat-user-11116a31-17b5-4fb7-9df5-b288870f11xx\ arn:aws:iam::111122223333:user/some-user-11116a31-17b5-4fb7-9df5-b288870f11xx\ \ \ someName\ 75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a\ \ STANDARD\ " ); Ok(()) } }