Search in sources :

Example 6 with Role

use of com.google.api.services.iam.v1.model.Role in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiManager method createServiceAccountAndDownloadKey.

/**
 * Creates a new {@link ServiceAccount}, adds the supplied set of {@link Role roles} to it, and
 * creates and downloads the service account private key to the user's file system.
 *
 * @param roles the set of {@link Role} to add to the new service account
 * @param name the name of the new service account to be created
 * @param downloadDir the {@link Path} of the download directory of the service account private
 *     key json file
 * @param cloudProject the current {@link CloudProject}
 * @param project the current {@link Project}
 */
static void createServiceAccountAndDownloadKey(Set<Role> roles, String name, Path downloadDir, CloudProject cloudProject, Project project) {
    Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername());
    if (!user.isPresent()) {
        LOG.error("Cannot enable APIs: logged in user not found.");
        return;
    }
    ProgressIndicator progress = ServiceManager.getService(ProgressManager.class).getProgressIndicator();
    try {
        int numSteps = roles.isEmpty() ? 3 : 4;
        double step = 0;
        updateProgress(progress, GctBundle.message("cloud.apis.service.account.create.account.progress.message", name), step / numSteps);
        step++;
        ServiceAccount serviceAccount = createServiceAccount(user.get(), name, cloudProject);
        if (!roles.isEmpty()) {
            updateProgress(progress, GctBundle.message("cloud.apis.service.account.add.roles.progress.message"), step / numSteps);
            step++;
            addRolesToServiceAccount(user.get(), serviceAccount, roles, cloudProject);
        }
        updateProgress(progress, GctBundle.message("cloud.apis.service.account.create.key.progress.message"), step / numSteps);
        step++;
        ServiceAccountKey serviceAccountKey = createServiceAccountKey(user.get(), serviceAccount);
        updateProgress(progress, GctBundle.message("cloud.apis.service.account.download.key.progress.message"), step / numSteps);
        Path keyPath = writeServiceAccountKey(serviceAccountKey, downloadDir, cloudProject);
        notifyServiceAccountCreated(project, name, keyPath);
    } catch (IOException e) {
        LOG.warn("Exception occurred attempting to create service account on GCP and download its key", e);
        notifyServiceAccountError(project, name, e.toString());
    }
}
Also used : Path(java.nio.file.Path) ServiceAccount(com.google.api.services.iam.v1.model.ServiceAccount) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProgressManager(com.intellij.openapi.progress.ProgressManager) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) IOException(java.io.IOException) ServiceAccountKey(com.google.api.services.iam.v1.model.ServiceAccountKey)

Example 7 with Role

use of com.google.api.services.iam.v1.model.Role in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiManagerTest method createServiceAccountAndDownloadKey_withRoles_createsKey.

@Test
public void createServiceAccountAndDownloadKey_withRoles_createsKey() {
    Role role = new Role();
    role.setName("my-role");
    Set<Role> roles = ImmutableSet.of(role);
    CloudApiManager.createServiceAccountAndDownloadKey(roles, SERVICE_ACCOUNT_NAME, downloadDir.toPath(), cloudProject, testFixture.getProject());
    verify(progressIndicator, times(4)).setText(anyString());
    verify(progressIndicator, times(4)).setFraction(anyDouble());
    String[] contents = downloadDir.list();
    assertThat(contents.length).isEqualTo(1);
    assertThat(contents[0]).startsWith(CLOUD_PROJECT_NAME);
}
Also used : Role(com.google.api.services.iam.v1.model.Role) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 8 with Role

use of com.google.api.services.iam.v1.model.Role in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiManagementConfirmationDialogTest method serviceAccountUi_whenSomeRolesExist_isVisible.

@Test
public void serviceAccountUi_whenSomeRolesExist_isVisible() {
    Set<CloudLibrary> libraries = ImmutableSet.of(TestCloudLibrary.createEmpty().toCloudLibrary());
    Role role = new Role();
    role.setName("name");
    role.setTitle("title");
    Set<Role> roles = ImmutableSet.of(role);
    ApplicationManager.getApplication().invokeAndWait(() -> {
        CloudApiManagementConfirmationDialog dialog = new CloudApiManagementConfirmationDialog(module, cloudProject, libraries, ImmutableSet.of(), roles);
        assertThat(dialog.getRolePanel().isVisible()).isTrue();
    });
}
Also used : Role(com.google.api.services.iam.v1.model.Role) CloudLibrary(com.google.cloud.tools.libraries.json.CloudLibrary) TestCloudLibrary(com.google.cloud.tools.intellij.testing.apis.TestCloudLibrary) Test(org.junit.Test)

Aggregations

Role (com.google.api.services.iam.v1.model.Role)6 CloudLibrary (com.google.cloud.tools.libraries.json.CloudLibrary)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)3 Iam (com.google.api.services.iam.v1.Iam)2 ServiceAccount (com.google.api.services.iam.v1.model.ServiceAccount)2 ServiceAccountKey (com.google.api.services.iam.v1.model.ServiceAccountKey)2 CloudProject (com.google.cloud.tools.intellij.project.CloudProject)2 TestCloudLibrary (com.google.cloud.tools.intellij.testing.apis.TestCloudLibrary)2 Notification (com.intellij.notification.Notification)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2 Base64 (com.google.api.client.util.Base64)1 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)1 Binding (com.google.api.services.cloudresourcemanager.model.Binding)1 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest)1 Policy (com.google.api.services.cloudresourcemanager.model.Policy)1 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest)1 CreateServiceAccountKeyRequest (com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest)1