Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

refactor: simplify samples and sample tests #240

Merged
merged 4 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"docs": "jsdoc -c .jsdoc.js",
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json",
"lint": "eslint '**/*.js'",
"samples-test": "cd samples/ && npm link ../ && cd startup-script && npm link ../../ && cd ../ && npm test && cd ../",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"system-test": "mocha system-test/*.js --timeout 600000",
"test": "nyc mocha",
"fix": "eslint --fix '**/*.js'"
Expand Down
28 changes: 28 additions & 0 deletions samples/createVM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017, 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';

async function createVM(
vmName = 'new_virtual_machine' // VM name of your choice
) {
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!');
}

createVM(...process.argv.slice(2)).catch(console.error);
28 changes: 28 additions & 0 deletions samples/deleteVM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017, 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';

async function deleteVM(
name = 'virtual_machine_name' // VM name of your choice
) {
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!`);
}

deleteVM(...process.argv.slice(2)).catch(console.error);
24 changes: 12 additions & 12 deletions samples/system-test/vms.test.js → samples/listVMs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

'use strict';

const execa = require('execa');
const path = require(`path`);
const {assert} = require('chai');

const cmd = `node vms.js`;
const cwd = path.join(__dirname, `..`);

describe('should retrieve list of vms', () => {
it('vms_inspect_string', async () => {
const {stdout} = await execa.shell(cmd, {cwd});
assert.match(stdout, /^VMs:/);
// [START list]
async function listVMs() {
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));
}
// [END list]

listVMs().catch(console.error);
4 changes: 2 additions & 2 deletions samples/mailjet.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ async function mailjet() {
});
console.log(json);
}
mailjet().catch(console.error);

// [END send]

mailjet().catch(console.error);
12 changes: 5 additions & 7 deletions samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@
"node": ">=8"
},
"scripts": {
"system-test": "mocha system-test/*.js --timeout 600000",
"startup-test": "mocha startup-script/system-test/*.test.js --timeout 600000",
"test": "npm run system-test && npm run startup-test"
"test": "mocha --timeout 1200000"
},
"dependencies": {
"node-fetch": "^2.3.0",
"@google-cloud/compute": "^0.11.0",
"googleapis": "^36.0.0",
"nodemailer": "^4.3.1",
"nodemailer-smtp-transport": "^2.7.4",
"sendgrid": "^5.2.3",
"uuid": "^3.2.1"
"sendgrid": "^5.2.3"
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.0.0",
"proxyquire": "^2.0.1"
"proxyquire": "^2.0.1",
"uuid": "^3.2.1"
}
}
37 changes: 18 additions & 19 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,31 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/* eslint-disable no-unused-vars */

'use strict';

// [START compute_engine_quickstart]
// Imports the Google Cloud client library
const Compute = require('@google-cloud/compute');
const uuid = require('uuid');
// Creates a client
const compute = new Compute();
async function createVM(
vmName = 'new_virtual_machine' // VM name of your choice
) {
// Imports the Google Cloud client library
const Compute = require('@google-cloud/compute');

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

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

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

// `operation` lets you check the status of long-running tasks.
await operation.promise();
// Virtual machine created!
}

createVM().catch(console.error);

// Complete!
console.log('Virtual machine created!');
}
// [END compute_engine_quickstart]

createVM(...process.argv.slice(2)).catch(console.error);
70 changes: 0 additions & 70 deletions samples/startup-script/README.md

This file was deleted.

Binary file removed samples/startup-script/apache.png
Binary file not shown.
28 changes: 0 additions & 28 deletions samples/startup-script/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions samples/startup-script/system-test/.eslintrc.yml

This file was deleted.

40 changes: 0 additions & 40 deletions samples/startup-script/system-test/index.test.js

This file was deleted.

Loading