Search in sources :

Example 1 with GOOGLE_PROJECT_ID

use of bio.terra.buffer.service.resource.FlightMapKeys.GOOGLE_PROJECT_ID in project terra-resource-buffer by DataBiosphere.

the class SetIamPolicyStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws RetryException {
    // Skip if IAM binding is not set.
    if (gcpProjectConfig.getIamBindings() == null || gcpProjectConfig.getIamBindings().isEmpty()) {
        return StepResult.getStepResultSuccess();
    }
    String projectId = flightContext.getWorkingMap().get(GOOGLE_PROJECT_ID, String.class);
    try {
        Policy policy = rmCow.projects().getIamPolicy(projectId, new GetIamPolicyRequest()).execute();
        gcpProjectConfig.getIamBindings().stream().map(iamBinding -> new Binding().setRole(iamBinding.getRole()).setMembers(iamBinding.getMembers())).forEach(policy.getBindings()::add);
        rmCow.projects().setIamPolicy(projectId, new SetIamPolicyRequest().setPolicy(policy)).execute();
    } catch (IOException e) {
        logger.info("Error when setting IAM policy", e);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : StepResult(bio.terra.stairway.StepResult) Logger(org.slf4j.Logger) Step(bio.terra.stairway.Step) RetryException(bio.terra.stairway.exception.RetryException) com.google.api.services.cloudresourcemanager.v3.model(com.google.api.services.cloudresourcemanager.v3.model) GcpProjectConfig(bio.terra.buffer.generated.model.GcpProjectConfig) LoggerFactory(org.slf4j.LoggerFactory) CloudResourceManagerCow(bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow) IOException(java.io.IOException) StepStatus(bio.terra.stairway.StepStatus) GOOGLE_PROJECT_ID(bio.terra.buffer.service.resource.FlightMapKeys.GOOGLE_PROJECT_ID) FlightContext(bio.terra.stairway.FlightContext) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult)

Aggregations

GcpProjectConfig (bio.terra.buffer.generated.model.GcpProjectConfig)1 GOOGLE_PROJECT_ID (bio.terra.buffer.service.resource.FlightMapKeys.GOOGLE_PROJECT_ID)1 CloudResourceManagerCow (bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow)1 FlightContext (bio.terra.stairway.FlightContext)1 Step (bio.terra.stairway.Step)1 StepResult (bio.terra.stairway.StepResult)1 StepStatus (bio.terra.stairway.StepStatus)1 RetryException (bio.terra.stairway.exception.RetryException)1 com.google.api.services.cloudresourcemanager.v3.model (com.google.api.services.cloudresourcemanager.v3.model)1 IOException (java.io.IOException)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1