Skip to content

Instantly share code, notes, and snippets.

@beckyconning
Last active December 18, 2018 11:44
Show Gist options
  • Save beckyconning/42a1fd25866e84f375b81bea0b39ce70 to your computer and use it in GitHub Desktop.
Save beckyconning/42a1fd25866e84f375b81bea0b39ce70 to your computer and use it in GitHub Desktop.
{
"name": "cloud-functions-schedule-instance",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=6.0"
},
"scripts": {
"lint": "semistandard '**/*.js'",
"pretest": "npm run lint",
"test": "ava -T 20s --verbose test/*.test.js"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.2.5",
"ava": "^0.25.0",
"proxyquire": "^2.0.0",
"sinon": "^4.4.2"
},
"dependencies": {
"@google-cloud/compute": "^0.10.0",
"safe-buffer": "5.1.2"
}
}
const Buffer = require('safe-buffer').Buffer;
const Compute = require('@google-cloud/compute');
const compute = new Compute();
/**
* Starts a Compute Engine instance.
*
* Expects a PubSub message with JSON-formatted event data containing the
* following attributes:
* zone - the GCP zone the instance is located in.
* instance - the name of the instance.
*
* @param {!object} event Cloud Function PubSub message event.
* @param {!object} callback Cloud Function PubSub callback indicating completion.
*/
exports.startInstancePubSub = (event, callback) => {
try {
const pubsubMessage = event.data;
const payload = _validatePayload(JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()));
compute.zone(payload.zone)
.vm(payload.instance)
.start()
.then(data => {
// Operation pending.
const operation = data[0];
return operation.promise();
})
.then(() => {
// Operation complete. Instance successfully started.
const message = 'Successfully started instance ' + payload.instance;
console.log(message);
callback(null, message);
})
.catch(err => {
console.log(err);
callback(err);
});
} catch (err) {
console.log(err);
callback(err);
}
};
/**
* Validates that a request payload contains the expected fields.
*
* @param {!object} payload the request payload to validate.
* @returns {!object} the payload object.
*/
function _validatePayload (payload) {
if (!payload.zone) {
throw new Error(`Attribute 'zone' missing from payload`);
} else if (!payload.instance) {
throw new Error(`Attribute 'instance' missing from payload`);
}
return payload;
}
const Buffer = require('safe-buffer').Buffer;
const Compute = require('@google-cloud/compute');
const compute = new Compute();
/**
* Stops a Compute Engine instance.
*
* Expects a PubSub message with JSON-formatted event data containing the
* following attributes:
* zone - the GCP zone the instance is located in.
* instance - the name of the instance.
*
* @param {!object} event Cloud Function PubSub message event.
* @param {!object} callback Cloud Function PubSub callback indicating completion.
*/
exports.stopInstancePubSub = (event, callback) => {
try {
const pubsubMessage = event.data;
const payload = _validatePayload(JSON.parse(Buffer.from(pubsubMessage.data, 'base64').toString()));
compute.zone(payload.zone)
.vm(payload.instance)
.stop()
.then(data => {
// Operation pending.
const operation = data[0];
return operation.promise();
})
.then(() => {
// Operation complete. Instance successfully stopped.
const message = 'Successfully stopped instance ' + payload.instance;
console.log(message);
callback(null, message);
})
.catch(err => {
console.log(err);
callback(err);
});
} catch (err) {
console.log(err);
callback(err);
}
};
/**
* Validates that a request payload contains the expected fields.
*
* @param {!object} payload the request payload to validate.
* @returns {!object} the payload object.
*/
function _validatePayload (payload) {
if (!payload.zone) {
throw new Error(`Attribute 'zone' missing from payload`);
} else if (!payload.instance) {
throw new Error(`Attribute 'instance' missing from payload`);
}
return payload;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment