Skip to content

Commit 7e005ea

Browse files
docs: use repo-metadata to generate README (#289)
1 parent b5d5872 commit 7e005ea

File tree

7 files changed

+156
-110
lines changed

7 files changed

+156
-110
lines changed

compute/createVM.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
// sample-metadata:
15+
// title: Create VM
16+
// usage: node createVM <vmName>
17+
1418
'use strict';
1519

16-
async function createVM(
20+
async function main(
1721
vmName = 'new_virtual_machine' // VM name of your choice
1822
) {
23+
// [START gce_create_vm]
1924
const Compute = require('@google-cloud/compute');
2025
const compute = new Compute();
2126
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!');
27+
28+
async function createVM() {
29+
// TODO(developer): provide a name for your VM
30+
// const vmName = 'new-virutal-machine';
31+
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
32+
console.log(vm);
33+
await operation.promise();
34+
console.log('Virtual machine created!');
35+
}
36+
createVM();
37+
// [END gce_create_vm]
2638
}
2739

28-
createVM(...process.argv.slice(2)).catch(console.error);
40+
main(...process.argv.slice(2));

compute/deleteVM.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
// sample-metadata:
15+
// title: Delete VM
16+
// usage: node deleteVM <vmName>
17+
1418
'use strict';
1519

16-
async function deleteVM(
20+
async function main(
1721
name = 'virtual_machine_name' // VM name of your choice
1822
) {
23+
// [START gce_delete_vm]
1924
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!`);
25+
26+
async function deleteVM() {
27+
const compute = new Compute();
28+
const zone = compute.zone('us-central1-c');
29+
// TODO(developer): choose a name for the VM to delete
30+
// const name = 'vm-name';
31+
const vm = zone.vm(name);
32+
const [operation] = await vm.delete();
33+
await operation.promise();
34+
console.log(`VM deleted!`);
35+
}
36+
deleteVM();
37+
// [END gce_delete_vm]
2638
}
2739

28-
deleteVM(...process.argv.slice(2)).catch(console.error);
40+
main(...process.argv.slice(2));

compute/listVMs.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@
1313
* limitations under the License.
1414
*/
1515

16+
// sample-metadata:
17+
// title: List VMs
18+
// usage: node listVMs
19+
1620
'use strict';
1721

18-
// [START list]
19-
async function listVMs() {
22+
async function main() {
23+
// [START gce_list_vms]
2024
const Compute = require('@google-cloud/compute');
2125
const compute = new Compute();
22-
const vms = await compute.getVMs({
23-
maxResults: 10,
24-
});
25-
console.log(`Found ${vms.length} VMs!`);
26-
vms.forEach(vm => console.log(vm));
26+
async function listVMs() {
27+
const vms = await compute.getVMs({
28+
maxResults: 10,
29+
});
30+
console.log(`Found ${vms.length} VMs!`);
31+
vms.forEach(vm => console.log(vm));
32+
}
33+
listVMs();
34+
// [END gce_list_vms]
2735
}
28-
// [END list]
29-
30-
listVMs().catch(console.error);
36+
main().catch(console.error);

compute/mailjet.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16+
// sample-metadata:
17+
// title: Mailjet
18+
// usage: node mailjet
19+
1620
'use strict';
1721

1822
// [START send]
@@ -39,6 +43,5 @@ async function mailjet() {
3943
});
4044
console.log(json);
4145
}
46+
mailjet();
4247
// [END send]
43-
44-
mailjet().catch(console.error);

compute/quickstart.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,35 @@
1313

1414
'use strict';
1515

16-
// [START compute_engine_quickstart]
17-
async function createVM(
16+
async function main(
1817
vmName = 'new_virtual_machine' // VM name of your choice
1918
) {
19+
// [START compute_engine_quickstart]
2020
// Imports the Google Cloud client library
2121
const Compute = require('@google-cloud/compute');
2222

2323
// Creates a client
2424
const compute = new Compute();
2525

26-
// Create a new VM using the latest OS image of your choice.
27-
const zone = compute.zone('us-central1-c');
26+
async function quickstart() {
27+
// Create a new VM using the latest OS image of your choice.
28+
const zone = compute.zone('us-central1-c');
2829

29-
// Start the VM create task
30-
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
31-
console.log(vm);
30+
// TODO(developer): choose a name for the VM
31+
// const vmName = 'vm-name';
3232

33-
// `operation` lets you check the status of long-running tasks.
34-
await operation.promise();
33+
// Start the VM create task
34+
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
35+
console.log(vm);
3536

36-
// Complete!
37-
console.log('Virtual machine created!');
37+
// `operation` lets you check the status of long-running tasks.
38+
await operation.promise();
39+
40+
// Complete!
41+
console.log('Virtual machine created!');
42+
}
43+
quickstart();
44+
// [END compute_engine_quickstart]
3845
}
39-
// [END compute_engine_quickstart]
4046

41-
createVM(...process.argv.slice(2)).catch(console.error);
47+
main(...process.argv.slice(2));

compute/sendgrid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ async function sendgridExample() {
2828
'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.',
2929
});
3030
}
31-
sendgridExample().catch(console.error);
31+
sendgridExample();
3232
// [END send]

compute/startupScript.js

Lines changed: 77 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,81 +15,88 @@
1515

1616
'use strict';
1717

18-
const Compute = require('@google-cloud/compute');
19-
const fetch = require('node-fetch');
18+
async function main(name = 'start-script-example') {
19+
// [START gce_startup_script]
20+
const Compute = require('@google-cloud/compute');
21+
const fetch = require('node-fetch');
2022

21-
const compute = new Compute();
22-
const zone = compute.zone('us-central1-c');
23+
const compute = new Compute();
24+
const zone = compute.zone('us-central1-c');
2325

24-
/**
25-
* Create a new virtual machine with Ubuntu and Apache
26-
* @param {string} name Name of the virtual machine
27-
*/
28-
async function createVMWithStartupScript(name) {
29-
// Create a new VM, using default ubuntu image. The startup script
30-
// installs apache and a custom homepage.
31-
const config = {
32-
os: 'ubuntu',
33-
http: true,
34-
metadata: {
35-
items: [
36-
{
37-
key: 'startup-script',
38-
value: `#! /bin/bash
39-
40-
# Installs apache and a custom homepage
41-
apt-get update
42-
apt-get install -y apache2
43-
cat <<EOF > /var/www/html/index.html
44-
<!doctype html>
45-
<h1>Hello World</h1>
46-
<p>This page was created from a simple start-up script!</p>`,
47-
},
48-
],
49-
},
50-
};
51-
52-
const vm = zone.vm(name);
53-
54-
console.log(`Creating VM ${name}...`);
55-
const [, operation] = await vm.create(config);
56-
57-
console.log(`Polling operation ${operation.id}...`);
58-
await operation.promise();
59-
60-
console.log('Acquiring VM metadata...');
61-
const [metadata] = await vm.getMetadata();
62-
63-
// External IP of the VM.
64-
const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP;
65-
console.log(`Booting new VM with IP http://${ip}...`);
66-
67-
// Ping the VM to determine when the HTTP server is ready.
68-
console.log('Operation complete. Waiting for IP');
69-
await pingVM(ip);
70-
71-
console.log(`\n${name} created succesfully`);
72-
}
26+
// TODO(developer): choose a name for your virtual machine
27+
// const name = 'your-vm-name';
7328

74-
/**
75-
* Poll a given IP address until it returns a result.
76-
* @param {string} ip IP address to poll
77-
*/
78-
async function pingVM(ip) {
79-
let exit = false;
80-
while (!exit) {
81-
await new Promise(r => setTimeout(r, 2000));
82-
try {
83-
const res = await fetch(`http://${ip}`);
84-
if (res.status !== 200) {
85-
throw new Error(res.status);
29+
/**
30+
* Create a new virtual machine with Ubuntu and Apache
31+
* @param {string} name Name of the virtual machine
32+
*/
33+
async function createVMWithStartupScript() {
34+
// Create a new VM, using default ubuntu image. The startup script
35+
// installs apache and a custom homepage.
36+
const config = {
37+
os: 'ubuntu',
38+
http: true,
39+
metadata: {
40+
items: [
41+
{
42+
key: 'startup-script',
43+
value: `#! /bin/bash
44+
45+
# Installs apache and a custom homepage
46+
apt-get update
47+
apt-get install -y apache2
48+
cat <<EOF > /var/www/html/index.html
49+
<!doctype html>
50+
<h1>Hello World</h1>
51+
<p>This page was created from a simple start-up script!</p>`,
52+
},
53+
],
54+
},
55+
};
56+
57+
const vm = zone.vm(name);
58+
59+
console.log(`Creating VM ${name}...`);
60+
const [, operation] = await vm.create(config);
61+
62+
console.log(`Polling operation ${operation.id}...`);
63+
await operation.promise();
64+
65+
console.log('Acquiring VM metadata...');
66+
const [metadata] = await vm.getMetadata();
67+
68+
// External IP of the VM.
69+
const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP;
70+
console.log(`Booting new VM with IP http://${ip}...`);
71+
72+
// Ping the VM to determine when the HTTP server is ready.
73+
console.log('Operation complete. Waiting for IP');
74+
await pingVM(ip);
75+
76+
console.log(`\n${name} created succesfully`);
77+
}
78+
79+
/**
80+
* Poll a given IP address until it returns a result.
81+
* @param {string} ip IP address to poll
82+
*/
83+
async function pingVM(ip) {
84+
let exit = false;
85+
while (!exit) {
86+
await new Promise(r => setTimeout(r, 2000));
87+
try {
88+
const res = await fetch(`http://${ip}`);
89+
if (res.status !== 200) {
90+
throw new Error(res.status);
91+
}
92+
exit = true;
93+
} catch (err) {
94+
process.stdout.write('.');
8695
}
87-
exit = true;
88-
} catch (err) {
89-
process.stdout.write('.');
9096
}
9197
}
98+
99+
createVMWithStartupScript();
92100
}
93101

94-
const args = process.argv.slice(2);
95-
createVMWithStartupScript(...args).catch(console.error);
102+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)