aboutsummaryrefslogtreecommitdiff
path: root/src/api/s3_bucket.rs
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2021-05-02 22:30:56 +0200
committerGitea <gitea@fake.local>2021-05-03 21:55:30 +0200
commit631c36b3ff76f9b09a5aed54dc343469a89f9989 (patch)
tree3f9c3b9741de7afb012cd354e3da6316750c13b5 /src/api/s3_bucket.rs
parentee2a3d363b70ca9e7572c30ca9547f7fd7f8f608 (diff)
downloadgarage-631c36b3ff76f9b09a5aed54dc343469a89f9989.tar.gz
garage-631c36b3ff76f9b09a5aed54dc343469a89f9989.zip
S3 API: support ListBuckets
Diffstat (limited to 'src/api/s3_bucket.rs')
-rw-r--r--src/api/s3_bucket.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/api/s3_bucket.rs b/src/api/s3_bucket.rs
index cbefd005..d1a4425a 100644
--- a/src/api/s3_bucket.rs
+++ b/src/api/s3_bucket.rs
@@ -2,11 +2,62 @@ use std::fmt::Write;
use std::sync::Arc;
use hyper::{Body, Response};
+use quick_xml::se::to_string;
+use serde::Serialize;
use garage_model::garage::Garage;
+use garage_model::key_table::Key;
+use garage_util::time::*;
use crate::error::*;
+#[derive(Debug, Serialize, PartialEq)]
+struct CreationDate {
+ #[serde(rename = "$value")]
+ pub body: String,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct Name {
+ #[serde(rename = "$value")]
+ pub body: String,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct Bucket {
+ #[serde(rename = "CreationDate")]
+ pub creation_date: CreationDate,
+ #[serde(rename = "Name")]
+ pub name: Name,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct DisplayName {
+ #[serde(rename = "$value")]
+ pub body: String,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct ID {
+ #[serde(rename = "$value")]
+ pub body: String,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct Owner {
+ #[serde(rename = "DisplayName")]
+ display_name: DisplayName,
+ #[serde(rename = "ID")]
+ id: ID,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct BucketList {
+ #[serde(rename = "Bucket")]
+ pub entries: Vec<Bucket>,
+}
+#[derive(Debug, Serialize, PartialEq)]
+struct ListAllMyBucketsResult {
+ #[serde(rename = "Buckets")]
+ buckets: BucketList,
+ #[serde(rename = "Owner")]
+ owner: Owner,
+}
+
pub fn handle_get_bucket_location(garage: Arc<Garage>) -> Result<Response<Body>, Error> {
let mut xml = String::new();
@@ -22,3 +73,39 @@ pub fn handle_get_bucket_location(garage: Arc<Garage>) -> Result<Response<Body>,
.header("Content-Type", "application/xml")
.body(Body::from(xml.into_bytes()))?)
}
+
+pub fn handle_list_buckets(api_key: &Key) -> Result<Response<Body>, Error> {
+ let list_buckets = ListAllMyBucketsResult {
+ owner: Owner {
+ display_name: DisplayName {
+ body: api_key.name.get().to_string(),
+ },
+ id: ID {
+ body: api_key.key_id.to_string(),
+ },
+ },
+ buckets: BucketList {
+ entries: api_key
+ .authorized_buckets
+ .items()
+ .iter()
+ .map(|(name, ts, _)| Bucket {
+ creation_date: CreationDate {
+ body: msec_to_rfc3339(*ts),
+ },
+ name: Name {
+ body: name.to_string(),
+ },
+ })
+ .collect(),
+ },
+ };
+
+ let mut xml = r#"<?xml version="1.0" encoding="UTF-8"?>"#.to_string();
+ xml.push_str(&to_string(&list_buckets)?);
+ trace!("xml: {}", xml);
+
+ Ok(Response::builder()
+ .header("Content-Type", "application/xml")
+ .body(Body::from(xml))?)
+}