use of com.google.api.services.cloudresourcemanager.CloudResourceManager in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudApiManager method addRolesToServiceAccount.
/**
* Adds a set of {@link Role roles} to a {@link ServiceAccount}.
*
* <p>This is done by fetching the cloud project's existing IAM Policy, adding the new roles to
* the given service account, and then writing the updated policy back to the cloud project.
*
* @param user the current {@link CredentialedUser}
* @param serviceAccount the {@link ServiceAccount} to which to add roles
* @param roles the set of {@link Role} to be added to the service account
* @param cloudProject the current {@link CloudProject}
* @throws IOException if the API call fails to update the IAM policy
*/
private static void addRolesToServiceAccount(CredentialedUser user, ServiceAccount serviceAccount, Set<Role> roles, CloudProject cloudProject) throws IOException {
CloudResourceManager resourceManager = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential());
Policy existingPolicy = resourceManager.projects().getIamPolicy(cloudProject.projectId(), new GetIamPolicyRequest()).execute();
List<Binding> bindings = Lists.newArrayList(existingPolicy.getBindings());
List<Binding> additionalBindings = roles.stream().map(role -> {
Binding binding = new Binding();
binding.setRole(role.getName());
binding.setMembers(createServiceAccountMemberBindings(serviceAccount));
return binding;
}).collect(Collectors.toList());
bindings.addAll(additionalBindings);
SetIamPolicyRequest policyRequest = new SetIamPolicyRequest();
Policy newPolicy = new Policy();
newPolicy.setBindings(bindings);
policyRequest.setPolicy(newPolicy);
resourceManager.projects().setIamPolicy(cloudProject.projectId(), policyRequest).execute();
}
use of com.google.api.services.cloudresourcemanager.CloudResourceManager in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectLoader method loadUserProjects.
private List<Project> loadUserProjects(CredentialedUser user) throws IOException {
CloudResourceManager cloudResourceManagerClient = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential());
final List<Project> result = new ArrayList<>();
ListProjectsResponse response = cloudResourceManagerClient.projects().list().setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
if (response != null && response.getProjects() != null) {
// Create a sorted set to sort the projects list by project name.
Set<Project> allProjects = new TreeSet<>(Comparator.comparing(project -> project.getName().toLowerCase()));
allProjects.addAll(response.getProjects());
while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
response = cloudResourceManagerClient.projects().list().setPageToken(response.getNextPageToken()).setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
allProjects.addAll(response.getProjects());
}
allProjects.stream().filter((project) -> !PROJECT_DELETE_REQUESTED.equals(project.getLifecycleState())).filter((project) -> !Strings.isNullOrEmpty(project.getProjectId())).forEach(result::add);
}
return result;
}
Aggregations