use of com.google.api.services.iam.v1.Iam 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.api.services.iam.v1.Iam in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudApiManager method createServiceAccountKey.
/**
* Using the supplied {@link ServiceAccount}, this creates and returns a new {@link
* ServiceAccountKey}.
*/
private static ServiceAccountKey createServiceAccountKey(CredentialedUser user, ServiceAccount serviceAccount) throws IOException {
Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.getCredential());
CreateServiceAccountKeyRequest keyRequest = new CreateServiceAccountKeyRequest();
return iam.projects().serviceAccounts().keys().create(serviceAccount.getName(), keyRequest).execute();
}
use of com.google.api.services.iam.v1.Iam 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.api.services.iam.v1.Iam in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudApiManager method createServiceAccount.
/**
* Creates a new {@link ServiceAccount} for the given {@link CloudProject} using the IAM API.
*/
private static ServiceAccount createServiceAccount(CredentialedUser user, String name, CloudProject cloudProject) throws IOException {
CreateServiceAccountRequest request = new CreateServiceAccountRequest();
ServiceAccount serviceAccount = new ServiceAccount();
serviceAccount.setDisplayName(name);
request.setServiceAccount(serviceAccount);
request.setAccountId(createServiceAccountId(name));
Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.getCredential());
return iam.projects().serviceAccounts().create(String.format(SERVICE_ACCOUNT_CREATE_REQUEST_PROJECT_FORMAT, cloudProject.projectId()), request).execute();
}
Aggregations