use super::*;
impl S3Service {
pub(crate) fn put_bucket_replication(
&self,
account_id: &str,
req: &AwsRequest,
bucket: &str,
) -> Result<AwsResponse, AwsServiceError> {
let body_str = std::str::from_utf8(&req.body).unwrap_or("").to_string();
let mut accts = self.state.write();
let state = accts.get_or_create(account_id);
let b = state
.buckets
.get_mut(bucket)
.ok_or_else(|| no_such_bucket(bucket))?;
if b.versioning.as_deref() != Some("Enabled") {
return Err(AwsServiceError::aws_error_with_fields(
StatusCode::BAD_REQUEST,
"InvalidRequest",
"Versioning must be 'Enabled' on the bucket to apply a replication configuration",
vec![("BucketName".to_string(), bucket.to_string())],
));
}
let normalized = normalize_replication_xml(&body_str);
b.replication_config = Some(normalized.clone());
self.store
.put_bucket_subresource(bucket, BucketSubresource::Replication, &normalized)
.map_err(crate::service::persistence_error)?;
Ok(empty_response(StatusCode::OK))
}
pub(crate) fn get_bucket_replication(
&self,
account_id: &str,
bucket: &str,
) -> Result<AwsResponse, AwsServiceError> {
let accts = self.state.read();
let __empty = crate::state::S3State::new(account_id, "us-east-1");
let state = accts.get(account_id).unwrap_or(&__empty);
let b = state
.buckets
.get(bucket)
.ok_or_else(|| no_such_bucket(bucket))?;
match &b.replication_config {
Some(config) => Ok(s3_xml(StatusCode::OK, config.clone())),
None => Err(AwsServiceError::aws_error_with_fields(
StatusCode::NOT_FOUND,
"ReplicationConfigurationNotFoundError",
"The replication configuration was not found",
vec![("BucketName".to_string(), bucket.to_string())],
)),
}
}
pub(crate) fn delete_bucket_replication(
&self,
account_id: &str,
bucket: &str,
) -> Result<AwsResponse, AwsServiceError> {
let mut accts = self.state.write();
let state = accts.get_or_create(account_id);
let b = state
.buckets
.get_mut(bucket)
.ok_or_else(|| no_such_bucket(bucket))?;
b.replication_config = None;
self.store
.delete_bucket_subresource(bucket, BucketSubresource::Replication)
.map_err(crate::service::persistence_error)?;
Ok(empty_response(StatusCode::NO_CONTENT))
}
}