Search in sources :

Example 1 with CloudResourceManager

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();
}
Also used : Policy(com.google.api.services.cloudresourcemanager.model.Policy) Binding(com.google.api.services.cloudresourcemanager.model.Binding) ServiceAccountKey(com.google.api.services.iam.v1.model.ServiceAccountKey) ZonedDateTime(java.time.ZonedDateTime) Binding(com.google.api.services.cloudresourcemanager.model.Binding) Role(com.google.api.services.iam.v1.model.Role) CloudProject(com.google.cloud.tools.intellij.project.CloudProject) Logger(com.intellij.openapi.diagnostic.Logger) Path(java.nio.file.Path) ProgressManager(com.intellij.openapi.progress.ProgressManager) CloudLibrary(com.google.cloud.tools.libraries.json.CloudLibrary) Set(java.util.Set) EnableServiceRequest(com.google.api.services.servicemanagement.model.EnableServiceRequest) GoogleApiClientFactory(com.google.cloud.tools.intellij.resources.GoogleApiClientFactory) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) NotificationType(com.intellij.notification.NotificationType) Notification(com.intellij.notification.Notification) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) Services(com.google.cloud.tools.intellij.login.Services) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Optional(java.util.Optional) ServiceManagement(com.google.api.services.servicemanagement.ServiceManagement) Pattern(java.util.regex.Pattern) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest) NotificationDisplayType(com.intellij.notification.NotificationDisplayType) ServiceAccount(com.google.api.services.iam.v1.model.ServiceAccount) ArrayList(java.util.ArrayList) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) PropertiesFileFlagReader(com.google.cloud.tools.intellij.flags.PropertiesFileFlagReader) NotificationGroup(com.intellij.notification.NotificationGroup) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) CreateServiceAccountKeyRequest(com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest) Project(com.intellij.openapi.project.Project) GctBundle(com.google.cloud.tools.intellij.util.GctBundle) DialogManager(git4idea.DialogManager) Policy(com.google.api.services.cloudresourcemanager.model.Policy) Base64(com.google.api.client.util.Base64) Files(java.nio.file.Files) GctTracking(com.google.cloud.tools.intellij.analytics.GctTracking) IOException(java.io.IOException) UsageTrackerProvider(com.google.cloud.tools.intellij.analytics.UsageTrackerProvider) Paths(java.nio.file.Paths) CreateServiceAccountRequest(com.google.api.services.iam.v1.model.CreateServiceAccountRequest) DateTimeFormatter(java.time.format.DateTimeFormatter) Iam(com.google.api.services.iam.v1.Iam) GoogleCloudCoreIcons(com.google.cloud.tools.intellij.GoogleCloudCoreIcons) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest)

Example 2 with CloudResourceManager

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;
}
Also used : Project(com.google.api.services.cloudresourcemanager.model.Project) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Set(java.util.Set) ThreadUtil(com.google.cloud.tools.intellij.util.ThreadUtil) IOException(java.io.IOException) GoogleApiClientFactory(com.google.cloud.tools.intellij.resources.GoogleApiClientFactory) FutureCallback(com.google.common.util.concurrent.FutureCallback) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) List(java.util.List) Strings(com.google.api.client.repackaged.com.google.common.base.Strings) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) Comparator(java.util.Comparator) ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse) Project(com.google.api.services.cloudresourcemanager.model.Project) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)

Aggregations

CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)2 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)2 GoogleApiClientFactory (com.google.cloud.tools.intellij.resources.GoogleApiClientFactory)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 Strings (com.google.api.client.repackaged.com.google.common.base.Strings)1 Base64 (com.google.api.client.util.Base64)1 Binding (com.google.api.services.cloudresourcemanager.model.Binding)1 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest)1 ListProjectsResponse (com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)1 Policy (com.google.api.services.cloudresourcemanager.model.Policy)1 Project (com.google.api.services.cloudresourcemanager.model.Project)1 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest)1 Iam (com.google.api.services.iam.v1.Iam)1 CreateServiceAccountKeyRequest (com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest)1 CreateServiceAccountRequest (com.google.api.services.iam.v1.model.CreateServiceAccountRequest)1 Role (com.google.api.services.iam.v1.model.Role)1 ServiceAccount (com.google.api.services.iam.v1.model.ServiceAccount)1