use of com.google.cloud.compute.v1.ImagesClient.ListPage in project java-docs-samples by GoogleCloudPlatform.
the class ListImages method listImagesByPage.
// [END compute_images_list]
// [START compute_images_list_page]
// Prints a list of all non-deprecated image names available in a given project,
// divided into pages as returned by the Compute Engine API.
public static void listImagesByPage(String project, int pageSize) throws IOException {
// safely clean up any remaining background resources.
try (ImagesClient imagesClient = ImagesClient.create()) {
// Listing only non-deprecated images to reduce the size of the reply.
ListImagesRequest imagesRequest = ListImagesRequest.newBuilder().setProject(project).setMaxResults(pageSize).setFilter("deprecated.state != DEPRECATED").build();
// Use the `iteratePages` attribute of returned iterable to have more granular control of
// iteration over paginated results from the API. Each time you want to access the
// next page, the library retrieves that page from the API.
int pageNumber = 1;
for (ListPage page : imagesClient.list(imagesRequest).iteratePages()) {
System.out.println("Page Number: " + pageNumber++);
for (Image image : page.getValues()) {
System.out.println(image.getName());
}
}
}
}
Aggregations