Search in sources :

Example 1 with Operation

use of com.google.api.services.cloudresourcemanager.model.Operation 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 2 with Operation

use of com.google.api.services.cloudresourcemanager.model.Operation in project java-resourcemanager by googleapis.

the class HttpResourceManagerRpc method create.

@Override
public Project create(Project project) {
    final Operation operation;
    try {
        operation = resourceManager.projects().create(project).execute();
    } catch (IOException ex) {
        throw translate(ex);
    }
    Operation finishedOp = runWithRetries(new Callable<Operation>() {

        @Override
        public Operation call() {
            try {
                return resourceManager.operations().get(operation.getName()).execute();
            } catch (IOException ex) {
                throw translate(ex);
            }
        }
    }, CREATE_RETRY_SETTINGS, OPERATION_HANDLER, clock);
    if (finishedOp.getError() != null) {
        throw translate(finishedOp.getError());
    }
    // so execute(), not knowing the type, parses it as String, not Long.
    try {
        String responseTxt = JSON_FACTORY.toString(finishedOp.getResponse());
        return JSON_FACTORY.fromString(responseTxt, Project.class);
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : Operation(com.google.api.services.cloudresourcemanager.model.Operation) IOException(java.io.IOException)

Example 3 with Operation

use of com.google.api.services.cloudresourcemanager.model.Operation in project java-resourcemanager by googleapis.

the class LocalResourceManagerHelper method create.

synchronized Response create(Project project) {
    String customErrorMessage = checkForProjectErrors(project);
    if (customErrorMessage != null) {
        return Error.INVALID_ARGUMENT.response(customErrorMessage);
    } else {
        project.setLifecycleState("ACTIVE");
        project.setProjectNumber(Math.abs(PROJECT_NUMBER_GENERATOR.nextLong() % Long.MAX_VALUE));
        project.setCreateTime(DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC).format(Instant.ofEpochMilli(System.currentTimeMillis())));
        if (projects.putIfAbsent(project.getProjectId(), project) != null) {
            return Error.ALREADY_EXISTS.response("A project with the same project ID (" + project.getProjectId() + ") already exists.");
        }
        Policy emptyPolicy = new Policy().setBindings(Collections.<Binding>emptyList()).setEtag(UUID.randomUUID().toString()).setVersion(0);
        policies.put(project.getProjectId(), emptyPolicy);
        try {
            // Pretend it's not done yet.
            String createdProjectStr = jsonFactory.toString(new Operation().setDone(false).setName("operations/" + project.getProjectId()));
            return new Response(HTTP_OK, createdProjectStr);
        } catch (IOException e) {
            return Error.INTERNAL_ERROR.response("Error serializing project " + project.getProjectId());
        }
    }
}
Also used : Policy(com.google.api.services.cloudresourcemanager.model.Policy) TestIamPermissionsResponse(com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse) Operation(com.google.api.services.cloudresourcemanager.model.Operation) IOException(java.io.IOException)

Aggregations

Operation (com.google.api.services.cloudresourcemanager.model.Operation)3 IOException (java.io.IOException)3 GcsProject (bio.terra.service.filedata.google.gcs.GcsProject)1 BillingProfile (bio.terra.service.resourcemanagement.BillingProfile)1 GoogleResourceException (bio.terra.service.resourcemanagement.exception.GoogleResourceException)1 InaccessibleBillingAccountException (bio.terra.service.resourcemanagement.exception.InaccessibleBillingAccountException)1 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)1 Policy (com.google.api.services.cloudresourcemanager.model.Policy)1 Project (com.google.api.services.cloudresourcemanager.model.Project)1 ResourceId (com.google.api.services.cloudresourcemanager.model.ResourceId)1 TestIamPermissionsResponse (com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse)1 GeneralSecurityException (java.security.GeneralSecurityException)1 UUID (java.util.UUID)1