Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): rework the compute list vms sample #1534

Merged
merged 1 commit into from Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion samples/compute/README.md
Expand Up @@ -6,4 +6,4 @@ The Google Compute Engine Metadata service lets you get and set key/value pairs

## Running the sample

`node compute.js`
`node listVMs.js`
38 changes: 0 additions & 38 deletions samples/compute/compute.js

This file was deleted.

48 changes: 48 additions & 0 deletions 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);
22 changes: 22 additions & 0 deletions 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:/);
});
});