1515
1616package com .spectralogic .ds3client .integration ;
1717
18+ import com .google .common .collect .ImmutableList ;
1819import com .spectralogic .ds3client .Ds3Client ;
1920import com .spectralogic .ds3client .Ds3ClientBuilder ;
20- import com .spectralogic .ds3client .commands .DeleteBucketRequest ;
21- import com .spectralogic .ds3client .commands .DeleteObjectRequest ;
22- import com .spectralogic .ds3client .commands .spectrads3 .GetSystemInformationSpectraS3Request ;
21+ import com .spectralogic .ds3client .commands .spectrads3 .*;
2322import com .spectralogic .ds3client .helpers .DeleteBucket ;
2423import com .spectralogic .ds3client .helpers .Ds3ClientHelpers ;
2524import com .spectralogic .ds3client .helpers .channelbuilders .PrefixAdderObjectChannelBuilder ;
26- import com .spectralogic .ds3client .models .Contents ;
25+ import com .spectralogic .ds3client .models .Bucket ;
26+ import com .spectralogic .ds3client .models .Job ;
27+ import com .spectralogic .ds3client .models .JobStatus ;
2728import com .spectralogic .ds3client .models .bulk .Ds3Object ;
2829import com .spectralogic .ds3client .utils .ResourceUtils ;
2930import org .slf4j .Logger ;
3031import org .slf4j .LoggerFactory ;
3132
3233import java .io .IOException ;
3334import java .net .URISyntaxException ;
35+ import java .nio .channels .FileChannel ;
3436import java .nio .file .Files ;
3537import java .nio .file .Path ;
38+ import java .nio .file .StandardOpenOption ;
3639import java .util .ArrayList ;
3740import java .util .List ;
3841
@@ -60,6 +63,22 @@ public static Ds3Client insecureFromEnv() {
6063 return builder .build ();
6164 }
6265
66+ /**
67+ * Goes through all buckets on a BP and attempts to delete them and all their contents. It cancels all active jobs
68+ * associated with this bucket. Created for cleaning up after cascade failures in functional tests.
69+ *
70+ * USE ONLY WHEN DELETING ALL BUCKETS AND ALL CONTENT IS YOUR GOAL.
71+ */
72+ public static void forceCleanupBucketsOnBP (final Ds3Client client ) throws IOException {
73+ final GetBucketsSpectraS3Request request = new GetBucketsSpectraS3Request ();
74+ final GetBucketsSpectraS3Response response = client .getBucketsSpectraS3 (request );
75+
76+ for (final Bucket bucket : response .getBucketListResult ().getBuckets ()) {
77+ cancelAllJobsForBucket (client , bucket .getName ());
78+ client .deleteBucketSpectraS3 (new DeleteBucketSpectraS3Request (bucket .getName ()).withForce (true ));
79+ }
80+ }
81+
6382 public static void assumeVersion1_2 (final Ds3Client client ) throws IOException {
6483 final int majorVersion = Integer .parseInt (client .getSystemInformationSpectraS3 (
6584 new GetSystemInformationSpectraS3Request ()).getSystemInformationResult ().getBuildInformation ().getVersion ().split ("\\ ." )[0 ]);
@@ -79,6 +98,19 @@ public static void loadBookTestData(final Ds3Client client, final String bucketN
7998 LOG .info ("Finished loading test data..." );
8099 }
81100
101+ /**
102+ * Loads a single test book to a BP with the specified object name.
103+ */
104+ public static void loadTestBook (final Ds3Client client , final String fileName , final String objectName , final String bucketName ) throws IOException , URISyntaxException {
105+ final Path filePath = ResourceUtils .loadFileResource (RESOURCE_BASE_NAME + fileName );
106+ final Ds3Object obj = new Ds3Object (objectName , Files .size (filePath ));
107+
108+ final Ds3ClientHelpers helpers = Ds3ClientHelpers .wrap (client );
109+
110+ final Ds3ClientHelpers .Job job = helpers .startWriteJob (bucketName , ImmutableList .of (obj ));
111+ job .transfer (key -> FileChannel .open (filePath , StandardOpenOption .READ ));
112+ }
113+
82114 public static Ds3ClientHelpers .Job getLoadJob (final Ds3Client client , final String bucketName , final String resourceBaseName ) throws IOException , URISyntaxException {
83115 final Ds3ClientHelpers helpers = Ds3ClientHelpers .wrap (client );
84116
@@ -118,4 +150,19 @@ public static void deleteBucketContents(final Ds3Client client, final String buc
118150 final Ds3ClientHelpers helpers = Ds3ClientHelpers .wrap (client );
119151 DeleteBucket .INSTANCE .deleteBucketContents (helpers , bucketName );
120152 }
153+
154+ // Cancels all in-progress jobs associated with a bucket
155+ public static void cancelAllJobsForBucket (final Ds3Client client , final String bucketName ) {
156+ try {
157+ final GetJobsSpectraS3Response getJobs = client .getJobsSpectraS3 (new GetJobsSpectraS3Request ().withBucketId (bucketName ));
158+
159+ for (final Job job : getJobs .getJobListResult ().getJobs ()) {
160+ if (job .getStatus () == JobStatus .IN_PROGRESS ) {
161+ client .cancelJobSpectraS3 (new CancelJobSpectraS3Request (job .getJobId ()));
162+ }
163+ }
164+ } catch (final IOException e ) {
165+ LOG .debug ("Could not cancel jobs for bucket '%s': %s" , bucketName , e .getMessage ());
166+ }
167+ }
121168}
0 commit comments