admin API refactoring (step 1) #939

Merged
lx merged 19 commits from refactor-admin into next-v2 2025-01-29 20:42:57 +00:00
7 changed files with 79 additions and 70 deletions
Showing only changes of commit 420bbc162d - Show all commits

View file

@ -950,7 +950,7 @@ paths:
post:
tags:
- Bucket aliases
operationId: "AddlBucketAlias"
operationId: "AddBucketAlias"
summary: "Add an alias to a bucket"
description: |
Add an alias for the target bucket.
@ -962,17 +962,19 @@ paths:
application/json:
schema:
type: object
required: [bucketId, alias]
required: [bucketId]
properties:
bucketId:
type: string
example: e6a14cd6a27f48684579ec6b381c078ab11697e6bc8513b72b2f5307e25fff9b
globalAlias:
type: string
localAlias:
type: string
example: my_documents
accessKeyId:
type: string
example: GK31c2f218a2e44f485b94239e
alias:
type: string
example: my_documents
responses:
'500':
description: "The server can not handle your request. Check your connectivity with the rest of the cluster."
@ -1003,17 +1005,18 @@ paths:
application/json:
schema:
type: object
required: [bucketId, alias]
required: [bucketId]
properties:
bucketId:
type: string
example: e6a14cd6a27f48684579ec6b381c078ab11697e6bc8513b72b2f5307e25fff9b
globalAlias:
type: string
example: the_bucket
localAlias:
type: string
accessKeyId:
type: string
example: GK31c2f218a2e44f485b94239e
alias:
type: string
example: my_documents
responses:
'500':
description: "The server can not handle your request. Check your connectivity with the rest of the cluster."

View file

@ -753,32 +753,32 @@ Other flags will remain unchanged.
#### AddBucketAlias `POST /v2/AddBucketAlias`
Creates an alias for a bucket in the namespace of a specific access key.
If `accessKeyId` is specified, an alias is created in the local namespace
of the key. Otherwise, a global alias is created.
To create a global alias, specify the `globalAlias` field.
To create a local alias, specify the `localAlias` and `accessKeyId` fields.
Request body format:
```json
{
"bucketId": "e6a14cd6a27f48684579ec6b381c078ab11697e6bc8513b72b2f5307e25fff9b",
"globalAlias": "my-bucket"
}
```
or:
```json
{
"bucketId": "e6a14cd6a27f48684579ec6b381c078ab11697e6bc8513b72b2f5307e25fff9b",
"accessKeyId": "GK31c2f218a2e44f485b94239e",
"alias": "my-bucket"
"localAlias": "my-bucket"
}
```
#### RemoveBucketAlias `POST /v2/RemoveBucketAlias`
Removes an alias for a bucket in the namespace of a specific access key.
If `accessKeyId` is specified, the alias is removed from the local namespace
of the key. Otherwise, the alias is removed from the global namespace.
Request body format:
```json
{
"bucketId": "e6a14cd6a27f48684579ec6b381c078ab11697e6bc8513b72b2f5307e25fff9b",
"accessKeyId": "GK31c2f218a2e44f485b94239e",
"alias": "my-bucket"
}
```
To remove a global alias, specify the `globalAlias` field.
To remove a local alias, specify the `localAlias` and `accessKeyId` fields.
Request body format: same as AddBucketAlias.

View file

@ -515,22 +515,36 @@ pub struct DenyBucketKeyResponse(pub GetBucketInfoResponse);
// ---- AddBucketAlias ----
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddBucketAliasRequest {
pub bucket_id: String,
pub access_key_id: Option<String>,
pub alias: String,
#[serde(flatten)]
pub alias: BucketAliasEnum,
}
#[derive(Serialize, Deserialize)]
pub struct AddBucketAliasResponse(pub GetBucketInfoResponse);
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum BucketAliasEnum {
#[serde(rename_all = "camelCase")]
Global { global_alias: String },
#[serde(rename_all = "camelCase")]
Local {
local_alias: String,
access_key_id: String,
},
}
// ---- RemoveBucketAlias ----
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoveBucketAliasRequest {
pub bucket_id: String,
pub access_key_id: Option<String>,
pub alias: String,
#[serde(flatten)]
pub alias: BucketAliasEnum,
}
#[derive(Serialize, Deserialize)]

View file

@ -16,15 +16,7 @@ use garage_model::permission::*;
use garage_model::s3::mpu_table;
use garage_model::s3::object_table::*;
use crate::admin::api::ApiBucketKeyPerm;
use crate::admin::api::{
AddBucketAliasRequest, AddBucketAliasResponse, AllowBucketKeyRequest, AllowBucketKeyResponse,
ApiBucketQuotas, BucketKeyPermChangeRequest, BucketLocalAlias, CreateBucketRequest,
CreateBucketResponse, DeleteBucketRequest, DeleteBucketResponse, DenyBucketKeyRequest,
DenyBucketKeyResponse, GetBucketInfoKey, GetBucketInfoRequest, GetBucketInfoResponse,
GetBucketInfoWebsiteResponse, ListBucketsRequest, ListBucketsResponse, ListBucketsResponseItem,
RemoveBucketAliasRequest, RemoveBucketAliasResponse, UpdateBucketRequest, UpdateBucketResponse,
};
use crate::admin::api::*;
use crate::admin::error::*;
use crate::admin::EndpointHandler;
use crate::common_error::CommonError;
@ -459,15 +451,18 @@ impl EndpointHandler for AddBucketAliasRequest {
let helper = garage.locked_helper().await;
match self.access_key_id {
None => {
match self.alias {
BucketAliasEnum::Global { global_alias } => {
helper
.set_global_bucket_alias(bucket_id, &self.alias)
.set_global_bucket_alias(bucket_id, &global_alias)
.await?;
}
Some(ak) => {
BucketAliasEnum::Local {
local_alias,
access_key_id,
} => {
helper
.set_local_bucket_alias(bucket_id, &ak, &self.alias)
.set_local_bucket_alias(bucket_id, &access_key_id, &local_alias)
.await?;
}
}
@ -487,15 +482,18 @@ impl EndpointHandler for RemoveBucketAliasRequest {
let helper = garage.locked_helper().await;
match self.access_key_id {
None => {
match self.alias {
BucketAliasEnum::Global { global_alias } => {
helper
.unset_global_bucket_alias(bucket_id, &self.alias)
.unset_global_bucket_alias(bucket_id, &global_alias)
.await?;
}
Some(ak) => {
BucketAliasEnum::Local {
local_alias,
access_key_id,
} => {
helper
.unset_local_bucket_alias(bucket_id, &ak, &self.alias)
.unset_local_bucket_alias(bucket_id, &access_key_id, &local_alias)
.await?;
}
}

View file

@ -10,14 +10,7 @@ use garage_rpc::layout;
use garage_model::garage::Garage;
use crate::admin::api::{
ApplyClusterLayoutRequest, ApplyClusterLayoutResponse, ConnectClusterNodeResponse,
ConnectClusterNodesRequest, ConnectClusterNodesResponse, FreeSpaceResp,
GetClusterHealthRequest, GetClusterHealthResponse, GetClusterLayoutRequest,
GetClusterLayoutResponse, GetClusterStatusRequest, GetClusterStatusResponse, NodeResp,
NodeRoleChange, NodeRoleChangeEnum, NodeRoleResp, RevertClusterLayoutRequest,
RevertClusterLayoutResponse, UpdateClusterLayoutRequest, UpdateClusterLayoutResponse,
};
use crate::admin::api::*;
use crate::admin::error::*;
use crate::admin::EndpointHandler;

View file

@ -8,12 +8,7 @@ use garage_table::*;
use garage_model::garage::Garage;
use garage_model::key_table::*;
use crate::admin::api::{
ApiBucketKeyPerm, CreateKeyRequest, CreateKeyResponse, DeleteKeyRequest, DeleteKeyResponse,
GetKeyInfoRequest, GetKeyInfoResponse, ImportKeyRequest, ImportKeyResponse,
KeyInfoBucketResponse, KeyPerm, ListKeysRequest, ListKeysResponse, ListKeysResponseItem,
UpdateKeyRequest, UpdateKeyResponse,
};
use crate::admin::api::*;
use crate::admin::error::*;
use crate::admin::EndpointHandler;

View file

@ -174,16 +174,18 @@ impl AdminApiRequest {
// Bucket aliasing
Endpoint::GlobalAliasBucket { id, alias } => {
Ok(AdminApiRequest::AddBucketAlias(AddBucketAliasRequest {
access_key_id: None,
bucket_id: id,
alias,
alias: BucketAliasEnum::Global {
global_alias: alias,
},
}))
}
Endpoint::GlobalUnaliasBucket { id, alias } => Ok(AdminApiRequest::RemoveBucketAlias(
RemoveBucketAliasRequest {
access_key_id: None,
bucket_id: id,
alias,
alias: BucketAliasEnum::Global {
global_alias: alias,
},
},
)),
Endpoint::LocalAliasBucket {
@ -191,9 +193,11 @@ impl AdminApiRequest {
access_key_id,
alias,
} => Ok(AdminApiRequest::AddBucketAlias(AddBucketAliasRequest {
access_key_id: Some(access_key_id),
bucket_id: id,
alias,
alias: BucketAliasEnum::Local {
local_alias: alias,
access_key_id,
},
})),
Endpoint::LocalUnaliasBucket {
id,
@ -201,9 +205,11 @@ impl AdminApiRequest {
alias,
} => Ok(AdminApiRequest::RemoveBucketAlias(
RemoveBucketAliasRequest {
access_key_id: Some(access_key_id),
bucket_id: id,
alias,
alias: BucketAliasEnum::Local {
local_alias: alias,
access_key_id,
},
},
)),