/** * Updates component UI and state with the new project and user account information. * * @param cloudProject New project/account information, null to clear selected project. */ public void setSelectedProject(CloudProject cloudProject) { this.cloudProject = cloudProject; if (cloudProject == null || Strings.isNullOrEmpty(cloudProject.googleUsername())) { updateEmptySelection(); } else { updateCloudProjectSelection(cloudProject); } }
@VisibleForTesting void setSelectedProject(CloudProject cloudProject) { if (cloudProject != null && !Strings.isNullOrEmpty(cloudProject.googleUsername())) { Optional<CredentialedUser> loggedInUser = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (loggedInUser.isPresent()) { selectedProjectsByAccount.put(loggedInUser.get(), cloudProject.projectName()); accountComboBox.setSelectedItem(loggedInUser.get()); } } else { // no project set, show active user projects by default. accountComboBox.setSelectedItem(Services.getLoginService().getActiveUser()); } }
private void loadRepositories(@Nullable Runnable onComplete) { if (cloudProject == null) { return; } Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (!user.isPresent()) { return; } if (repositories == null) { repositories = new ProjectRepositoriesModelItem(); } repositories.loadRepositories(cloudProject.projectId(), user.get(), onComplete); }
private void updateCloudProjectSelection(CloudProject selection) { projectNameLabel.setHyperlinkText(selection.projectName()); projectAccountSeparatorLabel.setVisible(true); // first just show account email, then expand with name/picture if this account is signed in. // if not signed in, hide icon and account name completely and prompt to sign in. accountInfoLabel.setHyperlinkText(selection.googleUsername()); IntegratedGoogleLoginService loginService = Services.getLoginService(); Optional<CredentialedUser> loggedInUser = loginService.getLoggedInUser(selection.googleUsername()); if (!loggedInUser.isPresent()) { accountInfoLabel.setHyperlinkText( GoogleCloudCoreMessageBundle.message( "cloud.project.selector.not.signed.in", selection.googleUsername())); } else if (loggedInUser.isPresent()) { accountInfoLabel.setHyperlinkText( String.format( "%s (%s)", Strings.nullToEmpty(loggedInUser.get().getName()), loggedInUser.get().getEmail())); } accountInfoLabel.setIcon( loginService.isLoggedIn() ? GoogleLoginIcons.getScaledUserIcon(ACCOUNT_ICON_SIZE, loggedInUser.orElse(null)) : null); }
private void mockUserLoggedIn(String username) { when(mockLoginService.isLoggedIn()).thenReturn(true); CredentialedUser mockUser = mock(CredentialedUser.class); when(mockLoginService.getLoggedInUser(TEST_PROJECT.googleUsername())) .thenReturn(Optional.of(mockUser)); when(mockUser.getEmail()).thenReturn(username); } }
@Nullable private Debugger getCloudDebuggerClient() { CloudProject cloudProject = projectSelector.getSelectedProject(); CredentialedUser credentialedUser = cloudProject == null ? null : Services.getLoginService() .getLoggedInUser(cloudProject.googleUsername()) .orElse(null); if (this.credentialedUser == credentialedUser) { return cloudDebuggerClient; } this.credentialedUser = credentialedUser; cloudDebuggerClient = this.credentialedUser != null ? CloudDebuggerClient.getLongTimeoutClient(this.credentialedUser.getEmail()) : null; return cloudDebuggerClient; }
@Nullable private String getCurrentUrlText() { CloudProject selectedProject = projectSelector.getSelectedProject(); Optional<CredentialedUser> selectedUser = selectedProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()); if (selectedProject == null || !selectedUser.isPresent() || StringUtil.isEmpty(repositorySelector.getText())) { return null; } return GcpHttpAuthDataProvider.getGcpUrl( selectedProject.projectId(), repositorySelector.getText()); }
void setActiveCloudProject( @NotNull CloudProject activeCloudProject, @NotNull Project ideProject) { PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(ideProject); propertiesComponent.setValue(PROJECT_ACCOUNT_KEY, activeCloudProject.googleUsername()); propertiesComponent.setValue(PROJECT_NAME_KEY, activeCloudProject.projectName()); propertiesComponent.setValue(PROJECT_ID_KEY, activeCloudProject.projectId()); if (activeCloudProject.projectNumber() != null) { propertiesComponent.setValue( PROJECT_NUMBER_KEY, activeCloudProject.projectNumber().toString()); } }
/** * Shared implementation of {@link * com.intellij.openapi.options.SettingsEditor#applyEditorTo(Object)}. To be invoked by users of * this panel in the overriden method. */ public void applyEditorTo(@NotNull AppEngineDeploymentConfiguration configuration) { configuration.setVersion(versionIdField.getText()); configuration.setPromote(promoteCheckbox.isSelected()); configuration.setStopPreviousVersion(stopPreviousVersionCheckbox.isSelected()); configuration.setDeployAllConfigs(deployAllConfigsCheckbox.isSelected()); CloudProject selectedProject = projectSelector.getSelectedProject(); if (selectedProject != null) { configuration.setCloudProjectName(selectedProject.projectId()); configuration.setGoogleUsername(selectedProject.googleUsername()); } }
@Override protected void doOKAction() { CloudProject selectedProject = projectSelector.getSelectedProject(); projectId = selectedProject.projectId(); repositoryId = repositorySelector.getText(); remoteName = remoteNameSelector.getText(); credentialedUser = Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()).orElse(null); super.doOKAction(); } }
/** * Fetches the list of {@link Role} for the supplied {@link CloudProject} by querying the Iam API. */ static List<Role> getServiceAccountRoles(CloudProject cloudProject) { Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (!user.isPresent()) { LOG.error("Cannot fetch service account roles: logged in user not found."); return ImmutableList.of(); } Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.get().getCredential()); try { return iam.roles().list().execute().getRoles(); } catch (IOException e) { LOG.warn("Exception occurred attempting to fetch service account roles"); return ImmutableList.of(); } }
/** * Updates the text of the panel as follows: if no project is selected, no message is displayed, * if the project represents a valid project, the project details are displayed, if the project * represents an invalid project, an error message is displayed. */ private void refreshApplicationInfoPanel(CloudProject selectedProject) { if (selectedProject == null) { applicationInfoPanel.clearMessage(); return; } Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()); if (user.isPresent()) { applicationInfoPanel.refresh(selectedProject.projectId(), user.get().getCredential()); } else { applicationInfoPanel.setMessage( AppEngineMessageBundle.getString("appengine.infopanel.no.region"), true /* isError*/); } }
void refresh() { showNotificationPanel(); if (!Services.getLoginService().isLoggedIn()) { notificationLabel.setText( GoogleCloudStorageMessageBundle.message("gcs.panel.bucket.listing.not.logged.in")); } else if (projectSelector.getSelectedProject() == null) { notificationLabel.setText( GoogleCloudStorageMessageBundle.message("gcs.panel.bucket.listing.no.project.selected")); } else { CloudProject cloudProject = projectSelector.getSelectedProject(); Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (user.isPresent()) { loadAndDisplayBuckets(cloudProject.projectId(), user.get()); } else { notificationLabel.setText( GoogleCloudStorageMessageBundle.message( "gcs.panel.bucket.listing.error.loading.buckets")); log.warn("Cloud not load credentialed user for GCS operation. User may not be logged."); } } }
@Test public void validProject_no_LoggedInUsers_showLoginPrompt() { when(mockLoginService.isLoggedIn()).thenReturn(false); projectSelector.setSelectedProject(TEST_PROJECT); assertThat(projectSelector.getAccountInfoLabel().getHyperlinkText()) .isEqualTo( GoogleCloudCoreMessageBundle.message( "cloud.project.selector.not.signed.in", TEST_PROJECT.googleUsername())); assertThat(projectSelector.getAccountInfoLabel().getIcon()).isNull(); }
@Test public void validProject_newLogIn_changesTo_AccountInformation() { when(mockLoginService.isLoggedIn()).thenReturn(false); projectSelector.setSelectedProject(TEST_PROJECT); // log in now, fire event mockUserLoggedIn(TEST_PROJECT.googleUsername()); projectSelector.googleLoginListener.statusChanged(); // drain UI events. ApplicationManager.getApplication().invokeAndWait(() -> {}); verifyUiStateForProject(TEST_PROJECT); }
@Override public void showPopup(RelativePoint showTarget) { Optional<CredentialedUser> user = cloudProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (user.isPresent()) { if (popup == null || popup.isDisposed()) { panel = new RepositoryPanel(); ComponentPopupBuilder popupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null); popup = popupBuilder.createPopup(); } if (!popup.isVisible()) { popup.show(showTarget); } } else { panel = new ProjectNotSelectedPanel(); ComponentPopupBuilder popupBuilder = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, null); popup = popupBuilder.createPopup(); popup.show(showTarget); } }
selectedProject == null ? Optional.empty() : Services.getLoginService().getLoggedInUser(selectedProject.googleUsername());
@Test public void validProject_logOut_changesTo_loginPrompt() { projectSelector.setSelectedProject(TEST_PROJECT); // log out all users, notify login listeners. when(mockLoginService.isLoggedIn()).thenReturn(false); projectSelector.googleLoginListener.statusChanged(); // drain UI events. ApplicationManager.getApplication().invokeAndWait(() -> {}); assertThat(projectSelector.getAccountInfoLabel().getHyperlinkText()) .isEqualTo( GoogleCloudCoreMessageBundle.message( "cloud.project.selector.not.signed.in", TEST_PROJECT.googleUsername())); assertThat(projectSelector.getAccountInfoLabel().getIcon()).isNull(); }
private void verifyUiStateForProject(CloudProject project) { if (project == null) { assertThat(projectSelector.getProjectNameLabel().getHyperlinkText()) .isEqualTo( GoogleCloudCoreMessageBundle.getString("cloud.project.selector.no.selected.project")); // no account information UI is visible/populated. assertThat(projectSelector.getProjectAccountSeparatorLabel().isVisible()).isFalse(); assertThat(projectSelector.getAccountInfoLabel().getHyperlinkText()).isEmpty(); assertThat(projectSelector.getAccountInfoLabel().getIcon()).isNull(); } else { assertThat(projectSelector.getProjectNameLabel().getHyperlinkText()) .isEqualTo(project.projectName()); assertThat(projectSelector.getProjectAccountSeparatorLabel().isVisible()).isTrue(); assertThat(projectSelector.getAccountInfoLabel().getHyperlinkText()) .contains(project.googleUsername()); assertThat(projectSelector.getAccountInfoLabel().getIcon()).isNotNull(); } }
Services.getLoginService().getLoggedInUser(selectedProject.googleUsername());