Java Code Examples for com.google.api.services.cloudresourcemanager.model.ListProjectsResponse#getNextPageToken()

The following examples show how to use com.google.api.services.cloudresourcemanager.model.ListProjectsResponse#getNextPageToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Authenticator.java    From styx with Apache License 2.0 6 votes vote down vote up
void cacheResources() throws IOException {
  final CloudResourceManager.Projects.List request = cloudResourceManager.projects().list();

  ListProjectsResponse response;
  do {
    response = executeWithRetries(request, retryWaitStrategy, retryStopStrategy);
    if (response.getProjects() == null) {
      continue;
    }
    for (Project project : response.getProjects()) {
      final boolean access = resolveProject(project);
      logger.info("Resolved project: {}, access={}", project.getProjectId(), access);
    }
    request.setPageToken(response.getNextPageToken());
  } while (response.getNextPageToken() != null);

  logger.info("Resource cache loaded");
}
 
Example 2
Source File: LiveProjectSource.java    From policyscanner with Apache License 2.0 5 votes vote down vote up
private boolean refreshProjects(String nextPageToken) throws IOException {
  ListProjectsResponse projectListResponse;
  Projects.List projectsList;
  try {
    projectsList = GCPProject.getProjectsApiStub().list();
    if (nextPageToken != null) {
      projectsList = projectsList.setPageToken(nextPageToken);
    }
    if (source.getOrgId() != null) {
        projectsList = projectsList
            .setFilter("parent.type:organization parent.id:" + source.getOrgId());
    }
    projectListResponse = projectsList.execute();
  } catch (GeneralSecurityException gse) {
    throw new IOException("Cannot get projects. Access denied");
  }
  List<Project> projects = projectListResponse.getProjects();

  for (Project project : projects) {
    String orgId = null;
    if (project.getParent() != null) {
      orgId = project.getParent().getId();
    }
    if (project.getLifecycleState() == null
        || project.getLifecycleState().startsWith(DELETE_PREFIX)) {
      continue;
    }
    this.projects.add(new GCPProject(project.getProjectId(), orgId, project.getName()));
  }
  this.nextPageToken = projectListResponse.getNextPageToken();

  return !this.projects.isEmpty();
}
 
Example 3
Source File: ProjectRepository.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * @return all active projects the account identified by {@code credential} has access to
 * @throws ProjectRepositoryException if an error happens while communicating with the backend
 */
public List<GcpProject> getProjects(Credential credential) throws ProjectRepositoryException {
  Preconditions.checkNotNull(credential);
  // TODO cache results https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/1374
  try {
    Projects projects = apiFactory.newProjectsApi(credential);
    
    String token = null;
    List<Project> projectList = new ArrayList<>();
    do {
      Projects.List listRequest = projects.list().setPageSize(PROJECT_LIST_PAGESIZE);
      if (token != null) {
        listRequest = listRequest.setPageToken(token); 
      }
      ListProjectsResponse response = listRequest.execute();
      List<Project> responseProjects = response.getProjects();
      if (responseProjects != null) {
        projectList.addAll(responseProjects);
      }
      token = response.getNextPageToken();
    } while (token != null);
    List<GcpProject> gcpProjects = convertToGcpProjects(projectList);
    return gcpProjects;
  } catch (IOException ex) {
    throw new ProjectRepositoryException(ex);
  }
}