use of bio.terra.service.resourcemanagement.google.GoogleBucketRequest in project jade-data-repo by DataBiosphere.
the class DataLocationService method getOrCreateBucketForFile.
/**
* Fetch/create a project, then use that to fetch/create a bucket.
* The profileId is used to determine the project name.
* The flightId is used to lock the bucket metadata during possible creation.
* @param profileId
* @param flightId
* @return a reference to the bucket as a POJO GoogleBucketResource
* @throws CorruptMetadataException in two cases. 1) if the bucket already exists, but the metadata does not AND the
* application property allowReuseExistingBuckets=false. 2) if the metadata exists, but the bucket does not
*/
public GoogleBucketResource getOrCreateBucketForFile(String profileId, String flightId) throws InterruptedException {
// Every bucket needs to live in a project, so we get a project first (one will be created if it can't be found)
GoogleProjectResource projectResource = getProjectForFile(profileId);
BillingProfile profile = profileService.getProfileById(UUID.fromString(profileId));
GoogleBucketRequest googleBucketRequest = new GoogleBucketRequest().googleProjectResource(projectResource).bucketName(getBucketName(profileId)).profileId(UUID.fromString(profileId)).region(profile.getGcsRegion());
return resourceService.getOrCreateBucket(googleBucketRequest, flightId);
}
use of bio.terra.service.resourcemanagement.google.GoogleBucketRequest in project jade-data-repo by DataBiosphere.
the class BucketResourceTest method noBucketButMetadataExistsTest.
@Test
public // bucket_resource metadata row exists, but the corresponding bucket cloud resource does not
void noBucketButMetadataExistsTest() throws Exception {
logger.info("app property allowReuseExistingBuckets = " + resourceService.getAllowReuseExistingBuckets());
String bucketName = "testbucket_nobucketbutmetadataexiststest";
String flightIdA = "noBucketButMetadataExistsTestA";
bucketNames.add(bucketName);
// create the bucket and metadata
GoogleBucketRequest googleBucketRequest = buildBucketRequest(bucketName);
GoogleBucketResource bucketResource = resourceService.getOrCreateBucket(googleBucketRequest, flightIdA);
checkBucketExists(bucketResource.getResourceId());
// delete the bucket cloud resource only
Bucket bucket = storage.get(bucketName);
boolean bucketDeleted = bucket.delete();
assertTrue("bucket cloud resource deleted", bucketDeleted);
// try to fetch the bucket again, check fails with corrupt metadata exception
boolean caughtCorruptMetadataException = false;
try {
resourceService.getBucketResourceById(bucketResource.getResourceId(), true);
} catch (CorruptMetadataException cmEx) {
caughtCorruptMetadataException = true;
}
assertTrue("fetch failed when cloud resource does not exist", caughtCorruptMetadataException);
// try to getOrCreate bucket again, check fails with corrupt metadata exception
String flightIdB = "bucketExistsBeforeMetadataTestB";
caughtCorruptMetadataException = false;
try {
resourceService.getOrCreateBucket(googleBucketRequest, flightIdB);
} catch (CorruptMetadataException cmEx) {
caughtCorruptMetadataException = true;
}
assertTrue("getOrCreate failed when cloud resource does not exist", caughtCorruptMetadataException);
// update the metadata to match the cloud state, check that everything is deleted
resourceService.updateBucketMetadata(bucketName, null);
checkBucketDeleted(bucketResource.getName(), bucketResource.getResourceId());
}
use of bio.terra.service.resourcemanagement.google.GoogleBucketRequest in project jade-data-repo by DataBiosphere.
the class BucketResourceTest method bucketExistsBeforeMetadataTest.
@Test
public // bucket cloud resource exists, but the corresponding bucket_resource metadata row does not
void bucketExistsBeforeMetadataTest() throws Exception {
logger.info("app property allowReuseExistingBuckets = " + resourceService.getAllowReuseExistingBuckets());
String bucketName = "testbucket_bucketexistsbeforemetadatatest";
String flightIdA = "bucketExistsBeforeMetadataTestA";
bucketNames.add(bucketName);
// create the bucket and metadata
GoogleBucketRequest googleBucketRequest = buildBucketRequest(bucketName);
GoogleBucketResource bucketResource = resourceService.getOrCreateBucket(googleBucketRequest, flightIdA);
checkBucketExists(bucketResource.getResourceId());
// delete the metadata only
boolean rowDeleted = resourceDao.deleteBucketMetadata(bucketName, flightIdA);
assertTrue("metadata row deleted", rowDeleted);
// try to fetch the bucket again, check fails with not found exception
boolean caughtNotFoundException = false;
try {
resourceService.getBucketResourceById(bucketResource.getResourceId(), true);
} catch (GoogleResourceNotFoundException cmEx) {
caughtNotFoundException = true;
}
assertTrue("fetch failed when metadata does not exist", caughtNotFoundException);
// set application property allowReuseExistingBuckets=false
// try to create bucket again, check fails with corrupt metadata exception
resourceService.setAllowReuseExistingBuckets(false);
String flightIdB = "bucketExistsBeforeMetadataTestB";
boolean caughtCorruptMetadataException = false;
try {
resourceService.getOrCreateBucket(googleBucketRequest, flightIdB);
} catch (CorruptMetadataException cmEx) {
caughtCorruptMetadataException = true;
}
assertTrue("create failed when cloud resource already exists", caughtCorruptMetadataException);
// set application property allowReuseExistingBuckets=true
// try to create bucket again, check succeeds
resourceService.setAllowReuseExistingBuckets(true);
String flightIdC = "bucketExistsBeforeMetadataTestC";
bucketResource = resourceService.getOrCreateBucket(googleBucketRequest, flightIdC);
// check the bucket and metadata exist
checkBucketExists(bucketResource.getResourceId());
// delete the bucket and metadata
deleteBucket(bucketResource.getName());
checkBucketDeleted(bucketResource.getName(), bucketResource.getResourceId());
// restore original value of application property allowReuseExistingBuckets, which was saved in setup
// (this is also done in cleanup after all tests, in case this test errors out before reaching this line)
resourceService.setAllowReuseExistingBuckets(allowReuseExistingBuckets);
}
use of bio.terra.service.resourcemanagement.google.GoogleBucketRequest in project jade-data-repo by DataBiosphere.
the class BucketResourceTest method twoThreadsCompeteForLockTest.
@Test
public // after it's been created, confirm it succeeds.
void twoThreadsCompeteForLockTest() throws Exception {
String flightIdBase = "twoThreadsCompeteForLockTest";
String bucketName = "twothreadscompeteforlocktest";
bucketNames.add(bucketName);
GoogleBucketRequest bucketRequest = buildBucketRequest(bucketName);
BucketResourceLockTester resourceLockA = new BucketResourceLockTester(resourceService, bucketRequest, flightIdBase + "A");
BucketResourceLockTester resourceLockB = new BucketResourceLockTester(resourceService, bucketRequest, flightIdBase + "B");
BucketResourceLockTester resourceLockC = new BucketResourceLockTester(resourceService, bucketRequest, flightIdBase + "C");
Thread threadA = new Thread(resourceLockA);
Thread threadB = new Thread(resourceLockB);
Thread threadC = new Thread(resourceLockC);
configService.setFault(ConfigEnum.BUCKET_LOCK_CONFLICT_STOP_FAULT.name(), true);
threadA.start();
TimeUnit.SECONDS.sleep(1);
threadB.start();
threadB.join();
assertTrue("Thread B did get a lock exception", resourceLockB.gotLockException());
configService.setFault(ConfigEnum.BUCKET_LOCK_CONFLICT_CONTINUE_FAULT.name(), true);
threadA.join();
assertFalse("Thread A did not get a lock exception", resourceLockA.gotLockException());
GoogleBucketResource bucketResource = resourceLockA.getBucketResource();
assertNotNull("Thread A did create the bucket", bucketResource);
checkBucketExists(bucketResource.getResourceId());
threadC.start();
threadC.join();
assertFalse("Thread C did not get a lock exception", resourceLockC.gotLockException());
assertNotNull("Thread C did get the bucket", resourceLockC.getBucketResource());
deleteBucket(bucketResource.getName());
checkBucketDeleted(bucketResource.getName(), bucketResource.getResourceId());
}
use of bio.terra.service.resourcemanagement.google.GoogleBucketRequest in project jade-data-repo by DataBiosphere.
the class BucketResourceTest method createAndDeleteBucketTest.
@Test
public // create and delete the bucket, checking that the metadata and cloud state match what is expected
void createAndDeleteBucketTest() throws Exception {
String bucketName = "testbucket_createanddeletebuckettest";
String flightId = "createAndDeleteBucketTest";
bucketNames.add(bucketName);
// create the bucket and metadata
GoogleBucketRequest googleBucketRequest = buildBucketRequest(bucketName);
GoogleBucketResource bucketResource = resourceService.getOrCreateBucket(googleBucketRequest, flightId);
// check the bucket and metadata exist
checkBucketExists(bucketResource.getResourceId());
// delete the bucket and metadata
deleteBucket(bucketResource.getName());
checkBucketDeleted(bucketResource.getName(), bucketResource.getResourceId());
}
Aggregations