Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add command quota set/get bucket quota #1337

Merged
merged 5 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion adminapi/src/main/java/io/minio/admin/MinioAdminClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ private enum Command {
ADD_CANNED_POLICY("add-canned-policy"),
SET_USER_OR_GROUP_POLICY("set-user-or-group-policy"),
LIST_CANNED_POLICIES("list-canned-policies"),
REMOVE_CANNED_POLICY("remove-canned-policy");
REMOVE_CANNED_POLICY("remove-canned-policy"),
SET_BUCKET_QUOTA("set-bucket-quota"),
GET_BUCKET_QUOTA("get-bucket-quota");
private final String value;

private Command(String value) {
Expand Down Expand Up @@ -291,6 +293,74 @@ public void deleteUser(@Nonnull String accessKey)
null)) {}
}

/**
* set bucket quota size
* @param bucketName bucketName
* @param size the capacity of the bucket
* @param unit the quota unit of the size argument
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws IOException thrown to indicate I/O error on MinIO REST operation.
*/
public void setBucketQuota(@Nonnull String bucketName, long size, @Nonnull QuotaUnit unit)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
Map<String, Object> quotaEntity = new HashMap<>(4);
harshavardhana marked this conversation as resolved.
Show resolved Hide resolved
if (size > 0) {
quotaEntity.put("quotatype", "hard");
}
quotaEntity.put("quota", unit.toBytes(size));
try (Response response =
execute(
Method.PUT,
Command.SET_BUCKET_QUOTA,
ImmutableMultimap.of("bucket", bucketName),
OBJECT_MAPPER.writeValueAsBytes(quotaEntity))) {
}
}

/**
* get bucket quota size
* @param bucketName bucketName
* @return bytes of bucket
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public long getBucketQuota(String bucketName)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try (Response response =
execute(
Method.GET,
Command.GET_BUCKET_QUOTA,
ImmutableMultimap.of("bucket", bucketName),
null)) {
MapType mapType =
OBJECT_MAPPER
.getTypeFactory()
.constructMapType(HashMap.class, String.class, JsonNode.class);
return OBJECT_MAPPER
.<Map<String, JsonNode>>readValue(response.body().bytes(), mapType)
.entrySet()
.stream()
.filter(entry -> "quota".equals(entry.getKey()))
.findFirst()
.map(entry -> Long.valueOf(entry.getValue().toString()))
.orElseThrow(() -> new IllegalArgumentException("found not quota"));
}
}

/**
* Reset bucket quota
* @param bucketName bucketName
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws IOException thrown to indicate I/O error on MinIO REST operation.
*/
public void clearBucketQuota(@Nonnull String bucketName)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
setBucketQuota(bucketName, 0, QuotaUnit.KB);
}

/**
* Creates a policy.
*
Expand Down
44 changes: 44 additions & 0 deletions adminapi/src/main/java/io/minio/admin/QuotaUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.admin;
butterflyzh marked this conversation as resolved.
Show resolved Hide resolved

/** Bucket Quota QuotaUnit. */
public enum QuotaUnit {

KB(1024),
MB(1024 * KB.unit),
GB(1024 * MB.unit),
TB(1024 * GB.unit);

private final long unit;

QuotaUnit(long unit) {
this.unit = unit;
}

public long toBytes(long size) {
long totalSize = size * this.unit;
if (totalSize < 0) {
throw new IllegalArgumentException("Quota size must be greater than zero.But actual is " + totalSize);
}
if (totalSize / this.unit != size) {
throw new IllegalArgumentException("Quota size overflow");
}
return totalSize;
}
}
32 changes: 32 additions & 0 deletions adminapi/src/test/java/io/minio/admin/QuotaUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.admin;
butterflyzh marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.Test;

public class QuotaUnitTest {
@Test(expected = IllegalArgumentException.class)
public void testBytesOverflow() {
QuotaUnit.GB.toBytes(Long.MAX_VALUE);
}

@Test(expected = IllegalArgumentException.class)
public void testBytesLessThanZero() {
QuotaUnit.KB.toBytes(-1);
}
}