Skip to content

Commit fae7dd0

Browse files
JustinBeckwithAce Nassri
authored and
Ace Nassri
committed
fix: modernize the samples (#240)
1 parent 614f364 commit fae7dd0

16 files changed

+176
-321
lines changed

compute/createVM.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
async function createVM(
17+
vmName = 'new_virtual_machine' // VM name of your choice
18+
) {
19+
const Compute = require('@google-cloud/compute');
20+
const compute = new Compute();
21+
const zone = compute.zone('us-central1-c');
22+
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
23+
console.log(vm);
24+
await operation.promise();
25+
console.log('Virtual machine created!');
26+
}
27+
28+
createVM(...process.argv.slice(2)).catch(console.error);

compute/deleteVM.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
async function deleteVM(
17+
name = 'virtual_machine_name' // VM name of your choice
18+
) {
19+
const Compute = require('@google-cloud/compute');
20+
const compute = new Compute();
21+
const zone = compute.zone('us-central1-c');
22+
const vm = zone.vm(name);
23+
const [operation] = await vm.delete();
24+
await operation.promise();
25+
console.log(`VM deleted!`);
26+
}
27+
28+
deleteVM(...process.argv.slice(2)).catch(console.error);

compute/system-test/vms.test.js renamed to compute/listVMs.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515

1616
'use strict';
1717

18-
const execa = require('execa');
19-
const path = require(`path`);
20-
const {assert} = require('chai');
21-
22-
const cmd = `node vms.js`;
23-
const cwd = path.join(__dirname, `..`);
24-
25-
describe('should retrieve list of vms', () => {
26-
it('vms_inspect_string', async () => {
27-
const {stdout} = await execa.shell(cmd, {cwd});
28-
assert.match(stdout, /^VMs:/);
18+
// [START list]
19+
async function listVMs() {
20+
const Compute = require('@google-cloud/compute');
21+
const compute = new Compute();
22+
const vms = await compute.getVMs({
23+
maxResults: 10,
2924
});
30-
});
25+
console.log(`Found ${vms.length} VMs!`);
26+
vms.forEach(vm => console.log(vm));
27+
}
28+
// [END list]
29+
30+
listVMs().catch(console.error);

compute/mailjet.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ async function mailjet() {
3939
});
4040
console.log(json);
4141
}
42-
mailjet().catch(console.error);
43-
4442
// [END send]
43+
44+
mailjet().catch(console.error);

compute/package.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111
"node": ">=8"
1212
},
1313
"scripts": {
14-
"system-test": "mocha system-test/*.js --timeout 600000",
15-
"startup-test": "mocha startup-script/system-test/*.test.js --timeout 600000",
16-
"test": "npm run system-test && npm run startup-test"
14+
"test": "mocha --timeout 1200000"
1715
},
1816
"dependencies": {
17+
"node-fetch": "^2.3.0",
1918
"@google-cloud/compute": "^0.11.0",
20-
"googleapis": "^36.0.0",
2119
"nodemailer": "^4.3.1",
2220
"nodemailer-smtp-transport": "^2.7.4",
23-
"sendgrid": "^5.2.3",
24-
"uuid": "^3.2.1"
21+
"sendgrid": "^5.2.3"
2522
},
2623
"devDependencies": {
2724
"chai": "^4.2.0",
2825
"execa": "^1.0.0",
2926
"mocha": "^5.0.0",
30-
"proxyquire": "^2.0.1"
27+
"proxyquire": "^2.0.1",
28+
"uuid": "^3.2.1"
3129
}
3230
}

compute/quickstart.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,31 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
/* eslint-disable no-unused-vars */
15-
1614
'use strict';
1715

1816
// [START compute_engine_quickstart]
19-
// Imports the Google Cloud client library
20-
const Compute = require('@google-cloud/compute');
21-
const uuid = require('uuid');
22-
// Creates a client
23-
const compute = new Compute();
17+
async function createVM(
18+
vmName = 'new_virtual_machine' // VM name of your choice
19+
) {
20+
// Imports the Google Cloud client library
21+
const Compute = require('@google-cloud/compute');
2422

25-
// Create a new VM using the latest OS image of your choice.
26-
const zone = compute.zone('us-central1-a');
27-
const name = `ubuntu-http-${uuid().split('-')[0]}`;
23+
// Creates a client
24+
const compute = new Compute();
2825

29-
async function createVM() {
30-
const data = await zone.createVM(name, {os: 'ubuntu'});
26+
// Create a new VM using the latest OS image of your choice.
27+
const zone = compute.zone('us-central1-c');
3128

32-
// `operation` lets you check the status of long-running tasks.
33-
const vm = data[0];
34-
const operation = data[1];
29+
// Start the VM create task
30+
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
31+
console.log(vm);
3532

33+
// `operation` lets you check the status of long-running tasks.
3634
await operation.promise();
37-
// Virtual machine created!
38-
}
39-
40-
createVM().catch(console.error);
4135

36+
// Complete!
37+
console.log('Virtual machine created!');
38+
}
4239
// [END compute_engine_quickstart]
40+
41+
createVM(...process.argv.slice(2)).catch(console.error);

compute/startup-script/README.md

-70
This file was deleted.

compute/startup-script/apache.png

-42.5 KB
Binary file not shown.

compute/startup-script/package.json

-28
This file was deleted.

compute/startup-script/system-test/.eslintrc.yml

-3
This file was deleted.

compute/startup-script/system-test/index.test.js

-40
This file was deleted.

0 commit comments

Comments
 (0)