Search in sources :

Example 1 with GoogleBucketRequest

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);
}
Also used : GoogleProjectResource(bio.terra.service.resourcemanagement.google.GoogleProjectResource) GoogleBucketRequest(bio.terra.service.resourcemanagement.google.GoogleBucketRequest)

Example 2 with GoogleBucketRequest

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());
}
Also used : GoogleBucketResource(bio.terra.service.resourcemanagement.google.GoogleBucketResource) Bucket(com.google.cloud.storage.Bucket) GoogleBucketRequest(bio.terra.service.resourcemanagement.google.GoogleBucketRequest) CorruptMetadataException(bio.terra.service.snapshot.exception.CorruptMetadataException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with GoogleBucketRequest

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);
}
Also used : GoogleResourceNotFoundException(bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException) GoogleBucketResource(bio.terra.service.resourcemanagement.google.GoogleBucketResource) GoogleBucketRequest(bio.terra.service.resourcemanagement.google.GoogleBucketRequest) CorruptMetadataException(bio.terra.service.snapshot.exception.CorruptMetadataException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with GoogleBucketRequest

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());
}
Also used : GoogleBucketResource(bio.terra.service.resourcemanagement.google.GoogleBucketResource) GoogleBucketRequest(bio.terra.service.resourcemanagement.google.GoogleBucketRequest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with GoogleBucketRequest

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());
}
Also used : GoogleBucketResource(bio.terra.service.resourcemanagement.google.GoogleBucketResource) GoogleBucketRequest(bio.terra.service.resourcemanagement.google.GoogleBucketRequest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

GoogleBucketRequest (bio.terra.service.resourcemanagement.google.GoogleBucketRequest)6 GoogleBucketResource (bio.terra.service.resourcemanagement.google.GoogleBucketResource)4 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 GoogleProjectResource (bio.terra.service.resourcemanagement.google.GoogleProjectResource)2 CorruptMetadataException (bio.terra.service.snapshot.exception.CorruptMetadataException)2 GoogleResourceNotFoundException (bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException)1 GoogleProjectRequest (bio.terra.service.resourcemanagement.google.GoogleProjectRequest)1 Bucket (com.google.cloud.storage.Bucket)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1