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();
}
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();
}
}
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();
}
}
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);
}
}
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);
});
}
Aggregations