From 41c76ec16c95c65d457bab2c589d4f55761b0092 Mon Sep 17 00:00:00 2001 From: muraliQlogic <38901089+muraliQlogic@users.noreply.github.com> Date: Sun, 4 Nov 2018 04:43:24 +0530 Subject: [PATCH] docs(samples): update samples to use async/await and mocha (#183) --- compute/mailjet.js | 38 ++-- compute/package.json | 10 +- compute/quickstart.js | 33 ++-- compute/sendgrid.js | 52 +++--- compute/startup-script/index.js | 163 ++++++++---------- compute/startup-script/package.json | 10 +- .../startup-script/system-test/.eslintrc.yml | 2 + .../startup-script/system-test/index.test.js | 42 ++--- compute/system-test/.eslintrc.yml | 2 + compute/system-test/vms.test.js | 19 +- compute/system-test/vms_api.test.js | 18 +- compute/test/.eslintrc.yml | 2 + compute/test/mailjet.test.js | 63 +++---- compute/test/sendgrid.test.js | 67 +++---- compute/vms.js | 22 +-- compute/vms_api.js | 84 ++++----- 16 files changed, 287 insertions(+), 340 deletions(-) diff --git a/compute/mailjet.js b/compute/mailjet.js index 800f79a3b6..3adf5b6103 100644 --- a/compute/mailjet.js +++ b/compute/mailjet.js @@ -19,30 +19,26 @@ const mailer = require('nodemailer'); const smtp = require('nodemailer-smtp-transport'); -const transport = mailer.createTransport( - smtp({ - host: 'in.mailjet.com', - port: 2525, - auth: { - user: process.env.MAILJET_API_KEY || '', - }, - }) -); +async function mailjet() { + const transport = mailer.createTransport( + smtp({ + host: 'in.mailjet.com', + port: 2525, + auth: { + user: process.env.MAILJET_API_KEY || '', + }, + }) + ); -transport.sendMail( - { + const json = await transport.sendMail({ from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', // From address to: 'EMAIL@EXAMPLE.COM', // To address subject: 'test email from Node.js on Google Cloud Platform', // Subject text: 'Hello!\n\nThis a test email from Node.js.', // Content - }, - function(err, json) { - if (err) { - console.log(err); - } else { - console.log(json); - } - } -); + }); + console.log(json); +} +mailjet().catch(console.error); + // [END send] diff --git a/compute/package.json b/compute/package.json index 3f2cc398e6..c3901c257a 100644 --- a/compute/package.json +++ b/compute/package.json @@ -9,20 +9,22 @@ "node": ">=8" }, "scripts": { - "system-test": "ava -T 20s --verbose system-test/*.test.js", - "startup-test": "ava -T 200s --verbose startup-script/system-test/*.test.js", + "system-test": "mocha system-test/*.js --timeout 600000", + "startup-test": "mocha startup-script/system-test/*.test.js --timeout 600000", "test": "npm run system-test && npm run startup-test" }, "dependencies": { "@google-cloud/compute": "^0.10.0", "googleapis": "^34.0.0", + "node-fetch": "^2.2.0", "nodemailer": "^4.3.1", "nodemailer-smtp-transport": "^2.7.4", - "sendgrid": "^5.2.3" + "sendgrid": "^5.2.3", + "uuid": "^3.2.1" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^2.3.1", - "ava": "^0.25.0", + "mocha": "^5.0.0", "proxyquire": "^2.0.1" } } diff --git a/compute/quickstart.js b/compute/quickstart.js index ae0ba47968..a65c414f9d 100644 --- a/compute/quickstart.js +++ b/compute/quickstart.js @@ -18,26 +18,25 @@ // [START compute_engine_quickstart] // Imports the Google Cloud client library const Compute = require('@google-cloud/compute'); - +const uuid = require('uuid'); // Creates a client const compute = new Compute(); // Create a new VM using the latest OS image of your choice. const zone = compute.zone('us-central1-a'); -const name = 'ubuntu-http'; - -zone - .createVM(name, {os: 'ubuntu'}) - .then(data => { - // `operation` lets you check the status of long-running tasks. - const vm = data[0]; - const operation = data[1]; - return operation.promise(); - }) - .then(() => { - // Virtual machine created! - }) - .catch(err => { - console.error('ERROR:', err); - }); +const name = `ubuntu-http-${uuid().split('-')[0]}`; + +async function createVM() { + const data = await zone.createVM(name, {os: 'ubuntu'}); + + // `operation` lets you check the status of long-running tasks. + const vm = data[0]; + const operation = data[1]; + + await operation.promise(); + // Virtual machine created! +} + +createVM().catch(console.error); + // [END compute_engine_quickstart] diff --git a/compute/sendgrid.js b/compute/sendgrid.js index 63ffad3b8a..2013785759 100644 --- a/compute/sendgrid.js +++ b/compute/sendgrid.js @@ -19,33 +19,31 @@ const Sendgrid = require('sendgrid')( process.env.SENDGRID_API_KEY || '' ); -const request = Sendgrid.emptyRequest({ - method: 'POST', - path: '/v3/mail/send', - body: { - personalizations: [ - { - to: [{email: 'to_email@example.com'}], - subject: 'Sendgrid test email from Node.js on Google Cloud Platform', - }, - ], - from: {email: 'from_email@example.com'}, - content: [ - { - type: 'text/plain', - value: - 'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.', - }, - ], - }, -}); +async function sendgrid() { + const request = Sendgrid.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: { + personalizations: [ + { + to: [{email: 'to_email@example.com'}], + subject: 'Sendgrid test email from Node.js on Google Cloud Platform', + }, + ], + from: {email: 'from_email@example.com'}, + content: [ + { + type: 'text/plain', + value: + 'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.', + }, + ], + }, + }); -Sendgrid.API(request, function(error, response) { - if (error) { - console.log('Mail not sent; see error message below.'); - } else { - console.log('Mail sent successfully!'); - } + const response = await Sendgrid.API(request); console.log(response); -}); +} +sendgrid().catch(console.error); + // [END send] diff --git a/compute/startup-script/index.js b/compute/startup-script/index.js index b9e9849cbb..b3b9e2836a 100644 --- a/compute/startup-script/index.js +++ b/compute/startup-script/index.js @@ -16,16 +16,16 @@ 'use strict'; const Compute = require('@google-cloud/compute'); -const http = require('http'); +const fetch = require('node-fetch'); const compute = new Compute(); const zone = compute.zone('us-central1-a'); -// callback(error, externalIp) -function createVm(name, callback) { +async function createVm(name) { // Create a new VM, using default ubuntu image. The startup script // installs apache and a custom homepage. + const config = { os: 'ubuntu', http: true, @@ -34,112 +34,91 @@ function createVm(name, callback) { { key: 'startup-script', value: `#! /bin/bash - - # Installs apache and a custom homepage - apt-get update - apt-get install -y apache2 - cat < /var/www/html/index.html - -

Hello World

-

This page was created from a simple start-up script!

`, + + # Installs apache and a custom homepage + apt-get update + apt-get install -y apache2 + cat < /var/www/html/index.html + +

Hello World

+

This page was created from a simple start-up script!

`, }, ], }, }; + const vmObj = zone.vm(name); + console.log('Creating VM ...'); + const [vm, operation] = await vmObj.create(config); + await operation.promise(); + const [metadata] = await vm.getMetadata(); - const vm = zone.vm(name); - - vm.create(config) - .then(data => { - const operation = data[1]; - return operation.promise(); - }) - .then(() => { - return vm.getMetadata(); - }) - .then(data => { - const metadata = data[0]; - - // External IP of the VM. - const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP; - console.log(`Booting new VM with IP http://${ip}...`); + // External IP of the VM. + const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP; + console.log(`Booting new VM with IP http://${ip}...`); - // Ping the VM to determine when the HTTP server is ready. - let waiting = true; - const timer = setInterval( - ip => { - http - .get('http://' + ip, res => { - const statusCode = res.statusCode; - if (statusCode === 200 && waiting) { - waiting = false; - clearTimeout(timer); - // HTTP server is ready. - console.log('Ready!'); - callback(null, ip); - } - }) - .on('error', () => { - // HTTP server is not ready yet. - process.stdout.write('.'); - }); - }, - 2000, - ip - ); - }) - .catch(err => callback(err)); + // Ping the VM to determine when the HTTP server is ready. + await pingVM(ip); + return ip; } +async function pingVM(ip) { + let waiting = true; + while (waiting) { + await new Promise(r => setTimeout(r, 2000)); + try { + const res = await fetch(`http://${ip}`); + const statusCode = res.status; + if (statusCode === 200) { + waiting = false; + console.log('Ready!'); + return; + } else { + process.stdout.write('.'); + } + } catch (err) { + process.stdout.write('.'); + } + } +} // List all VMs and their external IPs in a given zone. -// callback(error, [[name, ip], [name, ip], ...]) -function listVms(callback) { - zone - .getVMs() - .then(data => { - const vms = data[0]; - const results = vms.map(vm => vm.getMetadata()); - return Promise.all(results); +async function listVms() { + const [vms] = await zone.getVMs(); + return await Promise.all( + vms.map(async vm => { + const [metadata] = await vm.getMetadata(); + return { + ip: metadata['networkInterfaces'][0]['accessConfigs'] + ? metadata['networkInterfaces'][0]['accessConfigs'][0]['natIP'] + : 'no external ip', + name: metadata.name, + }; }) - .then(res => - callback( - null, - res.map(data => { - return { - ip: data[0]['networkInterfaces'][0]['accessConfigs'] - ? data[0]['networkInterfaces'][0]['accessConfigs'][0]['natIP'] - : 'no external ip', - name: data[0].name, - }; - }) - ) - ) - .catch(err => callback(err)); + ); } -function deleteVm(name, callback) { +async function deleteVm(name) { const vm = zone.vm(name); - vm.delete() - .then(data => { - console.log('Deleting ...'); - const operation = data[0]; - return operation.promise(); - }) - .then(() => { - // VM deleted - callback(null, name); - }) - .catch(err => callback(err)); + console.log('Deleting ...'); + const [operation] = await vm.delete(); + await operation.promise(); + // VM deleted + return name; } -exports.create = (name, cb) => { - createVm(name, cb); +exports.create = async name => { + const ip = await createVm(name); + console.log(`${name} created succesfully`); + return ip; }; -exports.list = cb => { - listVms(cb); +exports.list = async () => { + const vms = await listVms(); + console.log(vms); + return vms; }; -exports.delete = (name, cb) => { - deleteVm(name, cb); +exports.delete = async name => { + const result = await deleteVm(name); + console.log(`${name} deleted succesfully`); + return result; }; diff --git a/compute/startup-script/package.json b/compute/startup-script/package.json index c8e13f7c8a..6ba8aace99 100644 --- a/compute/startup-script/package.json +++ b/compute/startup-script/package.json @@ -4,15 +4,19 @@ "description": "Start a Google Compute Engine and run the startup script.", "main": "index.js", "dependencies": { - "@google-cloud/compute": "0.10.0" + "@google-cloud/compute": "0.10.0", + "node-fetch": "^2.2.0" + }, + "engines": { + "node": ">=8" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^2.3.0", - "ava": "^0.25.0", + "mocha": "^5.0.0", "uuid": "^3.2.1" }, "scripts": { - "test": "ava -T 600s --verbose system-test/*.test.js", + "test": "mocha system-test/*.js --timeout 600000", "start": "node -e 'require(\"./index.js\").create(\"vm-with-apache\", console.log)'", "delete": "node -e 'require(\"./index.js\").delete(\"vm-with-apache\", console.log)'", "list": "node -e 'require(\"./index.js\").list(console.log)'" diff --git a/compute/startup-script/system-test/.eslintrc.yml b/compute/startup-script/system-test/.eslintrc.yml index c0289282a6..752164361d 100644 --- a/compute/startup-script/system-test/.eslintrc.yml +++ b/compute/startup-script/system-test/.eslintrc.yml @@ -1,4 +1,6 @@ --- +env: + mocha: true rules: node/no-unpublished-require: off node/no-unsupported-features: off diff --git a/compute/startup-script/system-test/index.test.js b/compute/startup-script/system-test/index.test.js index c53657af7c..824106322d 100644 --- a/compute/startup-script/system-test/index.test.js +++ b/compute/startup-script/system-test/index.test.js @@ -15,26 +15,17 @@ 'use strict'; -const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); const uuid = require('uuid'); +const assert = require('assert'); const example = require(`../index`); -test.before(tools.checkCredentials); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +describe('start-up script', async () => { + before(tools.checkCredentials); + beforeEach(tools.stubConsole); + afterEach(tools.restoreConsole); -test.cb(`should list vms`, t => { - example.list((err, result) => { - t.ifError(err); - t.truthy(result); - t.true(Array.isArray(result)); - t.end(); - }); -}); - -test.cb(`should create vm`, t => { const TESTS_PREFIX = 'gcloud-tests-'; const name = generateName('vm-with-apache'); @@ -43,16 +34,17 @@ test.cb(`should create vm`, t => { .join('') .substr(0, 61); } - - example.create(name, (err, result) => { - t.ifError(err); - t.truthy(result); - - // Clean up newly created vm. - example.delete(name, (err, result) => { - t.ifError(err); - t.truthy(result); - t.end(); - }); + it('should create vm', async () => { + const ip = await example.create(name); + assert.ok(ip); + }); + it('should list vms', async () => { + const vms = await example.list(); + assert.ok(vms); + assert.strictEqual(Array.isArray(vms), true); + }); + it('should delete vm', async () => { + const result = await example.delete(name); + assert.strictEqual(result, name); }); }); diff --git a/compute/system-test/.eslintrc.yml b/compute/system-test/.eslintrc.yml index c0289282a6..752164361d 100644 --- a/compute/system-test/.eslintrc.yml +++ b/compute/system-test/.eslintrc.yml @@ -1,4 +1,6 @@ --- +env: + mocha: true rules: node/no-unpublished-require: off node/no-unsupported-features: off diff --git a/compute/system-test/vms.test.js b/compute/system-test/vms.test.js index 8a45dd63ad..16a5365647 100644 --- a/compute/system-test/vms.test.js +++ b/compute/system-test/vms.test.js @@ -15,20 +15,17 @@ 'use strict'; -const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); +const path = require(`path`); +const assert = require('assert'); -const vmsExample = require(`../vms`); +const cmd = `node vms.js`; +const cwd = path.join(__dirname, `..`); -test.before(tools.checkCredentials); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +describe('should retrieve list of vms', function() { + it('vms_inspect_string', async function() { + const output = await tools.runAsync(cmd, cwd); -test.cb(`should retrieve vms`, t => { - vmsExample.main((err, result) => { - t.ifError(err); - t.truthy(result); - t.true(Array.isArray(result)); - t.end(); + assert.strictEqual(output.includes('VMs:'), true); }); }); diff --git a/compute/system-test/vms_api.test.js b/compute/system-test/vms_api.test.js index e7b762fdfb..026ee14965 100644 --- a/compute/system-test/vms_api.test.js +++ b/compute/system-test/vms_api.test.js @@ -15,19 +15,17 @@ 'use strict'; -const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); +const path = require(`path`); +const assert = require('assert'); -const vmsExample = require(`../vms_api`); +const cmd = `node vms_api.js`; +const cwd = path.join(__dirname, `..`); -test.before(tools.checkCredentials); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +describe('should retrieve list of vms via api', function() { + it('vms_api_inspect_string', async function() { + const output = await tools.runAsync(cmd, cwd); -test.cb('should retrieve vms', t => { - vmsExample.main((err, result) => { - t.ifError(err); - t.truthy(result); - t.end(); + assert.strictEqual(output.includes('VMs:'), true); }); }); diff --git a/compute/test/.eslintrc.yml b/compute/test/.eslintrc.yml index 43b4c19e59..2385deb8e0 100644 --- a/compute/test/.eslintrc.yml +++ b/compute/test/.eslintrc.yml @@ -1,4 +1,6 @@ --- +env: + mocha: true rules: node/no-unpublished-require: off node/no-unsupported-features: off diff --git a/compute/test/mailjet.test.js b/compute/test/mailjet.test.js index 38df808602..c562e8ae05 100644 --- a/compute/test/mailjet.test.js +++ b/compute/test/mailjet.test.js @@ -16,44 +16,45 @@ 'use strict'; const proxyquire = require(`proxyquire`).noPreserveCache(); -const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); +const assert = require('assert'); process.env.MAILJET_API_KEY = `foo`; process.env.MAILJET_API_SECRET = `bar`; -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +describe('mailjet', () => { + beforeEach(tools.stubConsole); + afterEach(tools.restoreConsole); -test.cb(`should send an email`, t => { - proxyquire(`../mailjet`, { - nodemailer: { - createTransport: arg => { - t.is(arg, `test`); - return { - sendMail: (payload, cb) => { - t.deepEqual(payload, { - from: `ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM`, - to: `EMAIL@EXAMPLE.COM`, - subject: `test email from Node.js on Google Cloud Platform`, - text: `Hello!\n\nThis a test email from Node.js.`, - }); - cb(null, `done`); - t.end(); + it('should send an email', () => { + proxyquire(`../mailjet`, { + nodemailer: { + createTransport: arg => { + assert.strictEqual(arg, `test`); + return { + sendMail: payload => { + assert.deepStrictEqual(payload, { + from: `ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM`, + to: `EMAIL@EXAMPLE.COM`, + subject: `test email from Node.js on Google Cloud Platform`, + text: `Hello!\n\nThis a test email from Node.js.`, + }); + return `done`; + }, + }; + }, + }, + 'nodemailer-smtp-transport': options => { + assert.deepStrictEqual(options, { + host: `in.mailjet.com`, + port: 2525, + auth: { + user: `foo`, + pass: `bar`, }, - }; + }); + return `test`; }, - }, - 'nodemailer-smtp-transport': options => { - t.deepEqual(options, { - host: `in.mailjet.com`, - port: 2525, - auth: { - user: `foo`, - pass: `bar`, - }, - }); - return `test`; - }, + }); }); }); diff --git a/compute/test/sendgrid.test.js b/compute/test/sendgrid.test.js index a0162036b8..641032ca72 100644 --- a/compute/test/sendgrid.test.js +++ b/compute/test/sendgrid.test.js @@ -16,43 +16,44 @@ 'use strict'; const proxyquire = require(`proxyquire`).noPreserveCache(); -const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); +const assert = require('assert'); process.env.SENDGRID_API_KEY = `foo`; -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +describe('sendgrid', () => { + beforeEach(tools.stubConsole); + afterEach(tools.restoreConsole); -test.cb(`should send an email`, t => { - proxyquire(`../sendgrid`, { - sendgrid: key => { - t.is(key, `foo`); - return { - emptyRequest: x => x, - API: request => { - t.deepEqual(request, { - method: `POST`, - path: `/v3/mail/send`, - body: { - personalizations: [ - { - to: [{email: `to_email@example.com`}], - subject: `Sendgrid test email from Node.js on Google Cloud Platform`, - }, - ], - from: {email: `from_email@example.com`}, - content: [ - { - type: `text/plain`, - value: `Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.`, - }, - ], - }, - }); - t.end(); - }, - }; - }, + it('should send an email', () => { + proxyquire(`../sendgrid`, { + sendgrid: key => { + assert.strictEqual(key, `foo`); + return { + emptyRequest: x => x, + API: request => { + assert.deepStrictEqual(request, { + method: `POST`, + path: `/v3/mail/send`, + body: { + personalizations: [ + { + to: [{email: `to_email@example.com`}], + subject: `Sendgrid test email from Node.js on Google Cloud Platform`, + }, + ], + from: {email: `from_email@example.com`}, + content: [ + { + type: `text/plain`, + value: `Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.`, + }, + ], + }, + }); + }, + }; + }, + }); }); }); diff --git a/compute/vms.js b/compute/vms.js index 6188927a94..4023f0ad5c 100644 --- a/compute/vms.js +++ b/compute/vms.js @@ -30,29 +30,23 @@ const compute = new Compute(); // [END initialize] // [START list] -/** - * @param {Function} callback Callback function. - */ -function getVmsExample(callback) { + +async function getVmsExample() { // In this example we only want one VM per page const options = { maxResults: 1, }; - compute.getVMs(options, (err, vms) => { - if (err) { - return callback(err); - } - - console.log('VMs:', vms); - callback(null, vms); - }); + const vms = await compute.getVMs(options); + return vms; } // [END list] // [END complete] // Run the examples -exports.main = cb => { - getVmsExample(cb); +exports.main = async () => { + const vms = await getVmsExample().catch(console.error); + if (vms) console.log('VMs:', vms); + return vms; }; if (module === require.main) { diff --git a/compute/vms_api.js b/compute/vms_api.js index aff335a621..c92b16f1d9 100644 --- a/compute/vms_api.js +++ b/compute/vms_api.js @@ -22,32 +22,29 @@ const compute = google.compute('v1'); // [END initialize] // [START auth] -function auth(callback) { - google.auth.getApplicationDefault(function(err, authClient) { - if (err) { - return callback(err); - } +async function auth() { + const data = await google.auth.getApplicationDefault(); + let authClient = data.credential; + const projectId = authClient.projectId; - const projectId = authClient.projectId; + // The createScopedRequired method returns true when running on GAE or a + // local developer machine. In that case, the desired scopes must be passed + // in manually. When the code is running in GCE or GAE Flexible, the scopes + // are pulled from the GCE metadata server. + // See https://cloud.google.com/compute/docs/authentication for more + // information. + if (authClient.createScopedRequired && authClient.createScopedRequired()) { + // Scopes can be specified either as an array or as a single, + // space-delimited string. + authClient = authClient.createScoped([ + 'https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/compute', + 'https://www.googleapis.com/auth/compute.readonly', + ]); + authClient.projectId = projectId; + } - // The createScopedRequired method returns true when running on GAE or a - // local developer machine. In that case, the desired scopes must be passed - // in manually. When the code is running in GCE or GAE Flexible, the scopes - // are pulled from the GCE metadata server. - // See https://cloud.google.com/compute/docs/authentication for more - // information. - if (authClient.createScopedRequired && authClient.createScopedRequired()) { - // Scopes can be specified either as an array or as a single, - // space-delimited string. - authClient = authClient.createScoped([ - 'https://www.googleapis.com/auth/cloud-platform', - 'https://www.googleapis.com/auth/compute', - 'https://www.googleapis.com/auth/compute.readonly', - ]); - authClient.projectId = projectId; - } - callback(null, authClient); - }); + return authClient; } // [END auth] @@ -55,38 +52,21 @@ function auth(callback) { /** * @param {Function} callback Callback function. */ -function getVmsExample(callback) { - auth(function(err, authClient) { - if (err) { - return callback(err); - } - // Retrieve the vms - compute.instances.aggregatedList( - { - auth: authClient, - project: authClient.projectId, - // In this example we only want one VM per page - maxResults: 1, - }, - function(err, vms) { - if (err) { - return callback(err); - } +async function getVmsExample() { + const authClient = await auth(); - console.log('VMs:', vms); - callback(null, vms); - } - ); + // Retrieve the vms + const vms = await compute.instances.aggregatedList({ + auth: authClient, + project: authClient.projectId, + // In this example we only want one VM per page + maxResults: 1, }); + console.log('VMs:', vms); + return vms; } // [END list] // [END complete] // Run the examples -exports.main = function(cb) { - getVmsExample(cb); -}; - -if (module === require.main) { - exports.main(console.log); -} +getVmsExample().catch(console.error);