|
| 1 | +// Copyright 2017, Google LLC All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +if ( |
| 18 | + !process.env.GCLOUD_PROJECT || |
| 19 | + !process.env.GOOGLE_APPLICATION_CREDENTIALS |
| 20 | +) { |
| 21 | + throw new Error( |
| 22 | + 'Usage: GCLOUD_PROJECT=<project_id> GOOGLE_APPLICATION_CREDENTIALS=<path to json key> node #{$0}' |
| 23 | + ); |
| 24 | +} |
| 25 | +var projectId = process.env.GCLOUD_PROJECT; |
| 26 | + |
| 27 | +const dataproc = require('../src'); |
| 28 | + |
| 29 | +var client = new dataproc.v1.ClusterControllerClient({ |
| 30 | + // optional auth parameters. |
| 31 | +}); |
| 32 | + |
| 33 | +// Iterate over all elements. |
| 34 | +var projectId2 = projectId; |
| 35 | +var region = 'global'; |
| 36 | +var request = { |
| 37 | + projectId: projectId2, |
| 38 | + region: region, |
| 39 | +}; |
| 40 | + |
| 41 | +client.listClusters(request).then(responses => { |
| 42 | + var resources = responses[0]; |
| 43 | + console.log('Total resources:', resources.length); |
| 44 | + for (let i = 0; i < resources.length; i += 1) { |
| 45 | + console.log(resources[i]); |
| 46 | + } |
| 47 | +}); |
| 48 | + |
| 49 | +// Or obtain the paged response. |
| 50 | +var options = {autoPaginate: false}; |
| 51 | +var callback = responses => { |
| 52 | + // The actual resources in a response. |
| 53 | + var resources = responses[0]; |
| 54 | + // The next request if the response shows that there are more responses. |
| 55 | + var nextRequest = responses[1]; |
| 56 | + // The actual response object, if necessary. |
| 57 | + // var rawResponse = responses[2]; |
| 58 | + for (let i = 0; i < resources.length; i += 1) { |
| 59 | + console.log(resources[i]); |
| 60 | + } |
| 61 | + if (nextRequest) { |
| 62 | + // Fetch the next page. |
| 63 | + return client.listClusters(nextRequest, options).then(callback); |
| 64 | + } |
| 65 | +}; |
| 66 | +client.listClusters(request, options).then(callback); |
| 67 | + |
| 68 | +client.listClustersStream(request).on('data', element => { |
| 69 | + console.log(element); |
| 70 | +}); |
0 commit comments