use of com.google.cloud.tools.intellij.login.CredentialedUser 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.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudApiManager method getServiceAccountRoles.
/**
* 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();
}
}
use of com.google.cloud.tools.intellij.login.CredentialedUser 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();
}
use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectDebuggeeBinding method getCloudDebuggerClient.
@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;
}
use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class GoogleLoginAction method update.
@Override
public void update(AnActionEvent e) {
Presentation presentation = e.getPresentation();
CredentialedUser activeUser = Services.getLoginService().getActiveUser();
if (activeUser == null) {
presentation.setText(SIGN_IN_MESSAGE);
} else {
presentation.setText(activeUser.getEmail());
}
presentation.setIcon(GoogleLoginIcons.getScaledUserIcon(ICON_SIZE, activeUser));
}
Aggregations