Skip to content

Commit 55203fa

Browse files
committed
Cloud Functions samples.
1 parent d831992 commit 55203fa

20 files changed

+531
-8
lines changed

.jshintignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ appengine/sails/tasks/**
66
appengine/sails/assets/**
77
appengine/sails/api/responses/**
88
appengine/webpack/dist/**
9+
functions/**
910
**/node_modules/**
1011
coverage/

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This repository holds Node.js samples used throughout [cloud.google.com]().
77
## Table of Contents
88

99
* [Google App Engine](#google-app-engine)
10+
* [Google Cloud Functions](#google-cloud-functions)
1011
* [Google Cloud Logging](#google-cloud-logging)
1112
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
1213
* [Google Cloud Storage](#google-cloud-storage)
@@ -67,10 +68,14 @@ __Other Examples__
6768
- Reading/writing from/to disk - [Source code][aedisk_1]
6869
- Serving static files - [Source code][aestaticfiles_1]
6970

70-
## Google Cloud Datastorem
71+
## Google Cloud Datastore
7172

7273
- Tasks sample - [Source code][datastore_1] | [Documentation][datastore_2]
7374

75+
## Google Cloud Functions
76+
77+
- Samples - [Source code][functions_1] | [Documentation][functions_2]
78+
7479
## Google Cloud Logging
7580

7681
- Reading logs sample - [Source code][logging_read_1] | [Documentation][logging_read_2]
@@ -251,6 +256,9 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
251256
[datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js
252257
[datastore_2]: https://cloud.google.com/datastore/docs/concepts/overview
253258

259+
[functions_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/
260+
[functions_2]: https://cloud.google.com/functions/docs
261+
254262
[logging_read_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/list.js
255263
[logging_read_2]: https://cloud.google.com/logging/docs/api/tasks/authorization
256264
[logging_write_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/write.js

functions/helloworld/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Google Cloud Functions Hello World! sample
2+
3+
This sample shows a single exported function–a basic hello world example.
4+
5+
## Deploy sample
6+
7+
This example deploys the function with an HTTP trigger.
8+
9+
```
10+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
11+
```
12+
13+
## Test the function
14+
15+
```
16+
gcloud alpha functions call helloworld
17+
```
18+
19+
Running the above command should print "Hello World!".

functions/helloworld/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START helloworld]
17+
exports.helloworld = function (context, data) {
18+
context.success('Hello World!');
19+
};
20+
// [END helloworld]

functions/log/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Google Cloud Functions message sample
2+
3+
This sample shows writing to logs in a Cloud Function.
4+
5+
## Deploy sample
6+
7+
This example deploys the function with an HTTP trigger.
8+
9+
```
10+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
11+
```
12+
13+
## Test the function
14+
15+
```
16+
gcloud alpha functions call helloworld
17+
```

functions/log/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START log]
17+
exports.helloworld = function (context, data) {
18+
console.log('I am a log entry!');
19+
context.success();
20+
};
21+
// [END log]
22+
23+
exports.log = exports.helloworld;
24+
25+
// [START walkthrough_pubsub]
26+
exports.helloworld = function (context, data) {
27+
console.log('My GCF Function: ' + data.message);
28+
context.success();
29+
};
30+
// [END walkthrough_pubsub]
31+
32+
// [START walkthrough_http]
33+
exports.hellohttp = function (context, data) {
34+
// Use the success argument to send data back to the caller
35+
context.success('My GCF Function: ' + data.message);
36+
};
37+
// [END walkthrough_http]

functions/message/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Google Cloud Functions message sample
2+
3+
This sample shows calling the `success` and `failure` callbacks.
4+
5+
## Deploy sample
6+
7+
This example deploys the function with an HTTP trigger.
8+
9+
```
10+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
11+
```
12+
13+
## Test the function
14+
15+
```
16+
gcloud alpha functions call helloworld
17+
```

functions/message/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START message]
17+
module.exports = {
18+
helloworld: function (context, data) {
19+
if (data.message !== undefined) {
20+
// Everything is ok
21+
console.log(data.message);
22+
context.success();
23+
} else {
24+
// This is an error case
25+
context.failure('No message defined!');
26+
}
27+
}
28+
};
29+
// [END message]

functions/module/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google Cloud Functions module sample
2+
3+
This sample shows exporting a Google Cloud Function as part of a module, which
4+
is the method one would use to store multiple function in a single source file.
5+
6+
## Deploy sample
7+
8+
This example deploys the function with an HTTP trigger.
9+
10+
```
11+
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
12+
```
13+
14+
## Test the function
15+
16+
```
17+
gcloud alpha functions call helloworld
18+
```
19+
20+
Running the above command should print a generated UUID.

functions/module/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START module]
17+
module.exports = {
18+
helloworld: function (context, data) {
19+
context.success('Hello World!');
20+
}
21+
};
22+
// [END module]

functions/uuid/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google Cloud Functions UUID sample
2+
3+
This sample shows a Google Cloud Function that uses a dependency from NPM, and
4+
is a handy way to generate a v4 UUID from the command-line.
5+
6+
## Deploy sample
7+
8+
This example deploys the function with an HTTP trigger.
9+
10+
```
11+
gcloud alpha functions deploy uuid --bucket <your-bucket-name> --trigger-http
12+
```
13+
14+
## Test the function
15+
16+
```
17+
gcloud alpha functions call uuid
18+
```
19+
20+
Running the above command should print a generated UUID.

functions/uuid/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START uuid]
17+
var uuid = require('node-uuid');
18+
19+
exports.uuid = function (context, data) {
20+
context.success(uuid.v4());
21+
};
22+
// [END uuid]

functions/uuid/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "nodejs-docs-samples-functions",
3+
"description": "Node.js samples found on https://cloud.google.com",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"contributors": [
9+
{
10+
"name": "Jason Dobry",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
17+
},
18+
"dependencies": {
19+
"node-uuid": "^1.4.7"
20+
}
21+
}

package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
"mocha": "mocha --timeout 10000 --recursive",
2929
"cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 30000 --recursive",
3030
"coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls",
31-
"deps_datastore": "cd datastore; npm i; cd ../..",
32-
"deps_pubsub": "cd pubsub; npm i; cd ../..",
33-
"deps_storage": "cd storage; npm i; cd ../..",
34-
"deps_prediction": "cd prediction; npm i; cd ../..",
35-
"deps_logging": "cd logging; npm i; cd ../..",
31+
"deps_datastore": "cd datastore; npm i; cd ../",
32+
"deps_pubsub": "cd pubsub; npm i; cd ../",
33+
"deps_storage": "cd storage; npm i; cd ../",
34+
"deps_prediction": "cd prediction; npm i; cd ../",
35+
"deps_logging": "cd logging; npm i; cd ../",
36+
"deps_functions": "cd functions/uuid; npm i; cd ../..",
3637
"deps_sendgrid": "cd computeengine/sendgrid; npm i; cd ../..",
3738
"pretest_geddy": "cd appengine/geddy; npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;",
38-
"pretest": "npm run deps_datastore; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_sendgrid; npm run pretest_geddy",
39+
"pretest": "npm run deps_datastore; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_sendgrid; npm run pretest_geddy",
3940
"test": "npm run jshint && npm run cover"
4041
},
4142
"devDependencies": {

test/functions/helloworld.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var assert = require('assert');
17+
18+
var helloworldSample = require('../../functions/helloworld');
19+
20+
describe('functions/helloworld', function () {
21+
it('should return a hello world message', function (done) {
22+
helloworldSample.helloworld({
23+
success: function (result) {
24+
try {
25+
assert.equal(result, 'Hello World!');
26+
done();
27+
} catch (err) {
28+
done(err);
29+
}
30+
}
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)