Search in sources :

Example 1 with GoogleProjectResource

use of bio.terra.service.resourcemanagement.google.GoogleProjectResource in project jade-data-repo by DataBiosphere.

the class GcsPdao method storageForBucket.

private Storage storageForBucket(GoogleBucketResource bucketResource) {
    GoogleProjectResource projectResource = bucketResource.getProjectResource();
    GcsProject gcsProject = gcsProjectFactory.get(projectResource.getGoogleProjectId());
    return gcsProject.getStorage();
}
Also used : GoogleProjectResource(bio.terra.service.resourcemanagement.google.GoogleProjectResource)

Example 2 with GoogleProjectResource

use of bio.terra.service.resourcemanagement.google.GoogleProjectResource in project jade-data-repo by DataBiosphere.

the class DataLocationService method getProject.

/**
 * Fetch existing SnapshotDataProject for the Snapshot.
 * Delete it if it's invalid, that is, the referenced cloud resource doesn't exist.
 * @param snapshot
 * @return a populated SnapshotDataProject if one exists, empty if not
 */
public Optional<SnapshotDataProject> getProject(Snapshot snapshot) {
    SnapshotDataProjectSummary snapshotDataProjectSummary = null;
    try {
        // first, check if SnapshotDataProjectSummary (= mapping btw Snapshot ID and cloud Project ID) exists
        snapshotDataProjectSummary = dataProjectDao.retrieveSnapshotDataProject(snapshot.getId());
        // second, check if the referenced cloud resource exists
        GoogleProjectResource googleProjectResource = resourceService.getProjectResourceById(snapshotDataProjectSummary.getProjectResourceId());
        // if both exist, then create the DatasetDataProject object from the summary and return here
        return Optional.of(new SnapshotDataProject(snapshotDataProjectSummary).googleProjectResource(googleProjectResource));
    } catch (DataProjectNotFoundException projNfEx) {
    // suppress exception here, will create later
    } catch (GoogleResourceNotFoundException rsrcNfEx) {
        // I don't think this null check will ever be false, but just in case
        if (snapshotDataProjectSummary != null) {
            logger.warn("metadata has a project resource id it can't resolve for snapshot: " + snapshot.getName());
            dataProjectDao.deleteSnapshotDataProject(snapshotDataProjectSummary.getId());
        }
    }
    // did not find a valid SnapshotDataProject for the given Snapshot
    return Optional.empty();
}
Also used : SnapshotDataProjectSummary(bio.terra.service.snapshot.SnapshotDataProjectSummary) GoogleResourceNotFoundException(bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException) GoogleProjectResource(bio.terra.service.resourcemanagement.google.GoogleProjectResource) DataProjectNotFoundException(bio.terra.service.resourcemanagement.exception.DataProjectNotFoundException) SnapshotDataProject(bio.terra.service.snapshot.SnapshotDataProject)

Example 3 with GoogleProjectResource

use of bio.terra.service.resourcemanagement.google.GoogleProjectResource 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 4 with GoogleProjectResource

use of bio.terra.service.resourcemanagement.google.GoogleProjectResource in project jade-data-repo by DataBiosphere.

the class DataLocationService method getOrCreateProject.

/**
 * Fetch existing SnapshotDataProject for the Snapshot.
 * Create a new one if none exists already.
 * @param snapshot
 * @return a populated and valid SnapshotDataProject
 */
public SnapshotDataProject getOrCreateProject(Snapshot snapshot) throws InterruptedException {
    // check if for an existing SnapshotDataProject first, and return here if found one
    Optional<SnapshotDataProject> existingDataProject = getProject(snapshot);
    if (existingDataProject.isPresent()) {
        return existingDataProject.get();
    }
    // if we've made it here, then we need to create a new cloud resource and SnapshotDataProject
    GoogleProjectRequest googleProjectRequest = new GoogleProjectRequest().projectId(dataLocationSelector.projectIdForSnapshot(snapshot)).profileId(snapshot.getProfileId()).serviceIds(DATA_PROJECT_SERVICE_IDS);
    GoogleProjectResource googleProjectResource = resourceService.getOrCreateProject(googleProjectRequest);
    // create the SnapshotDataProjectSummary object first, which just holds all the IDs
    SnapshotDataProjectSummary snapshotDataProjectSummary = new SnapshotDataProjectSummary().projectResourceId(googleProjectResource.getRepositoryId()).snapshotId(snapshot.getId());
    UUID snapshotDataProjectId = dataProjectDao.createSnapshotDataProject(snapshotDataProjectSummary);
    snapshotDataProjectSummary.id(snapshotDataProjectId);
    // then create the SnapshotDataProject object from the summary
    return new SnapshotDataProject(snapshotDataProjectSummary).googleProjectResource(googleProjectResource);
}
Also used : SnapshotDataProjectSummary(bio.terra.service.snapshot.SnapshotDataProjectSummary) GoogleProjectResource(bio.terra.service.resourcemanagement.google.GoogleProjectResource) GoogleProjectRequest(bio.terra.service.resourcemanagement.google.GoogleProjectRequest) UUID(java.util.UUID) SnapshotDataProject(bio.terra.service.snapshot.SnapshotDataProject)

Example 5 with GoogleProjectResource

use of bio.terra.service.resourcemanagement.google.GoogleProjectResource in project jade-data-repo by DataBiosphere.

the class DataLocationService method getProject.

/**
 * Fetch existing DatasetDataProject for the Dataset.
 * Delete it if it's invalid, that is, the referenced cloud resource doesn't exist.
 * @param dataset
 * @return a populated DatasetDataProject if one exists, empty if not
 */
public Optional<DatasetDataProject> getProject(Dataset dataset) {
    DatasetDataProjectSummary datasetDataProjectSummary = null;
    try {
        // first, check if DatasetDataProjectSummary (= mapping btw Dataset ID and cloud Project ID) exists
        datasetDataProjectSummary = dataProjectDao.retrieveDatasetDataProject(dataset.getId());
        // second, check if the referenced cloud resource exists
        GoogleProjectResource googleProjectResource = resourceService.getProjectResourceById(datasetDataProjectSummary.getProjectResourceId());
        // if both exist, then create the DatasetDataProject object from the summary and return here
        return Optional.of(new DatasetDataProject(datasetDataProjectSummary).googleProjectResource(googleProjectResource));
    } catch (DataProjectNotFoundException projNfEx) {
    // suppress exception here, will create later
    } catch (GoogleResourceNotFoundException rsrcNfEx) {
        // I don't think this null check will ever be false, but just in case
        if (datasetDataProjectSummary != null) {
            logger.warn("metadata has a project resource id it can't resolve for dataset: " + dataset.getName());
            dataProjectDao.deleteDatasetDataProject(datasetDataProjectSummary.getId());
        }
    }
    // did not find a valid DatasetDataProject for the given Dataset
    return Optional.empty();
}
Also used : GoogleResourceNotFoundException(bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException) GoogleProjectResource(bio.terra.service.resourcemanagement.google.GoogleProjectResource) DataProjectNotFoundException(bio.terra.service.resourcemanagement.exception.DataProjectNotFoundException) DatasetDataProjectSummary(bio.terra.service.dataset.DatasetDataProjectSummary) DatasetDataProject(bio.terra.service.dataset.DatasetDataProject)

Aggregations

GoogleProjectResource (bio.terra.service.resourcemanagement.google.GoogleProjectResource)8 GoogleProjectRequest (bio.terra.service.resourcemanagement.google.GoogleProjectRequest)4 DatasetDataProject (bio.terra.service.dataset.DatasetDataProject)2 DatasetDataProjectSummary (bio.terra.service.dataset.DatasetDataProjectSummary)2 DataProjectNotFoundException (bio.terra.service.resourcemanagement.exception.DataProjectNotFoundException)2 GoogleResourceNotFoundException (bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException)2 GoogleBucketRequest (bio.terra.service.resourcemanagement.google.GoogleBucketRequest)2 SnapshotDataProject (bio.terra.service.snapshot.SnapshotDataProject)2 SnapshotDataProjectSummary (bio.terra.service.snapshot.SnapshotDataProjectSummary)2 HashMap (java.util.HashMap)2 List (java.util.List)2 UUID (java.util.UUID)2 Project (com.google.api.services.cloudresourcemanager.model.Project)1 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1