aboutsummaryrefslogtreecommitdiff
path: root/src/api/s3/lifecycle.rs
diff options
context:
space:
mode:
authorYureka <yuka@yuka.dev>2024-03-03 14:56:52 +0100
committerYureka <yuka@yuka.dev>2024-03-04 13:26:39 +0100
commitfb55682c66092921f766f82c16eb9e046f1bbb41 (patch)
tree744ae13518aa4e3f8cc572fb5c94a2bc19941818 /src/api/s3/lifecycle.rs
parent32d6b4def8d14d77727be9af640b1626d5153c75 (diff)
downloadgarage-fb55682c66092921f766f82c16eb9e046f1bbb41.tar.gz
garage-fb55682c66092921f766f82c16eb9e046f1bbb41.zip
add request context helper
Diffstat (limited to 'src/api/s3/lifecycle.rs')
-rw-r--r--src/api/s3/lifecycle.rs53
1 files changed, 28 insertions, 25 deletions
diff --git a/src/api/s3/lifecycle.rs b/src/api/s3/lifecycle.rs
index 35757e8c..7eb1c2cb 100644
--- a/src/api/s3/lifecycle.rs
+++ b/src/api/s3/lifecycle.rs
@@ -1,5 +1,4 @@
use quick_xml::de::from_reader;
-use std::sync::Arc;
use http_body_util::BodyExt;
use hyper::{Request, Response, StatusCode};
@@ -16,15 +15,12 @@ use garage_model::bucket_table::{
parse_lifecycle_date, Bucket, LifecycleExpiration as GarageLifecycleExpiration,
LifecycleFilter as GarageLifecycleFilter, LifecycleRule as GarageLifecycleRule,
};
-use garage_model::garage::Garage;
use garage_util::data::*;
-pub async fn handle_get_lifecycle(bucket: &Bucket) -> Result<Response<ResBody>, Error> {
- let param = bucket
- .params()
- .ok_or_internal_error("Bucket should not be deleted at this point")?;
+pub async fn handle_get_lifecycle(ctx: ReqCtx) -> Result<Response<ResBody>, Error> {
+ let ReqCtx { bucket_params, .. } = ctx;
- if let Some(lifecycle) = param.lifecycle_config.get() {
+ if let Some(lifecycle) = bucket_params.lifecycle_config.get() {
let wc = LifecycleConfiguration::from_garage_lifecycle_config(lifecycle);
let xml = to_xml_with_header(&wc)?;
Ok(Response::builder()
@@ -38,16 +34,18 @@ pub async fn handle_get_lifecycle(bucket: &Bucket) -> Result<Response<ResBody>,
}
}
-pub async fn handle_delete_lifecycle(
- garage: Arc<Garage>,
- mut bucket: Bucket,
-) -> Result<Response<ResBody>, Error> {
- let param = bucket
- .params_mut()
- .ok_or_internal_error("Bucket should not be deleted at this point")?;
-
- param.lifecycle_config.update(None);
- garage.bucket_table.insert(&bucket).await?;
+pub async fn handle_delete_lifecycle(ctx: ReqCtx) -> Result<Response<ResBody>, Error> {
+ let ReqCtx {
+ garage,
+ bucket_id,
+ mut bucket_params,
+ ..
+ } = ctx;
+ bucket_params.lifecycle_config.update(None);
+ garage
+ .bucket_table
+ .insert(&Bucket::present(bucket_id, bucket_params))
+ .await?;
Ok(Response::builder()
.status(StatusCode::NO_CONTENT)
@@ -55,28 +53,33 @@ pub async fn handle_delete_lifecycle(
}
pub async fn handle_put_lifecycle(
- garage: Arc<Garage>,
- mut bucket: Bucket,
+ ctx: ReqCtx,
req: Request<ReqBody>,
content_sha256: Option<Hash>,
) -> Result<Response<ResBody>, Error> {
+ let ReqCtx {
+ garage,
+ bucket_id,
+ mut bucket_params,
+ ..
+ } = ctx;
+
let body = BodyExt::collect(req.into_body()).await?.to_bytes();
if let Some(content_sha256) = content_sha256 {
verify_signed_content(content_sha256, &body[..])?;
}
- let param = bucket
- .params_mut()
- .ok_or_internal_error("Bucket should not be deleted at this point")?;
-
let conf: LifecycleConfiguration = from_reader(&body as &[u8])?;
let config = conf
.validate_into_garage_lifecycle_config()
.ok_or_bad_request("Invalid lifecycle configuration")?;
- param.lifecycle_config.update(Some(config));
- garage.bucket_table.insert(&bucket).await?;
+ bucket_params.lifecycle_config.update(Some(config));
+ garage
+ .bucket_table
+ .insert(&Bucket::present(bucket_id, bucket_params))
+ .await?;
Ok(Response::builder()
.status(StatusCode::OK)