Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit e800c6f

Browse files
committed
Merge Soft Delete feature
2 parents aff397f + e8c9edb commit e800c6f

20 files changed

+975
-36
lines changed

ChangeLog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
XXXX.XX.xx Version X.X.X
22
* Support for 2017-07-29 REST version. Please see our REST api documentation and blogs for information about the related added features.
3+
* Added support for soft delete feature. If a delete retention policy is enabled through the set service properties API, then blobs or snapshots can be deleted softly and retained for a specified number of days, before being permanently removed by garbage collection.
34

45
2017.11.01 Version 6.1.0
56
* Added support for the last time the tier was modified.

microsoft-azure-storage-test/src/com/microsoft/azure/storage/ServicePropertiesTests.java

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,154 @@ private void testAnalyticsRetentionPolicies(ServiceClient client, ServicePropert
397397
assertServicePropertiesAreEqual(props, callDownloadServiceProperties(client));
398398
}
399399

400+
/**
401+
* Test delete retention policy for blobs
402+
*
403+
* @throws StorageException
404+
* @throws InterruptedException
405+
*/
406+
@Test
407+
public void testValidDeleteRetentionPolicy() throws StorageException, InterruptedException {
408+
ServiceClient client = TestHelper.createCloudBlobClient();
409+
410+
// average setting
411+
testValidDeleteRetentionPolicy(client, true, 5);
412+
413+
// minimum setting
414+
testValidDeleteRetentionPolicy(client, true, 1);
415+
416+
// maximum setting
417+
testValidDeleteRetentionPolicy(client, true, 365);
418+
419+
// disable setting
420+
testValidDeleteRetentionPolicy(client, false, 5);
421+
}
422+
423+
private void testValidDeleteRetentionPolicy(ServiceClient client, boolean enabled,
424+
Integer interval)
425+
throws StorageException, InterruptedException {
426+
427+
try {
428+
ServiceProperties expectedServiceProperties = new ServiceProperties();
429+
expectedServiceProperties.setDefaultServiceVersion(Constants.HeaderConstants.TARGET_STORAGE_VERSION);
430+
431+
if (enabled) {
432+
expectedServiceProperties.getDeleteRetentionPolicy().setEnabled(true);
433+
expectedServiceProperties.getDeleteRetentionPolicy().setRetentionIntervalInDays(interval);
434+
callUploadServiceProps(client, expectedServiceProperties, null);
435+
} else {
436+
// interval and retained versions per blob would both be ignored by the service in case the policy is not enabled
437+
ServiceProperties propertiesToUpload = new ServiceProperties();
438+
propertiesToUpload.getDeleteRetentionPolicy().setEnabled(false);
439+
propertiesToUpload.getDeleteRetentionPolicy().setRetentionIntervalInDays(interval);
440+
441+
expectedServiceProperties.getDeleteRetentionPolicy().setEnabled(false);
442+
callUploadServiceProps(client, propertiesToUpload, null);
443+
}
444+
445+
// verify
446+
assertServicePropertiesAreEqual(expectedServiceProperties, callDownloadServiceProperties(client));
447+
}
448+
finally {
449+
// reset the delete retention policy
450+
ServiceProperties disabledDeleteRetentionPolicy = new ServiceProperties();
451+
disabledDeleteRetentionPolicy.getDeleteRetentionPolicy().setEnabled(false);
452+
callUploadServiceProps(client, disabledDeleteRetentionPolicy, null);
453+
}
454+
}
455+
456+
/**
457+
* Test invalid delete retention policy for blobs
458+
*
459+
* @throws StorageException
460+
* @throws InterruptedException
461+
*/
462+
@Test
463+
public void testInvalidDeleteRetentionPolicy() throws StorageException, InterruptedException {
464+
ServiceClient client = TestHelper.createCloudBlobClient();
465+
466+
// Should not work with 0 days
467+
testInvalidDeleteRetentionPolicy(client, true, 0);
468+
469+
// Should not work with <0 days
470+
testInvalidDeleteRetentionPolicy(client, true, -1);
471+
472+
// Should not work with 366 days
473+
testInvalidDeleteRetentionPolicy(client, true, 366);
474+
475+
476+
// Should not work with interval as null
477+
testInvalidDeleteRetentionPolicy(client, true, null);
478+
}
479+
480+
private void testInvalidDeleteRetentionPolicy(ServiceClient client, boolean enabled,
481+
Integer interval)
482+
throws StorageException, InterruptedException {
483+
484+
// Arrange
485+
ServiceProperties serviceProperties = new ServiceProperties();
486+
serviceProperties.setDefaultServiceVersion(Constants.HeaderConstants.TARGET_STORAGE_VERSION);
487+
488+
if (enabled) {
489+
serviceProperties.getDeleteRetentionPolicy().setEnabled(true);
490+
}
491+
else {
492+
serviceProperties.getDeleteRetentionPolicy().setEnabled(false);
493+
}
494+
serviceProperties.getDeleteRetentionPolicy().setRetentionIntervalInDays(interval);
495+
496+
// Failure is expected since the retention policy is invalid
497+
try {
498+
callUploadServiceProps(client, serviceProperties, null);
499+
fail("No exception received. An invalid delete retention policy should have raised an exception.");
500+
}
501+
catch (StorageException e) {
502+
assertEquals(e.errorCode, "InvalidXmlDocument");
503+
}
504+
catch (IllegalArgumentException e) {
505+
assertTrue(e.getMessage().contains("argument must not be null"));
506+
}
507+
catch (Exception e) {
508+
fail("Invalid exception " + e.getClass() + " received when expecting StorageException");
509+
}
510+
}
511+
512+
/**
513+
* Test empty delete retention policy for blobs
514+
*
515+
* @throws StorageException
516+
* @throws InterruptedException
517+
*/
518+
@Test
519+
public void testEmptyDeleteRetentionPolicy() throws StorageException, InterruptedException {
520+
ServiceClient client = TestHelper.createCloudBlobClient();
521+
522+
try {
523+
// set up initial delete retention policy
524+
ServiceProperties currentServiceProperties = new ServiceProperties();
525+
currentServiceProperties.setDefaultServiceVersion(Constants.HeaderConstants.TARGET_STORAGE_VERSION);
526+
currentServiceProperties.getDeleteRetentionPolicy().setEnabled(true);
527+
currentServiceProperties.getDeleteRetentionPolicy().setRetentionIntervalInDays(5);
528+
callUploadServiceProps(client, currentServiceProperties, null);
529+
530+
// verify
531+
assertServicePropertiesAreEqual(currentServiceProperties, callDownloadServiceProperties(client));
532+
533+
// try to upload empty retention policy
534+
ServiceProperties emptyServiceProperties = new ServiceProperties();
535+
callUploadServiceProps(client, emptyServiceProperties, null);
536+
537+
// verify
538+
assertServicePropertiesAreEqual(currentServiceProperties, callDownloadServiceProperties(client));
539+
}
540+
finally {
541+
// reset the delete retention policy
542+
ServiceProperties disabledDeleteRetentionPolicy = new ServiceProperties();
543+
disabledDeleteRetentionPolicy.getDeleteRetentionPolicy().setEnabled(false);
544+
callUploadServiceProps(client, disabledDeleteRetentionPolicy, null);
545+
}
546+
}
547+
400548
/**
401549
* Test CORS with different rules.
402550
*
@@ -974,6 +1122,15 @@ private static void assertServicePropertiesAreEqual(ServiceProperties propsA, Se
9741122
assertNull(propsA.getCors());
9751123
assertNull(propsB.getCors());
9761124
}
1125+
1126+
if (propsA.getDeleteRetentionPolicy() != null && propsB.getDeleteRetentionPolicy() != null) {
1127+
assertEquals(propsA.getDeleteRetentionPolicy().getEnabled(), propsB.getDeleteRetentionPolicy().getEnabled());
1128+
assertEquals(propsA.getDeleteRetentionPolicy().getRetentionIntervalInDays(), propsB.getDeleteRetentionPolicy().getRetentionIntervalInDays());
1129+
}
1130+
else {
1131+
assertNull(propsA.getDeleteRetentionPolicy());
1132+
assertNull(propsB.getDeleteRetentionPolicy());
1133+
}
9771134
}
9781135

9791136
/**

microsoft-azure-storage-test/src/com/microsoft/azure/storage/blob/BlobTestHelper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.microsoft.azure.storage.StorageCredentials;
1919
import com.microsoft.azure.storage.StorageException;
2020
import com.microsoft.azure.storage.TestHelper;
21+
import com.microsoft.azure.storage.ServiceProperties;
2122

2223
import java.io.ByteArrayInputStream;
2324
import java.io.IOException;
@@ -436,4 +437,23 @@ public static void assertAreEqual(CopyState copy1, CopyState copy2) {
436437
assertEquals(copy1.getTotalBytes(), copy2.getTotalBytes());
437438
}
438439
}
440+
441+
public static void enableSoftDelete() throws StorageException, URISyntaxException, InterruptedException {
442+
ServiceProperties serviceProperties = new ServiceProperties();
443+
serviceProperties.getDeleteRetentionPolicy().setEnabled(true);
444+
serviceProperties.getDeleteRetentionPolicy().setRetentionIntervalInDays(3);
445+
446+
CloudBlobClient bClient = TestHelper.createCloudBlobClient();
447+
bClient.uploadServiceProperties(serviceProperties);
448+
Thread.sleep(30000);
449+
}
450+
451+
public static void disableSoftDelete() throws StorageException, URISyntaxException, InterruptedException {
452+
ServiceProperties serviceProperties = new ServiceProperties();
453+
serviceProperties.getDeleteRetentionPolicy().setEnabled(false);
454+
455+
CloudBlobClient bClient = TestHelper.createCloudBlobClient();
456+
bClient.uploadServiceProperties(serviceProperties);
457+
Thread.sleep(30000);
458+
}
439459
}

0 commit comments

Comments
 (0)