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;
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations