Search in sources :

Example 1 with GoogleResourceException

use of bio.terra.service.resourcemanagement.exception.GoogleResourceException in project jade-data-repo by DataBiosphere.

the class GoogleResourceDao method getBucket.

/**
 * Fetch an existing bucket_resource metadata row using the name amd project id.
 * This method expects that there is exactly one row matching the provided name and project id.
 * @param bucketRequest
 * @return a reference to the bucket as a POJO GoogleBucketResource or null if not found
 * @throws GoogleResourceException if the bucket matches, but is in the wrong project
 * @throws CorruptMetadataException if multiple buckets have the same name
 */
public GoogleBucketResource getBucket(GoogleBucketRequest bucketRequest) {
    String bucketName = bucketRequest.getBucketName();
    List<GoogleBucketResource> bucketResourcesByName = retrieveBucketsBy("name", bucketName, String.class);
    if (bucketResourcesByName == null || bucketResourcesByName.size() == 0) {
        return null;
    }
    if (bucketResourcesByName.size() > 1) {
        // this also never happen because Google bucket names are unique
        throw new CorruptMetadataException("Multiple buckets found with same name: " + bucketName);
    }
    GoogleBucketResource bucketResource = bucketResourcesByName.get(0);
    UUID foundProjectId = bucketResource.getProjectResource().getRepositoryId();
    UUID requestedProjectId = bucketRequest.getGoogleProjectResource().getRepositoryId();
    if (!foundProjectId.equals(requestedProjectId)) {
        // there is a bucket with this name in our metadata, but it's for a different project
        throw new GoogleResourceException(String.format("A bucket with this name already exists for a different project: %s, %s", bucketName, requestedProjectId));
    }
    return bucketResource;
}
Also used : CorruptMetadataException(bio.terra.service.snapshot.exception.CorruptMetadataException) UUID(java.util.UUID) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException)

Example 2 with GoogleResourceException

use of bio.terra.service.resourcemanagement.exception.GoogleResourceException in project jade-data-repo by DataBiosphere.

the class GoogleResourceService method enableServices.

private void enableServices(GoogleProjectResource projectResource) throws InterruptedException {
    BatchEnableServicesRequest batchRequest = new BatchEnableServicesRequest().setServiceIds(projectResource.getServiceIds());
    try {
        ServiceUsage serviceUsage = serviceUsage();
        String projectNumberString = "projects/" + projectResource.getGoogleProjectNumber();
        logger.info("trying to get services for {} ({})", projectNumberString, projectResource.getGoogleProjectId());
        ServiceUsage.Services.List list = serviceUsage.services().list(projectNumberString).setFilter(ENABLED_FILTER);
        ListServicesResponse listServicesResponse = list.execute();
        logger.info("found: " + String.join(", ", projectResource.getServiceIds()));
        List<String> services = projectResource.getServiceIds().stream().map(s -> String.format("%s/services/%s", projectNumberString, s)).collect(Collectors.toList());
        List<Service> serviceList = listServicesResponse.getServices();
        List<String> actualServiceNames = Collections.emptyList();
        if (serviceList != null) {
            actualServiceNames = serviceList.stream().map(s -> s.getName()).collect(Collectors.toList());
        }
        if (actualServiceNames.containsAll(services)) {
            logger.info("project already has the right resources enabled, skipping");
        } else {
            logger.info("project does not have all resources enabled");
            ServiceUsage.Services.BatchEnable batchEnable = serviceUsage.services().batchEnable(projectNumberString, batchRequest);
            long timeout = resourceConfiguration.getProjectCreateTimeoutSeconds();
            blockUntilServiceOperationComplete(serviceUsage, batchEnable.execute(), timeout);
        }
    } catch (IOException | GeneralSecurityException e) {
        throw new GoogleResourceException("Could not enable services", e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Status(com.google.api.services.cloudresourcemanager.model.Status) ServiceUsage(com.google.api.services.serviceusage.v1beta1.ServiceUsage) ListServicesResponse(com.google.api.services.serviceusage.v1beta1.model.ListServicesResponse) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StorageOptions(com.google.cloud.storage.StorageOptions) Binding(com.google.api.services.cloudresourcemanager.model.Binding) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GeneralSecurityException(java.security.GeneralSecurityException) InaccessibleBillingAccountException(bio.terra.service.resourcemanagement.exception.InaccessibleBillingAccountException) Map(java.util.Map) Bucket(com.google.cloud.storage.Bucket) Project(com.google.api.services.cloudresourcemanager.model.Project) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) BucketInfo(com.google.cloud.storage.BucketInfo) Collection(java.util.Collection) HttpTransport(com.google.api.client.http.HttpTransport) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) UUID(java.util.UUID) BatchEnableServicesRequest(com.google.api.services.serviceusage.v1beta1.model.BatchEnableServicesRequest) Collectors(java.util.stream.Collectors) GcsProjectFactory(bio.terra.service.filedata.google.gcs.GcsProjectFactory) CorruptMetadataException(bio.terra.service.snapshot.exception.CorruptMetadataException) ConfigEnum(bio.terra.service.configuration.ConfigEnum) List(java.util.List) Service(com.google.api.services.serviceusage.v1beta1.model.Service) Environment(org.springframework.core.env.Environment) Storage(com.google.cloud.storage.Storage) ConfigurationService(bio.terra.service.configuration.ConfigurationService) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest) HashMap(java.util.HashMap) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest) Value(org.springframework.beans.factory.annotation.Value) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) ResourceId(com.google.api.services.cloudresourcemanager.model.ResourceId) EnablePermissionsFailedException(bio.terra.service.resourcemanagement.exception.EnablePermissionsFailedException) BillingProfile(bio.terra.service.resourcemanagement.BillingProfile) BucketLockException(bio.terra.service.resourcemanagement.exception.BucketLockException) Logger(org.slf4j.Logger) GcsProject(bio.terra.service.filedata.google.gcs.GcsProject) Policy(com.google.api.services.cloudresourcemanager.model.Policy) ProfileService(bio.terra.service.resourcemanagement.ProfileService) Acl(com.google.cloud.storage.Acl) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) JsonFactory(com.google.api.client.json.JsonFactory) StorageException(com.google.cloud.storage.StorageException) Operation(com.google.api.services.cloudresourcemanager.model.Operation) GoogleResourceNotFoundException(bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException) StorageClass(com.google.cloud.storage.StorageClass) Collections(java.util.Collections) GeneralSecurityException(java.security.GeneralSecurityException) Service(com.google.api.services.serviceusage.v1beta1.model.Service) ConfigurationService(bio.terra.service.configuration.ConfigurationService) ProfileService(bio.terra.service.resourcemanagement.ProfileService) IOException(java.io.IOException) ListServicesResponse(com.google.api.services.serviceusage.v1beta1.model.ListServicesResponse) ServiceUsage(com.google.api.services.serviceusage.v1beta1.ServiceUsage) BatchEnableServicesRequest(com.google.api.services.serviceusage.v1beta1.model.BatchEnableServicesRequest) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException)

Example 3 with GoogleResourceException

use of bio.terra.service.resourcemanagement.exception.GoogleResourceException in project jade-data-repo by DataBiosphere.

the class GoogleResourceService method newProject.

private GoogleProjectResource newProject(GoogleProjectRequest projectRequest, String googleProjectId) throws InterruptedException {
    BillingProfile profile = profileService.getProfileById(projectRequest.getProfileId());
    logger.info("creating a new project: {}", projectRequest.getProjectId());
    if (!profile.isAccessible()) {
        throw new InaccessibleBillingAccountException("The repository needs access to this billing account " + "in order to create: " + googleProjectId);
    }
    // projects created by service accounts must live under a parent resource (either a folder or an organization)
    ResourceId parentResource = new ResourceId().setType(resourceConfiguration.getParentResourceType()).setId(resourceConfiguration.getParentResourceId());
    Project requestBody = new Project().setName(googleProjectId).setProjectId(googleProjectId).setParent(parentResource);
    try {
        // kick off a project create request and poll until it is done
        CloudResourceManager resourceManager = cloudResourceManager();
        CloudResourceManager.Projects.Create request = resourceManager.projects().create(requestBody);
        Operation operation = request.execute();
        long timeout = resourceConfiguration.getProjectCreateTimeoutSeconds();
        blockUntilResourceOperationComplete(resourceManager, operation, timeout);
        // it should be retrievable once the create operation is complete
        Project project = getProject(googleProjectId);
        if (project == null) {
            throw new GoogleResourceException("Could not get project after creation");
        }
        String googleProjectNumber = project.getProjectNumber().toString();
        GoogleProjectResource googleProjectResource = new GoogleProjectResource(projectRequest).googleProjectId(googleProjectId).googleProjectNumber(googleProjectNumber);
        setupBilling(googleProjectResource);
        enableServices(googleProjectResource);
        enableIamPermissions(googleProjectResource.getRoleIdentityMapping(), googleProjectId);
        UUID repositoryId = resourceDao.createProject(googleProjectResource);
        return googleProjectResource.repositoryId(repositoryId);
    } catch (IOException | GeneralSecurityException e) {
        throw new GoogleResourceException("Could not create project", e);
    }
}
Also used : CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) GeneralSecurityException(java.security.GeneralSecurityException) InaccessibleBillingAccountException(bio.terra.service.resourcemanagement.exception.InaccessibleBillingAccountException) Operation(com.google.api.services.cloudresourcemanager.model.Operation) IOException(java.io.IOException) BillingProfile(bio.terra.service.resourcemanagement.BillingProfile) Project(com.google.api.services.cloudresourcemanager.model.Project) GcsProject(bio.terra.service.filedata.google.gcs.GcsProject) ResourceId(com.google.api.services.cloudresourcemanager.model.ResourceId) UUID(java.util.UUID) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException)

Example 4 with GoogleResourceException

use of bio.terra.service.resourcemanagement.exception.GoogleResourceException in project jade-data-repo by DataBiosphere.

the class GoogleResourceService method getProject.

public Project getProject(String googleProjectId) {
    try {
        CloudResourceManager resourceManager = cloudResourceManager();
        CloudResourceManager.Projects.Get request = resourceManager.projects().get(googleProjectId);
        return request.execute();
    } catch (GoogleJsonResponseException e) {
        // if the project does not exist, the API will return a 403 unauth. to prevent people probing for projects
        if (e.getDetails().getCode() != 403) {
            throw new GoogleResourceException("Unexpected error while checking on project state", e);
        }
        return null;
    } catch (IOException | GeneralSecurityException e) {
        throw new GoogleResourceException("Could not check on project state", e);
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException)

Example 5 with GoogleResourceException

use of bio.terra.service.resourcemanagement.exception.GoogleResourceException in project jade-data-repo by DataBiosphere.

the class GoogleResourceService method blockUntilResourceOperationComplete.

/**
 * Poll the resource manager api until an operation completes. It is possible to hit quota issues here, so the
 * timeout is set to 10 seconds.
 * @param resourceManager service instance
 * @param operation has an id for us to use in the check
 * @param timeoutSeconds how many seconds before we give up
 * @return a completed operation
 */
private static Operation blockUntilResourceOperationComplete(CloudResourceManager resourceManager, Operation operation, long timeoutSeconds) throws IOException, InterruptedException {
    long start = System.currentTimeMillis();
    // 10 seconds
    final long pollInterval = 10 * 1000;
    String opId = operation.getName();
    while (operation != null && (operation.getDone() == null || !operation.getDone())) {
        Status error = operation.getError();
        if (error != null) {
            throw new GoogleResourceException("Error while waiting for operation to complete" + error.getMessage());
        }
        Thread.sleep(pollInterval);
        long elapsed = System.currentTimeMillis() - start;
        if (elapsed >= timeoutSeconds * 1000) {
            throw new GoogleResourceException("Timed out waiting for operation to complete");
        }
        logger.info("checking operation: {}", opId);
        CloudResourceManager.Operations.Get request = resourceManager.operations().get(opId);
        operation = request.execute();
    }
    return operation;
}
Also used : Status(com.google.api.services.cloudresourcemanager.model.Status) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException)

Aggregations

GoogleResourceException (bio.terra.service.resourcemanagement.exception.GoogleResourceException)7 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)4 IOException (java.io.IOException)4 GeneralSecurityException (java.security.GeneralSecurityException)4 UUID (java.util.UUID)3 GcsProject (bio.terra.service.filedata.google.gcs.GcsProject)2 BillingProfile (bio.terra.service.resourcemanagement.BillingProfile)2 InaccessibleBillingAccountException (bio.terra.service.resourcemanagement.exception.InaccessibleBillingAccountException)2 CorruptMetadataException (bio.terra.service.snapshot.exception.CorruptMetadataException)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 Operation (com.google.api.services.cloudresourcemanager.model.Operation)2 Project (com.google.api.services.cloudresourcemanager.model.Project)2 ResourceId (com.google.api.services.cloudresourcemanager.model.ResourceId)2 Status (com.google.api.services.cloudresourcemanager.model.Status)2 DaoKeyHolder (bio.terra.common.DaoKeyHolder)1 ConfigEnum (bio.terra.service.configuration.ConfigEnum)1 ConfigurationService (bio.terra.service.configuration.ConfigurationService)1 GcsProjectFactory (bio.terra.service.filedata.google.gcs.GcsProjectFactory)1 ProfileService (bio.terra.service.resourcemanagement.ProfileService)1 BucketLockException (bio.terra.service.resourcemanagement.exception.BucketLockException)1