From d55cfd032cb8b253f52450b235ff770a24c71922 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Mon, 29 Apr 2019 18:45:27 -0700 Subject: [PATCH] docs: use repo-metadata to generate README (#289) --- compute/createVM.js | 24 +++++-- compute/deleteVM.js | 28 +++++--- compute/listVMs.js | 26 ++++--- compute/mailjet.js | 7 +- compute/quickstart.js | 32 +++++---- compute/sendgrid.js | 2 +- compute/startupScript.js | 147 ++++++++++++++++++++------------------- 7 files changed, 156 insertions(+), 110 deletions(-) diff --git a/compute/createVM.js b/compute/createVM.js index 788e301e04..dd08047e4f 100644 --- a/compute/createVM.js +++ b/compute/createVM.js @@ -11,18 +11,30 @@ // See the License for the specific language governing permissions and // limitations under the License. +// sample-metadata: +// title: Create VM +// usage: node createVM + 'use strict'; -async function createVM( +async function main( vmName = 'new_virtual_machine' // VM name of your choice ) { + // [START gce_create_vm] const Compute = require('@google-cloud/compute'); const compute = new Compute(); const zone = compute.zone('us-central1-c'); - const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'}); - console.log(vm); - await operation.promise(); - console.log('Virtual machine created!'); + + async function createVM() { + // TODO(developer): provide a name for your VM + // const vmName = 'new-virutal-machine'; + const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'}); + console.log(vm); + await operation.promise(); + console.log('Virtual machine created!'); + } + createVM(); + // [END gce_create_vm] } -createVM(...process.argv.slice(2)).catch(console.error); +main(...process.argv.slice(2)); diff --git a/compute/deleteVM.js b/compute/deleteVM.js index fe88f0dc1a..41d47bdcd2 100644 --- a/compute/deleteVM.js +++ b/compute/deleteVM.js @@ -11,18 +11,30 @@ // See the License for the specific language governing permissions and // limitations under the License. +// sample-metadata: +// title: Delete VM +// usage: node deleteVM + 'use strict'; -async function deleteVM( +async function main( name = 'virtual_machine_name' // VM name of your choice ) { + // [START gce_delete_vm] const Compute = require('@google-cloud/compute'); - const compute = new Compute(); - const zone = compute.zone('us-central1-c'); - const vm = zone.vm(name); - const [operation] = await vm.delete(); - await operation.promise(); - console.log(`VM deleted!`); + + async function deleteVM() { + const compute = new Compute(); + const zone = compute.zone('us-central1-c'); + // TODO(developer): choose a name for the VM to delete + // const name = 'vm-name'; + const vm = zone.vm(name); + const [operation] = await vm.delete(); + await operation.promise(); + console.log(`VM deleted!`); + } + deleteVM(); + // [END gce_delete_vm] } -deleteVM(...process.argv.slice(2)).catch(console.error); +main(...process.argv.slice(2)); diff --git a/compute/listVMs.js b/compute/listVMs.js index b2375a414b..4bf18ee502 100644 --- a/compute/listVMs.js +++ b/compute/listVMs.js @@ -13,18 +13,24 @@ * limitations under the License. */ +// sample-metadata: +// title: List VMs +// usage: node listVMs + 'use strict'; -// [START list] -async function listVMs() { +async function main() { + // [START gce_list_vms] const Compute = require('@google-cloud/compute'); const compute = new Compute(); - const vms = await compute.getVMs({ - maxResults: 10, - }); - console.log(`Found ${vms.length} VMs!`); - vms.forEach(vm => console.log(vm)); + async function listVMs() { + const vms = await compute.getVMs({ + maxResults: 10, + }); + console.log(`Found ${vms.length} VMs!`); + vms.forEach(vm => console.log(vm)); + } + listVMs(); + // [END gce_list_vms] } -// [END list] - -listVMs().catch(console.error); +main().catch(console.error); diff --git a/compute/mailjet.js b/compute/mailjet.js index f0e559f38e..58b9c3bdca 100644 --- a/compute/mailjet.js +++ b/compute/mailjet.js @@ -13,6 +13,10 @@ * limitations under the License. */ +// sample-metadata: +// title: Mailjet +// usage: node mailjet + 'use strict'; // [START send] @@ -39,6 +43,5 @@ async function mailjet() { }); console.log(json); } +mailjet(); // [END send] - -mailjet().catch(console.error); diff --git a/compute/quickstart.js b/compute/quickstart.js index 522d26c4eb..f71b0ed4f3 100644 --- a/compute/quickstart.js +++ b/compute/quickstart.js @@ -13,29 +13,35 @@ 'use strict'; -// [START compute_engine_quickstart] -async function createVM( +async function main( vmName = 'new_virtual_machine' // VM name of your choice ) { + // [START compute_engine_quickstart] // Imports the Google Cloud client library const Compute = require('@google-cloud/compute'); // 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-c'); + async function quickstart() { + // Create a new VM using the latest OS image of your choice. + const zone = compute.zone('us-central1-c'); - // Start the VM create task - const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'}); - console.log(vm); + // TODO(developer): choose a name for the VM + // const vmName = 'vm-name'; - // `operation` lets you check the status of long-running tasks. - await operation.promise(); + // Start the VM create task + const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'}); + console.log(vm); - // Complete! - console.log('Virtual machine created!'); + // `operation` lets you check the status of long-running tasks. + await operation.promise(); + + // Complete! + console.log('Virtual machine created!'); + } + quickstart(); + // [END compute_engine_quickstart] } -// [END compute_engine_quickstart] -createVM(...process.argv.slice(2)).catch(console.error); +main(...process.argv.slice(2)); diff --git a/compute/sendgrid.js b/compute/sendgrid.js index 8b06f4ddb2..87a448ac82 100644 --- a/compute/sendgrid.js +++ b/compute/sendgrid.js @@ -28,5 +28,5 @@ async function sendgridExample() { 'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.', }); } -sendgridExample().catch(console.error); +sendgridExample(); // [END send] diff --git a/compute/startupScript.js b/compute/startupScript.js index e09af659e7..76c1535b96 100644 --- a/compute/startupScript.js +++ b/compute/startupScript.js @@ -15,81 +15,88 @@ 'use strict'; -const Compute = require('@google-cloud/compute'); -const fetch = require('node-fetch'); +async function main(name = 'start-script-example') { + // [START gce_startup_script] + const Compute = require('@google-cloud/compute'); + const fetch = require('node-fetch'); -const compute = new Compute(); -const zone = compute.zone('us-central1-c'); + const compute = new Compute(); + const zone = compute.zone('us-central1-c'); -/** - * Create a new virtual machine with Ubuntu and Apache - * @param {string} name Name of the virtual machine - */ -async function createVMWithStartupScript(name) { - // Create a new VM, using default ubuntu image. The startup script - // installs apache and a custom homepage. - const config = { - os: 'ubuntu', - http: true, - metadata: { - items: [ - { - 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!

`, - }, - ], - }, - }; - - const vm = zone.vm(name); - - console.log(`Creating VM ${name}...`); - const [, operation] = await vm.create(config); - - console.log(`Polling operation ${operation.id}...`); - await operation.promise(); - - console.log('Acquiring VM metadata...'); - const [metadata] = await vm.getMetadata(); - - // 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. - console.log('Operation complete. Waiting for IP'); - await pingVM(ip); - - console.log(`\n${name} created succesfully`); -} + // TODO(developer): choose a name for your virtual machine + // const name = 'your-vm-name'; -/** - * Poll a given IP address until it returns a result. - * @param {string} ip IP address to poll - */ -async function pingVM(ip) { - let exit = false; - while (!exit) { - await new Promise(r => setTimeout(r, 2000)); - try { - const res = await fetch(`http://${ip}`); - if (res.status !== 200) { - throw new Error(res.status); + /** + * Create a new virtual machine with Ubuntu and Apache + * @param {string} name Name of the virtual machine + */ + async function createVMWithStartupScript() { + // Create a new VM, using default ubuntu image. The startup script + // installs apache and a custom homepage. + const config = { + os: 'ubuntu', + http: true, + metadata: { + items: [ + { + 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!

`, + }, + ], + }, + }; + + const vm = zone.vm(name); + + console.log(`Creating VM ${name}...`); + const [, operation] = await vm.create(config); + + console.log(`Polling operation ${operation.id}...`); + await operation.promise(); + + console.log('Acquiring VM metadata...'); + const [metadata] = await vm.getMetadata(); + + // 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. + console.log('Operation complete. Waiting for IP'); + await pingVM(ip); + + console.log(`\n${name} created succesfully`); + } + + /** + * Poll a given IP address until it returns a result. + * @param {string} ip IP address to poll + */ + async function pingVM(ip) { + let exit = false; + while (!exit) { + await new Promise(r => setTimeout(r, 2000)); + try { + const res = await fetch(`http://${ip}`); + if (res.status !== 200) { + throw new Error(res.status); + } + exit = true; + } catch (err) { + process.stdout.write('.'); } - exit = true; - } catch (err) { - process.stdout.write('.'); } } + + createVMWithStartupScript(); } -const args = process.argv.slice(2); -createVMWithStartupScript(...args).catch(console.error); +main(...process.argv.slice(2));