diff --git a/package.json b/package.json index e37da98228..0d23e9bae9 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ }, "devDependencies": { "@compodoc/compodoc": "^1.1.7", + "@types/chai": "^4.1.7", "@types/execa": "^0.9.0", "@types/minimist": "^1.2.0", "@types/mkdirp": "^0.5.2", @@ -74,6 +75,7 @@ "@types/url-template": "^2.0.28", "assert-rejects": "^1.0.0", "axios": "^0.18.0", + "chai": "^4.2.0", "codecov": "^3.0.2", "eslint": "^5.6.0", "eslint-config-prettier": "^3.0.1", diff --git a/samples/compute/README.md b/samples/compute/README.md index 0643c4eaec..af9faf2680 100644 --- a/samples/compute/README.md +++ b/samples/compute/README.md @@ -6,4 +6,4 @@ The Google Compute Engine Metadata service lets you get and set key/value pairs ## Running the sample -`node compute.js` \ No newline at end of file +`node listVMs.js` diff --git a/samples/compute/compute.js b/samples/compute/compute.js deleted file mode 100644 index 5d3558735d..0000000000 --- a/samples/compute/compute.js +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2013-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -'use strict'; - -const axios = require('axios'); -const {google} = require('googleapis'); -const compute = google.compute('v1'); - -// This example can be run from a GCE VM in your project. The example fetches -// your project ID from the VM's metadata server, and then uses the Compute API -// to fetch the list of GCE zones in your project. -// -// See the defaultauth.js sample for an alternate way of fetching compute credentials. -// -google.options({auth: new google.auth.Compute()}); -async function getZones() { - // Get the projectId from the GCE metadata server - const url = 'http://metadata/computeMetadata/v1/project/project-id'; - const headers = {'Metadata-Flavor': 'Google'}; - const res = await axios.get({url, headers}); - const project = res.data; - - const zonesResponse = await compute.zones.list({project}); - console.log(zonesResponse.data); -} - -getZones().catch(console.error); diff --git a/samples/compute/listVMs.js b/samples/compute/listVMs.js new file mode 100644 index 0000000000..fd6a861875 --- /dev/null +++ b/samples/compute/listVMs.js @@ -0,0 +1,48 @@ +/** + * Copyright 2019, Google, LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START complete] +// [START initialize] +const {google} = require('googleapis'); +const compute = google.compute('v1'); +// [END initialize] + +async function listVMs() { + // [START auth] + const authClient = await google.auth.getClient({ + scopes: [ + 'https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/compute', + 'https://www.googleapis.com/auth/compute.readonly', + ], + }); + // [END auth] + + // [START list] + const projectId = await google.auth.getProjectId(); + const result = await compute.instances.aggregatedList({ + auth: authClient, + project: projectId, + }); + const vms = result.data; + console.log('VMs:', vms); + // [END list] +} +// [END complete] + +// Run the examples +listVMs().catch(console.error); diff --git a/test/samples/test.compute.samples.ts b/test/samples/test.compute.samples.ts new file mode 100644 index 0000000000..028a2b6b6e --- /dev/null +++ b/test/samples/test.compute.samples.ts @@ -0,0 +1,22 @@ +// Copyright 2019, Google, LLC. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {assert} from 'chai'; +import {shellSync} from 'execa'; + +describe('Compute samples', () => { + it('should list all the VMs', async () => { + const res = shellSync('node ../samples/compute/listVMs'); + assert.match(res.stdout, /VMs:/); + }); +});