* Prints a list of all non-deprecated image names available in a given project, divided into pages as returned by the Compute Engine API. * * @param {string} projectId - ID or number of the project you want to list images from * @param {number} pageSize - size of the pages you want the API to ret
(projectId, pageSize = 10)
| 19 | * @param {number} pageSize - size of the pages you want the API to return on each call. |
| 20 | */ |
| 21 | function main(projectId, pageSize = 10) { |
| 22 | // [START compute_images_list_page] |
| 23 | /** |
| 24 | * TODO(developer): Uncomment and replace these variables before running the sample. |
| 25 | */ |
| 26 | // const projectId = 'YOUR_PROJECT_ID'; |
| 27 | // const pageSize = 10; |
| 28 | |
| 29 | const compute = require('@google-cloud/compute'); |
| 30 | |
| 31 | async function listImagesByPage() { |
| 32 | const imagesClient = new compute.ImagesClient(); |
| 33 | |
| 34 | // Listing only non-deprecated images to reduce the size of the reply. |
| 35 | const listRequest = { |
| 36 | project: projectId, |
| 37 | maxResults: pageSize, |
| 38 | filter: 'deprecated.state != DEPRECATED', |
| 39 | }; |
| 40 | |
| 41 | const options = { |
| 42 | autoPaginate: false, |
| 43 | }; |
| 44 | |
| 45 | let pageNum = 1; |
| 46 | |
| 47 | // Set autoPaginate option to `false` to have more granular control of |
| 48 | // iteration over paginated results from the API. Each time you want to access the |
| 49 | // next page, the library retrieves that page from the API. |
| 50 | const listCallback = (err, resources, nextPageRequest, response) => { |
| 51 | if (err) { |
| 52 | console.error(err); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | console.log(`Page ${pageNum}:`); |
| 57 | pageNum += 1; |
| 58 | |
| 59 | for (let i = 0; i < resources.length; i++) { |
| 60 | console.log(resources[i].name); |
| 61 | } |
| 62 | |
| 63 | if (response.nextPageToken) { |
| 64 | imagesClient.list(nextPageRequest, options, listCallback); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | imagesClient.list(listRequest, options, listCallback); |
| 69 | } |
| 70 | |
| 71 | listImagesByPage(); |
| 72 | // [END compute_images_list_page] |
| 73 | } |
| 74 | |
| 75 | main(...process.argv.slice(2)); |
no test coverage detected