Search in sources :

Example 1 with CloudLibrary

use of com.google.cloud.tools.libraries.json.CloudLibrary in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudLibraryDependencyWriter method addLibrariesToMavenModule.

/**
 * Adds the given set of {@link CloudLibrary CloudLibraries} to the given Maven {@link Module}.
 *
 * <p>If the given {@link Module} is not a Maven module, this method may throw an undefined {@link
 * RuntimeException}.
 *
 * @param libraries the set of {@link CloudLibrary CloudLibraries} to add
 * @param module the Maven {@link Module} to add the libraries to
 * @param bomVersion the version of the BOM to write. May be null
 */
private static void addLibrariesToMavenModule(@NotNull Set<CloudLibrary> libraries, @NotNull Module module, @Nullable String bomVersion) {
    Project project = module.getProject();
    MavenProject mavenProject = MavenProjectsManager.getInstance(project).findProject(module);
    MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(project, mavenProject.getFile());
    new WriteCommandAction(project, DomUtil.getFile(model)) {

        @Override
        protected void run(@NotNull Result result) {
            List<MavenDomDependency> dependencies = model.getDependencies().getDependencies();
            Map<Boolean, List<MavenId>> mavenIdsMap = libraries.stream().map(CloudLibraryUtils::getFirstJavaClientMavenCoordinates).filter(Optional::isPresent).map(Optional::get).map(MavenUtils::toMavenId).collect(Collectors.partitioningBy(mavenId -> MavenUtils.isMavenIdInDependencyList(mavenId, dependencies)));
            // The MavenIds in the "true" list are already in the pom.xml, so we don't duplicate them
            // and warn the user that they weren't added.
            List<MavenId> ignoredMavenIds = mavenIdsMap.get(true);
            notifyIgnoredDependencies(ignoredMavenIds, project);
            // The MavenIds in the "false" list are not in the pom.xml, so we add them and notify the
            // user when it's complete.
            List<MavenId> newMavenIds = mavenIdsMap.get(false);
            if (!newMavenIds.isEmpty()) {
                newMavenIds.forEach(mavenId -> writeNewMavenDependency(model, mavenId, bomVersion != null));
                if (bomVersion != null) {
                    addBomToMavenModule(model, bomVersion);
                }
                notifyAddedDependencies(newMavenIds, project);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) NotificationDisplayType(com.intellij.notification.NotificationDisplayType) MavenDomDependencies(org.jetbrains.idea.maven.dom.model.MavenDomDependencies) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) MavenId(org.jetbrains.idea.maven.model.MavenId) PropertiesFileFlagReader(com.google.cloud.tools.intellij.flags.PropertiesFileFlagReader) NotificationGroup(com.intellij.notification.NotificationGroup) Map(java.util.Map) Project(com.intellij.openapi.project.Project) GctBundle(com.google.cloud.tools.intellij.util.GctBundle) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Module(com.intellij.openapi.module.Module) DomUtil(com.intellij.util.xml.DomUtil) MavenProject(org.jetbrains.idea.maven.project.MavenProject) GctTracking(com.google.cloud.tools.intellij.analytics.GctTracking) CloudLibrary(com.google.cloud.tools.libraries.json.CloudLibrary) Set(java.util.Set) UsageTrackerProvider(com.google.cloud.tools.intellij.analytics.UsageTrackerProvider) Collectors(java.util.stream.Collectors) NotificationType(com.intellij.notification.NotificationType) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) MavenDomUtil(org.jetbrains.idea.maven.dom.MavenDomUtil) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Result(com.intellij.openapi.application.Result) Optional(java.util.Optional) GoogleCloudCoreIcons(com.google.cloud.tools.intellij.GoogleCloudCoreIcons) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Optional(java.util.Optional) List(java.util.List) Map(java.util.Map) Result(com.intellij.openapi.application.Result)

Example 2 with CloudLibrary

use of com.google.cloud.tools.libraries.json.CloudLibrary in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleCloudApiSelectorPanel method onClientLibrarySelection.

private void onClientLibrarySelection(ListSelectionEvent event) {
    ListSelectionModel model = (ListSelectionModel) event.getSource();
    if (!model.isSelectionEmpty()) {
        int selectedIndex = model.getMinSelectionIndex();
        CloudLibrary library = (CloudLibrary) cloudLibrariesTable.getModel().getValueAt(selectedIndex, CLOUD_LIBRARY_COL);
        detailsPanel.setCloudLibrary(library, bomComboBox.getSelectedItem() != null ? bomComboBox.getSelectedItem().toString() : null, apiManagementMap.get(library));
        updateManagementUI();
    }
}
Also used : CloudLibrary(com.google.cloud.tools.libraries.json.CloudLibrary) ListSelectionModel(javax.swing.ListSelectionModel)

Example 3 with CloudLibrary

use of com.google.cloud.tools.libraries.json.CloudLibrary in project google-cloud-intellij by GoogleCloudPlatform.

the class AddCloudLibrariesDialog method doOKAction.

/**
 * Overrides {@link DialogWrapper#doOKAction()} to first check if there are any APIs to enable on
 * GCP.
 *
 * <p>If so, the {@link CloudApiManagementConfirmationDialog} is opened confirming the API changes
 * to be made. If the user cancels, the user is returned to this parent dialog. Otherwise, it is
 * closed and the default {@link DialogWrapper#doOKAction()} is invoked.
 */
@Override
protected void doOKAction() {
    CloudProject cloudProject = getCloudProject();
    Set<CloudLibrary> selectedApis = getSelectedLibraries();
    Set<CloudLibrary> apisToEnable = getApisToEnable();
    Set<CloudLibrary> apisNotEnabled = Sets.difference(selectedApis, apisToEnable);
    if (cloudProject != null) {
        Set<Role> roles = getServiceAccountRoles(selectedApis);
        CloudApiManagementConfirmationDialog managementDialog = new CloudApiManagementConfirmationDialog(getSelectedModule(), cloudProject, apisToEnable, apisNotEnabled, roles);
        DialogManager.show(managementDialog);
        if (managementDialog.isOK()) {
            if (!apisToEnable.isEmpty()) {
                runApiEnablement(apisToEnable);
            }
            if (managementDialog.isCreateNewServiceAccount()) {
                runServiceAccountManagement(managementDialog.getSelectedRoles(), managementDialog.getServiceAccountName(), managementDialog.getServiceAccountKeyDownloadPath());
            }
            super.doOKAction();
        }
    } else {
        super.doOKAction();
    }
}
Also used : Role(com.google.api.services.iam.v1.model.Role) CloudProject(com.google.cloud.tools.intellij.project.CloudProject) CloudLibrary(com.google.cloud.tools.libraries.json.CloudLibrary)

Example 4 with CloudLibrary

use of com.google.cloud.tools.libraries.json.CloudLibrary in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiManager method enableApis.

/**
 * Enables the supplied set of {@link CloudLibrary CloudLibraries} on GCP.
 *
 * <p>Configures the {@link ProgressIndicator} to display the progress of the tasks. Also notifies
 * the user of the success / failure of API enablement via messages on the event log.
 *
 * @param libraries the set of {@link CloudLibrary CloudLibraries} to enable on GCP
 * @param cloudProject the {@link CloudProject} on which to enable the APIs
 * @param project the currently open IntelliJ {@link Project}
 */
static void enableApis(Set<CloudLibrary> libraries, 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;
    }
    List<CloudLibrary> libraryList = new ArrayList<>(libraries);
    Set<CloudLibrary> enabledApis = Sets.newHashSet();
    Set<CloudLibrary> erroredApis = Sets.newHashSet();
    for (int i = 0; i < libraryList.size(); i++) {
        CloudLibrary library = libraryList.get(i);
        try {
            ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
            if (progress.isCanceled()) {
                LOG.info("API enablement canceled by user");
                notifyApiEnableSkipped(Sets.difference(libraries, enabledApis), project);
                return;
            }
            updateProgress(progress, GctBundle.message("cloud.apis.enable.progress.message", library.getName(), cloudProject.projectName()), (double) i / libraryList.size());
            enableApi(library, cloudProject, user.get());
            enabledApis.add(library);
        } catch (IOException e) {
            LOG.warn("Exception occurred attempting to enable API " + library.getName() + " on GCP", e);
            erroredApis.add(library);
        }
    }
    if (!erroredApis.isEmpty()) {
        notifyApiEnableError(erroredApis, project);
    }
    if (!enabledApis.isEmpty()) {
        notifyApisEnabled(enabledApis, cloudProject.projectId(), project);
    }
}
Also used : ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CloudLibrary(com.google.cloud.tools.libraries.json.CloudLibrary) ArrayList(java.util.ArrayList) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) IOException(java.io.IOException)

Example 5 with CloudLibrary

use of com.google.cloud.tools.libraries.json.CloudLibrary in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiManagementConfirmationDialogTest method roleTable_whenRolesExist_isPopulated_andAllSelectedByDefault.

@Test
public void roleTable_whenRolesExist_isPopulated_andAllSelectedByDefault() {
    Set<CloudLibrary> librariesNotToEnable = ImmutableSet.of(TestCloudLibrary.createEmpty().toCloudLibrary());
    Role role1 = new Role();
    role1.setName("my_role");
    role1.setTitle("My Role");
    Role role2 = new Role();
    role2.setName("my_role_2");
    role2.setTitle("My Role 2");
    Set<Role> roles = ImmutableSet.of(role1, role2);
    ApplicationManager.getApplication().invokeAndWait(() -> {
        CloudApiManagementConfirmationDialog dialog = new CloudApiManagementConfirmationDialog(module, cloudProject, ImmutableSet.of(), librariesNotToEnable, roles);
        TableModel model = dialog.getRoleTable().getModel();
        assertThat(model.getRowCount()).isEqualTo(2);
        Set<Role> allValues = ImmutableSet.of((Role) model.getValueAt(0, 0), (Role) model.getValueAt(1, 0));
        assertThat(allValues).containsExactlyElementsIn(roles);
        assertThat(dialog.getSelectedRoles()).containsExactlyElementsIn(roles);
    });
}
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) TableModel(javax.swing.table.TableModel) Test(org.junit.Test)

Aggregations

CloudLibrary (com.google.cloud.tools.libraries.json.CloudLibrary)29 TestCloudLibrary (com.google.cloud.tools.intellij.testing.apis.TestCloudLibrary)25 Test (org.junit.Test)24 JTable (javax.swing.JTable)13 TableModel (javax.swing.table.TableModel)7 Role (com.google.api.services.iam.v1.model.Role)3 Module (com.intellij.openapi.module.Module)3 MavenId (org.jetbrains.idea.maven.model.MavenId)3 CloudProject (com.google.cloud.tools.intellij.project.CloudProject)2 TestCloudLibraryClientMavenCoordinates (com.google.cloud.tools.intellij.testing.apis.TestCloudLibrary.TestCloudLibraryClientMavenCoordinates)2 GoogleCloudCoreIcons (com.google.cloud.tools.intellij.GoogleCloudCoreIcons)1 GctTracking (com.google.cloud.tools.intellij.analytics.GctTracking)1 UsageTrackerProvider (com.google.cloud.tools.intellij.analytics.UsageTrackerProvider)1 PropertiesFileFlagReader (com.google.cloud.tools.intellij.flags.PropertiesFileFlagReader)1 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)1 ProjectSelector (com.google.cloud.tools.intellij.project.ProjectSelector)1 GctBundle (com.google.cloud.tools.intellij.util.GctBundle)1 Notification (com.intellij.notification.Notification)1 NotificationDisplayType (com.intellij.notification.NotificationDisplayType)1 NotificationGroup (com.intellij.notification.NotificationGroup)1