2021-12-14 12:55:11 +00:00
|
|
|
use garage_table::util::EmptyKey;
|
2022-01-03 12:58:05 +00:00
|
|
|
use garage_util::data::*;
|
2021-12-14 12:55:11 +00:00
|
|
|
|
|
|
|
use crate::bucket_table::Bucket;
|
|
|
|
use crate::garage::Garage;
|
2022-01-03 12:58:05 +00:00
|
|
|
use crate::helper::error::*;
|
2021-12-14 12:55:11 +00:00
|
|
|
|
|
|
|
pub struct BucketHelper<'a>(pub(crate) &'a Garage);
|
|
|
|
|
|
|
|
impl<'a> BucketHelper<'a> {
|
2021-12-17 10:53:13 +00:00
|
|
|
#[allow(clippy::ptr_arg)]
|
2021-12-14 12:55:11 +00:00
|
|
|
pub async fn resolve_global_bucket_name(
|
|
|
|
&self,
|
|
|
|
bucket_name: &String,
|
|
|
|
) -> Result<Option<Uuid>, Error> {
|
2021-12-17 10:53:13 +00:00
|
|
|
// Bucket names in Garage are aliases, true bucket identifiers
|
|
|
|
// are 32-byte UUIDs. This function resolves bucket names into
|
|
|
|
// their full identifier by looking up in the bucket_alias_table.
|
|
|
|
// This function also allows buckets to be identified by their
|
|
|
|
// full UUID (hex-encoded). Here, if the name to be resolved is a
|
|
|
|
// hex string of the correct length, it is directly parsed as a bucket
|
|
|
|
// identifier which is returned. There is no risk of this conflicting
|
|
|
|
// with an actual bucket name: bucket names are max 63 chars long by
|
|
|
|
// the AWS spec, and hex-encoded UUIDs are 64 chars long.
|
2021-12-15 17:36:15 +00:00
|
|
|
let hexbucket = hex::decode(bucket_name.as_str())
|
|
|
|
.ok()
|
|
|
|
.map(|by| Uuid::try_from(&by))
|
|
|
|
.flatten();
|
|
|
|
if let Some(bucket_id) = hexbucket {
|
|
|
|
Ok(self
|
|
|
|
.0
|
|
|
|
.bucket_table
|
|
|
|
.get(&bucket_id, &EmptyKey)
|
|
|
|
.await?
|
|
|
|
.filter(|x| !x.state.is_deleted())
|
|
|
|
.map(|_| bucket_id))
|
|
|
|
} else {
|
|
|
|
Ok(self
|
|
|
|
.0
|
|
|
|
.bucket_alias_table
|
|
|
|
.get(&EmptyKey, bucket_name)
|
|
|
|
.await?
|
|
|
|
.map(|x| x.state.get().as_option().map(|x| x.bucket_id))
|
|
|
|
.flatten())
|
|
|
|
}
|
2021-12-14 12:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_existing_bucket(&self, bucket_id: Uuid) -> Result<Bucket, Error> {
|
|
|
|
self.0
|
|
|
|
.bucket_table
|
|
|
|
.get(&bucket_id, &EmptyKey)
|
|
|
|
.await?
|
|
|
|
.filter(|b| !b.is_deleted())
|
2022-01-03 12:58:05 +00:00
|
|
|
.ok_or_bad_request(format!("Bucket {:?} does not exist", bucket_id))
|
2021-12-14 12:55:11 +00:00
|
|
|
}
|
|
|
|
}
|