use of bio.terra.service.resourcemanagement.exception.EnablePermissionsFailedException in project jade-data-repo by DataBiosphere.
the class GoogleResourceService method enableIamPermissions.
public void enableIamPermissions(Map<String, List<String>> userPermissions, String projectId) throws InterruptedException {
GetIamPolicyRequest getIamPolicyRequest = new GetIamPolicyRequest();
Exception lastException = null;
int retryWait = INITIAL_WAIT_SECONDS;
for (int i = 0; i < RETRIES; i++) {
try {
CloudResourceManager resourceManager = cloudResourceManager();
Policy policy = resourceManager.projects().getIamPolicy(projectId, getIamPolicyRequest).execute();
List<Binding> bindingsList = policy.getBindings();
for (Map.Entry<String, List<String>> entry : userPermissions.entrySet()) {
Binding binding = new Binding().setRole(entry.getKey()).setMembers(entry.getValue());
bindingsList.add(binding);
}
policy.setBindings(bindingsList);
SetIamPolicyRequest setIamPolicyRequest = new SetIamPolicyRequest().setPolicy(policy);
resourceManager.projects().setIamPolicy(projectId, setIamPolicyRequest).execute();
return;
} catch (IOException | GeneralSecurityException ex) {
logger.info("Failed to enable iam permissions. Retry " + i + " of " + RETRIES, ex);
lastException = ex;
}
TimeUnit.SECONDS.sleep(retryWait);
retryWait = retryWait + retryWait;
if (retryWait > MAX_WAIT_SECONDS) {
retryWait = MAX_WAIT_SECONDS;
}
}
throw new EnablePermissionsFailedException("Cannot enable iam permissions", lastException);
}
Aggregations