diff options
author | Alex Auvolat <alex@adnab.me> | 2024-03-19 16:57:51 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2024-03-19 16:57:51 +0100 |
commit | 783b586de93ffa210d6c34fd7cc266220a57a4d2 (patch) | |
tree | daea6fe15eb1d982ec810d4657a5f9f777a073ed /src/model/helper | |
parent | 1a0bffae3491fae6af5a8d4defc5c6b84839e197 (diff) | |
download | garage-783b586de93ffa210d6c34fd7cc266220a57a4d2.tar.gz garage-783b586de93ffa210d6c34fd7cc266220a57a4d2.zip |
[bucket-id-prefix] CLI: allow manipulating buckets by prefixes of their full IDsbucket-id-prefix
Diffstat (limited to 'src/model/helper')
-rw-r--r-- | src/model/helper/bucket.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/model/helper/bucket.rs b/src/model/helper/bucket.rs index f4e669c3..4ae9122f 100644 --- a/src/model/helper/bucket.rs +++ b/src/model/helper/bucket.rs @@ -67,6 +67,49 @@ impl<'a> BucketHelper<'a> { } } + /// Find a bucket by its global alias or a prefix of its uuid + pub async fn admin_get_existing_matching_bucket( + &self, + pattern: &String, + ) -> Result<Uuid, Error> { + if let Some(uuid) = self.resolve_global_bucket_name(pattern).await? { + return Ok(uuid); + } else if pattern.len() >= 2 { + let hexdec = pattern + .get(..pattern.len() & !1) + .and_then(|x| hex::decode(x).ok()); + if let Some(hex) = hexdec { + let mut start = [0u8; 32]; + start + .as_mut_slice() + .get_mut(..hex.len()) + .ok_or_bad_request("invalid length")? + .copy_from_slice(&hex); + let mut candidates = self + .0 + .bucket_table + .get_range( + &EmptyKey, + Some(start.into()), + Some(DeletedFilter::NotDeleted), + 10, + EnumerationOrder::Forward, + ) + .await? + .into_iter() + .collect::<Vec<_>>(); + candidates.retain(|x| hex::encode(x.id).starts_with(pattern)); + if candidates.len() == 1 { + return Ok(candidates.into_iter().next().unwrap().id); + } + } + } + Err(Error::BadRequest(format!( + "Bucket not found / several matching buckets: {}", + pattern + ))) + } + /// Returns a Bucket if it is present in bucket table, /// even if it is in deleted state. Querying a non-existing /// bucket ID returns an internal error. |