diff --git a/src/minio.rs b/src/minio.rs index 7bea047..5516352 100644 --- a/src/minio.rs +++ b/src/minio.rs @@ -79,6 +79,11 @@ struct MinioAnonymousAccess { pub permission: String, } +#[derive(Debug, Clone, Deserialize)] +struct MinioQuota { + pub quota: Option, +} + impl BasicMinioResult { pub fn success(&self) -> bool { self.status == "success" @@ -215,23 +220,7 @@ impl MinioService { .await?; // Set quota, if requested - if let Some(quota) = &b.quota { - let quota = format!("{}MB", quota); - - let res = self - .exec_mc_cmd::(&[ - "quota", - "set", - bucket_name.as_str(), - "--size", - quota.as_str(), - ]) - .await?; - - if res.get(0).map(|r| r.success()) != Some(true) { - return Err(MinioError::SetQuotaFailed.into()); - } - } + self.bucket_set_quota(&b.name, b.quota).await?; // Set retention, if requested if let Some(retention) = &b.retention { @@ -329,6 +318,41 @@ impl MinioService { .permission == "download") } + + /// Set bucket quota, in bytes + pub async fn bucket_set_quota(&self, bucket: &str, quota: Option) -> anyhow::Result<()> { + let bucket_name = self.absolute_bucket_name(bucket); + + let res = if let Some(quota) = "a { + let quota = format!("{}B", quota); + self.exec_mc_cmd::(&[ + "quota", + "set", + bucket_name.as_str(), + "--size", + quota.as_str(), + ]) + .await? + } else { + self.exec_mc_cmd::(&["quota", "clear", bucket_name.as_str()]) + .await? + }; + + if res.get(0).map(|r| r.success()) != Some(true) { + return Err(MinioError::SetQuotaFailed.into()); + } + Ok(()) + } + + /// Get current bucket quota, in bytes + pub async fn bucket_get_quota(&self, bucket_name: &str) -> anyhow::Result> { + let bucket_name = self.absolute_bucket_name(bucket_name); + Ok(self + .exec_mc_cmd::(&["quota", "info", bucket_name.as_str()]) + .await? + .remove(0) + .quota) + } } #[cfg(test)] @@ -492,7 +516,6 @@ mod test { ); } - // With versioning #[tokio::test] async fn bucket_with_versioning() { let _ = env_logger::builder().is_test(true).try_init(); @@ -590,10 +613,78 @@ mod test { .unwrap()); } - // TODO : with quota - // TODO : without quota - // TODO : with lock - // TODO : without lock + #[tokio::test] + async fn bucket_without_qutoa() { + let _ = env_logger::builder().is_test(true).try_init(); + + let srv = MinioTestServer::start().await.unwrap(); + let service = srv.as_service(); + service + .create_bucket(&MinioBucketSpec { + instance: "".to_string(), + name: TEST_BUCKET_NAME.to_string(), + secret: "".to_string(), + anonymous_read_access: false, + versioning: false, + quota: None, + lock: false, + retention: None, + }) + .await + .unwrap(); + + assert!(service.bucket_exists(TEST_BUCKET_NAME).await.unwrap()); + assert_eq!( + service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(), + None + ); + + service + .bucket_set_quota(TEST_BUCKET_NAME, Some(5122600)) + .await + .unwrap(); + assert_eq!( + service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(), + Some(5122600) + ); + + service + .bucket_set_quota(TEST_BUCKET_NAME, None) + .await + .unwrap(); + assert_eq!( + service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(), + None + ); + } + + #[tokio::test] + async fn bucket_with_qutoa() { + let _ = env_logger::builder().is_test(true).try_init(); + + let srv = MinioTestServer::start().await.unwrap(); + let service = srv.as_service(); + service + .create_bucket(&MinioBucketSpec { + instance: "".to_string(), + name: TEST_BUCKET_NAME.to_string(), + secret: "".to_string(), + anonymous_read_access: false, + versioning: false, + quota: Some(42300), + lock: false, + retention: None, + }) + .await + .unwrap(); + + assert!(service.bucket_exists(TEST_BUCKET_NAME).await.unwrap()); + assert_eq!( + service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(), + Some(42300) + ); + } + // TODO : with retention // TODO : without retention } diff --git a/yaml/minio-bucket.yaml b/yaml/minio-bucket.yaml index 8fa4a55..7a4795d 100644 --- a/yaml/minio-bucket.yaml +++ b/yaml/minio-bucket.yaml @@ -47,8 +47,8 @@ spec: default: false quota: type: integer - description: Limits the amount of data in the bucket, in Megabytes. By default it is unlimited - example: 100 + description: Limits the amount of data in the bucket, in bytes. By default it is unlimited + example: 1000000000 lock: description: Object locking prevent objects from being deleted. Will be considered as set to true when retention is defined. type: boolean