Skip to content

Commit

Permalink
add builder to get bucket version config
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhaashish committed May 28, 2020
1 parent 73ad509 commit 479614e
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 61 deletions.
27 changes: 27 additions & 0 deletions api/src/main/java/io/minio/IsVersioningEnabledArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 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;

/** Argument class of MinioClient.isVersioningEnabled(). */
public class IsVersioningEnabledArgs extends BucketArgs {
public static Builder builder() {
return new Builder();
}

/** Argument builder of {@link IsVersioningEnabledArgs}. */
public static final class Builder extends BucketArgs.Builder<Builder, IsVersioningEnabledArgs> {}
}
57 changes: 51 additions & 6 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import io.minio.messages.SseConfiguration;
import io.minio.messages.Tags;
import io.minio.messages.Upload;
import io.minio.messages.VersioningConfiguration;
import io.minio.org.apache.commons.validator.routines.InetAddressValidator;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
Expand Down Expand Up @@ -3648,9 +3649,8 @@ public void enableVersioning(EnableVersioningArgs args)

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("versioning", "");
String config =
"<VersioningConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">"
+ "<Status>Enabled</Status></VersioningConfiguration>";
VersioningConfiguration config = new VersioningConfiguration("Enabled");

Response response = executePut(args.bucket(), null, null, queryParamMap, config, 0);
response.body().close();
}
Expand Down Expand Up @@ -3712,13 +3712,58 @@ public void disableVersioning(DisableVersioningArgs args)

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("versioning", "");
String config =
"<VersioningConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">"
+ "<Status>Suspended</Status></VersioningConfiguration>";
VersioningConfiguration config = new VersioningConfiguration("Suspended");
Response response = executePut(args.bucket(), null, null, queryParamMap, config, 0);
response.body().close();
}

/**
* Returns true if versioning is enabled on the bucket.
*
* <pre>Example:{@code
* boolean isVersioningEnabled =
* minioClient.isVersioningEnabled(
* IsVersioningEnabledArgs.builder().bucket("my-bucketname").build());
* if (isVersioningEnabled) {
* System.out.println("Bucket versioning is enabled");
* } else {
* System.out.println("Bucket versioning is disabled");
* }
* }</pre>
*
* @param args {@link IsVersioningEnabledArgs} object.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
* @throws InternalException thrown to indicate internal library error.
* @throws InvalidBucketNameException thrown to indicate invalid bucket name passed.
* @throws InvalidKeyException thrown to indicate missing of HMAC SHA-256 library.
* @throws InvalidResponseException thrown to indicate S3 service returned invalid or no error
* response.
* @throws IOException thrown to indicate I/O error on S3 operation.
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
public boolean isVersioningEnabled(IsVersioningEnabledArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
checkArgs(args);

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("versioning", "");
try (Response response = executeGet(args.bucket(), null, null, queryParamMap)) {
VersioningConfiguration result =
Xml.unmarshal(VersioningConfiguration.class, response.body().charStream());
return result.status();
} catch (ErrorResponseException e) {
if (e.errorResponse().errorCode() != ErrorCode.ILLEGAL_VERSIONING_CONFIGURATION_EXCEPTION) {
throw e;
}
}
return false;
}

/**
* Sets default object retention in a bucket.
*
Expand Down
47 changes: 47 additions & 0 deletions api/src/main/java/io/minio/messages/VersioningConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 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.messages;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;

/**
* Object representation of request XML of <a
* href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketVersioning.html">PutBucketVersioning
* API</a> and response XML of <a
* href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketVersioning.html">GetBucketVersioning
* API</a>.
*/
@Root(name = "VersioningConfiguration", strict = false)
@Namespace(reference = "http://s3.amazonaws.com/doc/2006-03-01/")
public class VersioningConfiguration {
@Element(name = "Status", required = false)
private String status;

public VersioningConfiguration() {}

/** Constructs a new VersioningConfiguration object with given status. */
public VersioningConfiguration(String status) {
this.status = status;
}

/** Indicates whether the bucket is version enabled or not. */
public boolean status() {
return status != null && status.equals("Enabled");
}
}
60 changes: 43 additions & 17 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@ MinioClient s3Client = new MinioClient("https://s3.amazonaws.com",
| [`deleteBucketLifeCycle`](#deleteBucketLifeCycle) | [`deleteObjectTags`](#deleteObjectTags) |
| [`deleteBucketTags`](#deleteBucketTags) | [`disableObjectLegalHold`](#disableObjectLegalHold) |
| [`disableVersioning`](#disableVersioning) | [`enableObjectLegalHold`](#enableObjectLegalHold) |
| [`enableVersioning`](#enableVersioning) | [`getObject`](#getObject) |
| [`getBucketEncryption`](#getBucketEncryption) | [`getObjectRetention`](#getObjectRetention) |
| [`getBucketLifeCycle`](#getBucketLifeCycle) | [`getObjectTags`](#getObjectTags) |
| [`getBucketNotification`](#getBucketNotification) | [`getObjectUrl`](#getObjectUrl) |
| [`getBucketPolicy`](#getBucketPolicy) | [`getPresignedObjectUrl`](#getPresignedObjectUrl) |
| [`getBucketTags`](#getBucketTags) | [`isObjectLegalHoldEnabled`](#isObjectLegalHoldEnabled) |
| [`getDefaultRetention`](#getDefaultRetention) | [`listObjects`](#listObjects) |
| [`listBuckets`](#listBuckets) | [`presignedGetObject`](#presignedGetObject) |
| [`listenBucketNotification`](#listenBucketNotification) | [`presignedPostPolicy`](#presignedPostPolicy) |
| [`listIncompleteUploads`](#listIncompleteUploads) | [`presignedPutObject`](#presignedPutObject) |
| [`makeBucket`](#makeBucket) | [`putObject`](#putObject) |
| [`removeAllBucketNotification`](#removeAllBucketNotification) | [`removeObject`](#removeObject) |
| [`removeBucket`](#removeBucket) | [`removeObjects`](#removeObjects) |
| [`removeIncompleteUpload`](#removeIncompleteUpload) | [`selectObjectContent`](#selectObjectContent) |
| [`setBucketEncryption`](#setBucketEncryption) | [`setObjectRetention`](#setObjectRetention) |
| [`setBucketLifeCycle`](#setBucketLifeCycle) | [`setObjectTags`](#setObjectTags) |
| [`setBucketNotification`](#setBucketNotification) | [`statObject`](#statObject) |
| [`isVersioningEnabled`](#isVersioningEnabled) | [`getObject`](#getObject) |
| [`enableVersioning`](#enableVersioning) | [`getObjectRetention`](#getObjectRetention) |
| [`getBucketEncryption`](#getBucketEncryption) | [`getObjectTags`](#getObjectTags) |
| [`getBucketLifeCycle`](#getBucketLifeCycle) | [`getObjectUrl`](#getObjectUrl) |
| [`getBucketNotification`](#getBucketNotification) | [`getPresignedObjectUrl`](#getPresignedObjectUrl) |
| [`getBucketPolicy`](#getBucketPolicy) | [`isObjectLegalHoldEnabled`](#isObjectLegalHoldEnabled) |
| [`getBucketTags`](#getBucketTags) | [`listObjects`](#listObjects) |
| [`getDefaultRetention`](#getDefaultRetention) | [`presignedGetObject`](#presignedGetObject) |
| [`listBuckets`](#listBuckets) | [`presignedPostPolicy`](#presignedPostPolicy) |
| [`listenBucketNotification`](#listenBucketNotification) | [`presignedPutObject`](#presignedPutObject) |
| [`listIncompleteUploads`](#listIncompleteUploads) | [`putObject`](#putObject) |
| [`makeBucket`](#makeBucket) | [`removeObject`](#removeObject) |
| [`removeAllBucketNotification`](#removeAllBucketNotification) | [`removeObjects`](#removeObjects) |
| [`removeBucket`](#removeBucket) | [`selectObjectContent`](#selectObjectContent) |
| [`removeIncompleteUpload`](#removeIncompleteUpload) | [`setObjectRetention`](#setObjectRetention) |
| [`setBucketEncryption`](#setBucketEncryption) | [`setObjectTags`](#setObjectTags) |
| [`setBucketLifeCycle`](#setBucketLifeCycle) | [`statObject`](#statObject) |
| [`setBucketNotification`](#setBucketNotification) | |
| [`setBucketPolicy`](#setBucketPolicy) | |
| [`setBucketTags`](#setBucketTags) | |
| [`setDefaultRetention`](#setDefaultRetention) | |
Expand Down Expand Up @@ -371,6 +372,30 @@ __Example__
minioClient.enableVersioning(EnableVersioningArgs.builder().bucket("my-bucket").build());
```

<a name="isVersioningEnabled"></a>
### isVersioningEnabled(IsVersioningEnabledArgs args)
`public void isVersioningEnabled(IsVersioningEnabledArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#isVersioningEnabled-io.minio.IsVersioningEnabledArgs-)_

Get bucket version status.

__Parameters__

| Parameter | Type | Description |
|:-----------|:-----------------------------|:-----------------|
| ``args`` | _[IsVersioningEnabledArgs]_ | Arguments. |

__Example__
```java
boolean isVersioningEnabled =
minioClient.isVersioningEnabled(
IsVersioningEnabledArgs.builder().bucket("my-bucketname").build());
if (isVersioningEnabled) {
System.out.println("Bucket versioning is enabled");
} else {
System.out.println("Bucket versioning is disabled");
}
```

<a name="getBucketEncryption"></a>
### getBucketEncryption(GetBucketEncryptionArgs args)
`public SseConfiguration getBucketEncryption(GetBucketEncryptionArgs args)` _[[Javadoc]](http://minio.github.io/minio-java/io/minio/MinioClient.html#getBucketEncryption-io.minio.GetBucketEncryptionArgs-)_
Expand Down Expand Up @@ -1771,3 +1796,4 @@ ObjectStat objectStat =
[DeleteBucketLifeCycleArgs]: http://minio.github.io/minio-java/io/minio/DeleteBucketLifeCycleArgs.html
[GetBucketLifeCycleArgs]: http://minio.github.io/minio-java/io/minio/GetBucketLifeCycleArgs.html
[SetBucketLifeCycleArgs]: http://minio.github.io/minio-java/io/minio/SetBucketLifeCycleArgs.html
[IsVersioningEnabledArgs]: http://minio.github.io/minio-java/io/minio/IsVersioningEnabledArgs.html
56 changes: 18 additions & 38 deletions functional/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.minio.GetBucketTagsArgs;
import io.minio.GetObjectRetentionArgs;
import io.minio.GetObjectTagsArgs;
import io.minio.IsVersioningEnabledArgs;
import io.minio.ListObjectsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
Expand Down Expand Up @@ -426,73 +427,52 @@ public static void makeBucket_test4() throws Exception {

/** Test: enableVersioning(EnableVersioningArgs args). */
public static void enableVersioning_test() throws Exception {
String methodName = "enableVersioning(EnableVersioningArgs args yahoooooo)";
if (!mintEnv) {
System.out.println("Test: enableVersioning(EnableVersioningArgs args)");
System.out.println("Test: " + methodName);
}

long startTime = System.currentTimeMillis();
try {
String name = getRandomName();
client.makeBucket(MakeBucketArgs.builder().bucket(name).build());
client.enableVersioning(EnableVersioningArgs.builder().bucket(name).build());
if (!client.isVersioningEnabled(IsVersioningEnabledArgs.builder().bucket(name).build())) {
throw new Exception("[FAILED] isVersioningEnabled(): expected: true, got: false");
}
client.removeBucket(RemoveBucketArgs.builder().bucket(name).build());
mintSuccessLog("enableVersioning(EnableVersioningArgs args)", null, startTime);
mintSuccessLog(methodName, null, startTime);
} catch (Exception e) {
ErrorResponse errorResponse = null;
if (e instanceof ErrorResponseException) {
ErrorResponseException exp = (ErrorResponseException) e;
errorResponse = exp.errorResponse();
}
// Ignore NotImplemented error
if (errorResponse != null && errorResponse.errorCode() == ErrorCode.NOT_IMPLEMENTED) {
mintIgnoredLog("enableVersioning(EnableVersioningArgs args)", null, startTime);
} else {
mintFailedLog(
"enableVersioning(EnableVersioningArgs args)",
null,
startTime,
null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
handleException(methodName, null, startTime, e);
}
}

/** Test: disableVersioning(DisableVersioningArgs args). */
public static void disableVersioning_test() throws Exception {
String methodName = "disableVersioning(DisableVersioningArgs args)";
if (!mintEnv) {
System.out.println("Test: disableVersioning(DisableVersioningArgs args)");
System.out.println("Test: " + methodName);
}

long startTime = System.currentTimeMillis();
try {
String name = getRandomName();
client.makeBucket(MakeBucketArgs.builder().bucket(name).build());
client.disableVersioning(DisableVersioningArgs.builder().bucket(name).build());
if (client.isVersioningEnabled(IsVersioningEnabledArgs.builder().bucket(name).build())) {
throw new Exception("[FAILED] isVersioningEnabled(): expected: false, got: true");
}

client.enableVersioning(EnableVersioningArgs.builder().bucket(name).build());
client.disableVersioning(DisableVersioningArgs.builder().bucket(name).build());
if (client.isVersioningEnabled(IsVersioningEnabledArgs.builder().bucket(name).build())) {
throw new Exception("[FAILED] isVersioningEnabled(): expected: false, got: true");
}

client.removeBucket(RemoveBucketArgs.builder().bucket(name).build());
mintSuccessLog("disableVersioning(DisableVersioningArgs args)", null, startTime);
mintSuccessLog(methodName, null, startTime);
} catch (Exception e) {
ErrorResponse errorResponse = null;
if (e instanceof ErrorResponseException) {
ErrorResponseException exp = (ErrorResponseException) e;
errorResponse = exp.errorResponse();
}
// Ignore NotImplemented error
if (errorResponse != null && errorResponse.errorCode() == ErrorCode.NOT_IMPLEMENTED) {
mintIgnoredLog("disableVersioning(DisableVersioningArgs args)", null, startTime);
} else {
mintFailedLog(
"disableVersioning(DisableVersioningArgs args)",
null,
startTime,
null,
e.toString() + " >>> " + Arrays.toString(e.getStackTrace()));
throw e;
}
handleException(methodName, null, startTime, e);
}
}

Expand Down

0 comments on commit 479614e

Please sign in to comment.