Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused HTTP functions from scheduleinstance code sample #854

Merged
merged 20 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
14badec
NodeJS 6 code sample for Cloud Function to start/stop GCE instances
djmailhot Oct 31, 2018
3bb9df7
start/stop GCE instances Cloud Function updated with 400 status codes
djmailhot Oct 31, 2018
1c3740e
start/stop GCE instances Cloud Function update octet-stream tests
djmailhot Oct 31, 2018
e450d81
start/stop GCE instances Cloud Function linting updates
djmailhot Oct 31, 2018
be15496
start/stop GCE instances Cloud Function package.json updates
djmailhot Oct 31, 2018
49d9c54
Add .kokoro build file for functions/scheduleinstance
djmailhot Nov 1, 2018
29c123f
Added Apache license text to top of source files
djmailhot Nov 2, 2018
d5faf18
Added README file
djmailhot Nov 2, 2018
47599ff
Merge branch 'master' into master
djmailhot Nov 2, 2018
df6f89f
Add PubSub functions to scheduleinstance function
djmailhot Nov 5, 2018
2dde553
Update package.json for scheduleinstance sample code
djmailhot Nov 5, 2018
9ca252a
Merge remote-tracking branch 'upstream/master' into HEAD
djmailhot Nov 5, 2018
77a6495
Fix linting errors for scheduleinstance code sample
djmailhot Nov 5, 2018
e55d41a
Merge branch 'master' into master
fhinkel Nov 6, 2018
92e8447
Merge branch 'master' into master
fhinkel Nov 6, 2018
94d8a3f
Update scheduleinstance code sample README to have useful doc links
djmailhot Nov 6, 2018
68f2d03
Merge remote-tracking branch 'origin/master'
djmailhot Nov 6, 2018
cf83acd
Merge remote-tracking branch 'upstream/master'
djmailhot Nov 9, 2018
c7b9130
scheduleinstance code samples remove unused HTTP functions
djmailhot Nov 9, 2018
6916f77
Merge branch 'master' into master
fhinkel Nov 10, 2018
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
10 changes: 4 additions & 6 deletions functions/scheduleinstance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ See the [Scheduling Instances with Cloud Scheduler tutorial][tutorial].

## Additional resources

* [Cloud Scheduler documentation][docs]
* [HTTP Cloud Functions documentation][http_docs]
* [HTTP Cloud Functions tutorial][http_tutorial]
* [GCE NodeJS Client Library documentation][compute_nodejs_docs]
* [Background Cloud Functions documentation][background_functions_docs]

[docs]: https://cloud.google.com/scheduler/docs/
[http_docs]: https://cloud.google.com/functions/docs/writing/http
[http_tutorial]: https://cloud.google.com/functions/docs/tutorials/http
[compute_nodejs_docs]: https://cloud.google.com/compute/docs/tutorials/nodejs-guide
[background_functions_docs]: https://cloud.google.com/functions/docs/writing/background
132 changes: 2 additions & 130 deletions functions/scheduleinstance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,101 +13,13 @@
* limitations under the License.
*/

// [START functions_start_instance_http]
// [START functions_stop_instance_http]
// [START functions_start_instance_pubsub]
// [START functions_stop_instance_pubsub]
const Buffer = require('safe-buffer').Buffer;
const Compute = require('@google-cloud/compute');
const compute = new Compute();

// [END functions_stop_instance_http]
// [END functions_start_instance_pubsub]
// [END functions_stop_instance_pubsub]
/**
* Starts a Compute Engine instance.
*
* Expects an HTTP POST request with a JSON-formatted request body containing
* the following attributes:
* zone - the GCP zone the instance is located in.
* instance - the name of the instance.
*
* @param {!object} req Cloud Function HTTP request data.
* @param {!object} res Cloud Function HTTP response data.
* @returns {!object} Cloud Function response data with status code and message.
*/
exports.startInstanceHttp = (req, res) => {
try {
const payload = _validatePayload(_parseHttpPayload(_validateHttpReq(req)));
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);
res.status(200).send(message);
})
.catch(err => {
console.log(err);
res.status(500).send({error: err.message});
});
} catch (err) {
console.log(err);
res.status(400).send({error: err.message});
}
return res;
};

// [END functions_start_instance_http]
// [START functions_stop_instance_http]
/**
* Stops a Compute Engine instance.
*
* Expects an HTTP POST request with a JSON-formatted request body containing
* the following attributes:
* zone - the GCP zone the instance is located in.
* instance - the name of the instance.
*
* @param {!object} req Cloud Function HTTP request data.
* @param {!object} res Cloud Function HTTP response data.
* @returns {!object} Cloud Function response data with status code and message.
*/
exports.stopInstanceHttp = (req, res) => {
try {
const payload = _validatePayload(_parseHttpPayload(_validateHttpReq(req)));
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);
res.status(200).send(message);
})
.catch(err => {
console.log(err);
res.status(500).send({error: err.message});
});
} catch (err) {
console.log(err);
res.status(400).send({error: err.message});
}
return res;
};

// [END functions_stop_instance_http]
// [START functions_start_instance_pubsub]
/**
* Starts a Compute Engine instance.
*
Expand Down Expand Up @@ -146,9 +58,9 @@ exports.startInstancePubSub = (event, callback) => {
callback(err);
}
};

// [END functions_start_instance_pubsub]
// [START functions_stop_instance_pubsub]

/**
* Stops a Compute Engine instance.
*
Expand Down Expand Up @@ -187,10 +99,8 @@ exports.stopInstancePubSub = (event, callback) => {
callback(err);
}
};

// [START functions_start_instance_http]
// [START functions_stop_instance_http]
// [START functions_start_instance_pubsub]

/**
* Validates that a request payload contains the expected fields.
*
Expand All @@ -207,41 +117,3 @@ function _validatePayload (payload) {
}
// [END functions_start_instance_pubsub]
// [END functions_stop_instance_pubsub]

/**
* Parses the request payload of an HTTP request based on content-type.
*
* @param {!object} req a Cloud Functions HTTP request object.
* @returns {!object} an object with attributes matching the request payload.
*/
function _parseHttpPayload (req) {
const contentType = req.get('content-type');
if (contentType === 'application/json') {
// Request.body automatically parsed as an object.
return req.body;
} else if (contentType === 'application/octet-stream') {
// Convert buffer to a string and parse as JSON string.
return JSON.parse(req.body.toString());
} else {
throw new Error('Unsupported HTTP content-type ' + req.get('content-type') +
'; use application/json or application/octet-stream');
}
}

/**
* Validates that a HTTP request contains the expected fields.
*
* @param {!object} req the request to validate.
* @returns {!object} the request object.
*/
function _validateHttpReq (req) {
if (req.method !== 'POST') {
throw new Error('Unsupported HTTP method ' + req.method +
'; use method POST');
} else if (typeof req.get('content-type') === 'undefined') {
throw new Error('HTTP content-type missing');
}
return req;
}
// [END functions_start_instance_http]
// [END functions_stop_instance_http]
Loading