From 8ff95f09c9edf218b6302470cd57868147316e59 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 17 May 2022 16:42:13 +0200 Subject: [PATCH] Return website config in GetBucketInfo, use serde(rename_all) --- src/api/admin/bucket.rs | 109 ++++++++++++++++++++++----------------- src/api/admin/cluster.rs | 4 +- src/api/admin/key.rs | 9 ++-- 3 files changed, 67 insertions(+), 55 deletions(-) diff --git a/src/api/admin/bucket.rs b/src/api/admin/bucket.rs index 00450319..dacbd427 100644 --- a/src/api/admin/bucket.rs +++ b/src/api/admin/bucket.rs @@ -67,17 +67,16 @@ pub async fn handle_list_buckets(garage: &Arc) -> Result, } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct ListBucketResultItem { id: String, - #[serde(rename = "globalAliases")] global_aliases: Vec, - #[serde(rename = "localAliases")] local_aliases: Vec, } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct BucketLocalAlias { - #[serde(rename = "accessKeyId")] access_key_id: String, alias: String, } @@ -156,42 +155,50 @@ async fn bucket_info_results( let state = bucket.state.as_option().unwrap(); - let res = GetBucketInfoResult { - id: hex::encode(&bucket.id), - global_aliases: state - .aliases - .items() - .iter() - .filter(|(_, _, a)| *a) - .map(|(n, _, _)| n.to_string()) - .collect::>(), - keys: relevant_keys - .into_iter() - .map(|(_, key)| { - let p = key.state.as_option().unwrap(); - GetBucketInfoKey { - access_key_id: key.key_id, - name: p.name.get().to_string(), - permissions: p - .authorized_buckets - .get(&bucket.id) - .map(|p| ApiBucketKeyPerm { - read: p.allow_read, - write: p.allow_write, - owner: p.allow_owner, - }) - .unwrap_or_default(), - bucket_local_aliases: p - .local_aliases - .items() - .iter() - .filter(|(_, _, b)| *b == Some(bucket.id)) - .map(|(n, _, _)| n.to_string()) - .collect::>(), + let res = + GetBucketInfoResult { + id: hex::encode(&bucket.id), + global_aliases: state + .aliases + .items() + .iter() + .filter(|(_, _, a)| *a) + .map(|(n, _, _)| n.to_string()) + .collect::>(), + website_access: state.website_config.get().is_some(), + website_config: state.website_config.get().clone().map(|wsc| { + GetBucketInfoWebsiteResult { + index_document: wsc.index_document, + error_document: wsc.error_document, } - }) - .collect::>(), - }; + }), + keys: relevant_keys + .into_iter() + .map(|(_, key)| { + let p = key.state.as_option().unwrap(); + GetBucketInfoKey { + access_key_id: key.key_id, + name: p.name.get().to_string(), + permissions: p + .authorized_buckets + .get(&bucket.id) + .map(|p| ApiBucketKeyPerm { + read: p.allow_read, + write: p.allow_write, + owner: p.allow_owner, + }) + .unwrap_or_default(), + bucket_local_aliases: p + .local_aliases + .items() + .iter() + .filter(|(_, _, b)| *b == Some(bucket.id)) + .map(|(n, _, _)| n.to_string()) + .collect::>(), + } + }) + .collect::>(), + }; let resp_json = serde_json::to_string_pretty(&res).map_err(GarageError::from)?; Ok(Response::builder() @@ -200,21 +207,29 @@ async fn bucket_info_results( } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct GetBucketInfoResult { id: String, - #[serde(rename = "globalAliases")] global_aliases: Vec, + website_access: bool, + #[serde(default)] + website_config: Option, keys: Vec, } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct GetBucketInfoWebsiteResult { + index_document: String, + error_document: Option, +} + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct GetBucketInfoKey { - #[serde(rename = "accessKeyId")] access_key_id: String, - #[serde(rename = "name")] name: String, permissions: ApiBucketKeyPerm, - #[serde(rename = "bucketLocalAliases")] bucket_local_aliases: Vec, } @@ -293,19 +308,18 @@ pub async fn handle_create_bucket( } #[derive(Deserialize)] +#[serde(rename_all = "camelCase")] struct CreateBucketRequest { - #[serde(rename = "globalAlias")] global_alias: Option, - #[serde(rename = "localAlias")] local_alias: Option, } #[derive(Deserialize)] +#[serde(rename_all = "camelCase")] struct CreateBucketLocalAlias { - #[serde(rename = "accessKeyId")] access_key_id: String, alias: String, - #[serde(rename = "allPermissions", default)] + #[serde(default)] all_permissions: bool, } @@ -420,10 +434,9 @@ pub async fn handle_bucket_change_key_perm( } #[derive(Deserialize)] +#[serde(rename_all = "camelCase")] struct BucketKeyPermChangeRequest { - #[serde(rename = "bucketId")] bucket_id: String, - #[serde(rename = "accessKeyId")] access_key_id: String, permissions: ApiBucketKeyPerm, } diff --git a/src/api/admin/cluster.rs b/src/api/admin/cluster.rs index 91d99d8a..44ad4a37 100644 --- a/src/api/admin/cluster.rs +++ b/src/api/admin/cluster.rs @@ -76,19 +76,19 @@ fn get_cluster_layout(garage: &Arc) -> GetClusterLayoutResponse { } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct GetClusterStatusResponse { node: String, garage_version: &'static str, - #[serde(rename = "knownNodes")] known_nodes: HashMap, layout: GetClusterLayoutResponse, } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct GetClusterLayoutResponse { version: u64, roles: HashMap>, - #[serde(rename = "stagedRoleChanges")] staged_role_changes: HashMap>, } diff --git a/src/api/admin/key.rs b/src/api/admin/key.rs index 1e910d52..be37088b 100644 --- a/src/api/admin/key.rs +++ b/src/api/admin/key.rs @@ -203,28 +203,27 @@ async fn key_info_results(garage: &Arc, key: Key) -> Result, } #[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] struct KeyPerm { - #[serde(rename = "createBucket", default)] + #[serde(default)] create_bucket: bool, } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct KeyInfoBucketResult { id: String, - #[serde(rename = "globalAliases")] global_aliases: Vec, - #[serde(rename = "localAliases")] local_aliases: Vec, permissions: ApiBucketKeyPerm, }