Search in sources :

Example 1 with ListProjectsResponse

use of com.google.api.services.cloudresourcemanager.model.ListProjectsResponse in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectLoader method loadUserProjects.

private List<Project> loadUserProjects(CredentialedUser user) throws IOException {
    CloudResourceManager cloudResourceManagerClient = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential());
    final List<Project> result = new ArrayList<>();
    ListProjectsResponse response = cloudResourceManagerClient.projects().list().setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
    if (response != null && response.getProjects() != null) {
        // Create a sorted set to sort the projects list by project name.
        Set<Project> allProjects = new TreeSet<>(Comparator.comparing(project -> project.getName().toLowerCase()));
        allProjects.addAll(response.getProjects());
        while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
            response = cloudResourceManagerClient.projects().list().setPageToken(response.getNextPageToken()).setPageSize(PROJECTS_MAX_PAGE_SIZE).execute();
            allProjects.addAll(response.getProjects());
        }
        allProjects.stream().filter((project) -> !PROJECT_DELETE_REQUESTED.equals(project.getLifecycleState())).filter((project) -> !Strings.isNullOrEmpty(project.getProjectId())).forEach(result::add);
    }
    return result;
}
Also used : Project(com.google.api.services.cloudresourcemanager.model.Project) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Set(java.util.Set) ThreadUtil(com.google.cloud.tools.intellij.util.ThreadUtil) IOException(java.io.IOException) GoogleApiClientFactory(com.google.cloud.tools.intellij.resources.GoogleApiClientFactory) FutureCallback(com.google.common.util.concurrent.FutureCallback) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) List(java.util.List) Strings(com.google.api.client.repackaged.com.google.common.base.Strings) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) Comparator(java.util.Comparator) ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse) Project(com.google.api.services.cloudresourcemanager.model.Project) CloudResourceManager(com.google.api.services.cloudresourcemanager.CloudResourceManager) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)

Example 2 with ListProjectsResponse

use of com.google.api.services.cloudresourcemanager.model.ListProjectsResponse in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectLoaderTest method mockListProjectsResponse.

/**
 * Mocks the {@link ListProjectsResponse} returned by the {@link #mockCloudResourceManager}.
 *
 * @param projects the list of {@link Project Projects} to return as part of the {@link
 *     ListProjectsResponse}
 */
private void mockListProjectsResponse(List<Project> projects) {
    when(mockCloudResourceManager.projects()).thenReturn(mockProjects);
    try {
        when(mockProjects.list()).thenReturn(mockList);
        when(mockList.setPageSize(any())).thenReturn(mockList);
        when(mockList.setPageToken(any())).thenReturn(mockList);
        ListProjectsResponse response = new ListProjectsResponse();
        response.setProjects(projects);
        response.setNextPageToken("");
        when(mockList.execute()).thenReturn(response);
    } catch (IOException e) {
        // Should not happen when setting up these mocks.
        throw new AssertionError(e);
    }
}
Also used : ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse) IOException(java.io.IOException)

Example 3 with ListProjectsResponse

use of com.google.api.services.cloudresourcemanager.model.ListProjectsResponse in project google-cloud-intellij by GoogleCloudPlatform.

the class ProjectLoaderTest method mockTwoPageProjectsRespose.

/**
 * Mocks {@link ListProjectsResponse} to contain two pages, one test project per each, using the
 * specified token.
 *
 * @param token token to identify second page.
 * @return Argument captor to check which tokens are requested.
 */
private ArgumentCaptor<String> mockTwoPageProjectsRespose(String token) throws IOException {
    mockListProjectsResponse(null);
    // emulate page with size 1, one token returned, then completed.
    ListProjectsResponse firstResponse = new ListProjectsResponse();
    firstResponse.setProjects(Collections.singletonList(testProject1));
    firstResponse.setNextPageToken(token);
    ListProjectsResponse secondResponse = new ListProjectsResponse();
    secondResponse.setProjects(Collections.singletonList(testProject2));
    secondResponse.setNextPageToken("");
    ArgumentCaptor<String> pageTokenCaptor = ArgumentCaptor.forClass(String.class);
    when(mockList.setPageToken(pageTokenCaptor.capture())).thenReturn(mockList);
    when(mockList.execute()).thenReturn(firstResponse, secondResponse);
    return pageTokenCaptor;
}
Also used : ListProjectsResponse(com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)

Aggregations

ListProjectsResponse (com.google.api.services.cloudresourcemanager.model.ListProjectsResponse)3 IOException (java.io.IOException)2 Strings (com.google.api.client.repackaged.com.google.common.base.Strings)1 CloudResourceManager (com.google.api.services.cloudresourcemanager.CloudResourceManager)1 Project (com.google.api.services.cloudresourcemanager.model.Project)1 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)1 GoogleApiClientFactory (com.google.cloud.tools.intellij.resources.GoogleApiClientFactory)1 ThreadUtil (com.google.cloud.tools.intellij.util.ThreadUtil)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1