Skip to content

Commit

Permalink
Cloud Functions samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Feb 11, 2016
1 parent d831992 commit e300b24
Show file tree
Hide file tree
Showing 20 changed files with 551 additions and 8 deletions.
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ appengine/sails/tasks/**
appengine/sails/assets/**
appengine/sails/api/responses/**
appengine/webpack/dist/**
functions/**
**/node_modules/**
coverage/
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This repository holds Node.js samples used throughout [cloud.google.com]().
## Table of Contents

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

## Google Cloud Datastorem
## Google Cloud Datastore

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

## Google Cloud Functions

- Samples - [Source code][functions_1] | [Documentation][functions_2]

## Google Cloud Logging

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

[functions_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/
[functions_2]: https://cloud.google.com/functions/docs

[logging_read_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/list.js
[logging_read_2]: https://cloud.google.com/logging/docs/api/tasks/authorization
[logging_write_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/write.js
Expand Down
23 changes: 23 additions & 0 deletions functions/helloworld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Google Cloud Functions Hello World! sample

This is a basic hello world sample that shows a single exported function.

## Deploy sample

This example deploys the function with an HTTP trigger.

```
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
```

## Test the function

```
gcloud alpha functions call helloworld
```

Running the above command should print "Hello World!".

You can also use `curl` to trigger the function:

curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld
20 changes: 20 additions & 0 deletions functions/helloworld/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2016, 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';

// [START helloworld]
exports.helloworld = function (context, data) {
context.success('Hello World!');
};
// [END helloworld]
21 changes: 21 additions & 0 deletions functions/log/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Google Cloud Functions message sample

This sample shows writing to logs in a Cloud Function.

## Deploy sample

This example deploys the function with an HTTP trigger.

```
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
```

## Test the function

```
gcloud alpha functions call helloworld
```

You can also use `curl` to trigger the function:

curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld
37 changes: 37 additions & 0 deletions functions/log/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2016, 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';

// [START log]
exports.helloworld = function (context, data) {
console.log('I am a log entry!');
context.success();
};
// [END log]

exports.log = exports.helloworld;

// [START walkthrough_pubsub]
exports.helloworld = function (context, data) {
console.log('My GCF Function: ' + data.message);
context.success();
};
// [END walkthrough_pubsub]

// [START walkthrough_http]
exports.hellohttp = function (context, data) {
// Use the success argument to send data back to the caller
context.success('My GCF Function: ' + data.message);
};
// [END walkthrough_http]
21 changes: 21 additions & 0 deletions functions/message/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Google Cloud Functions message sample

This sample shows calling the `success` and `failure` callbacks.

## Deploy sample

This example deploys the function with an HTTP trigger.

```
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
```

## Test the function

```
gcloud alpha functions call helloworld
```

You can also use `curl` to trigger the function:

curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld --data '{"message":"cat"}'
29 changes: 29 additions & 0 deletions functions/message/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016, 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';

// [START message]
module.exports = {
helloworld: function (context, data) {
if (data.message !== undefined) {
// Everything is ok
console.log(data.message);
context.success();
} else {
// This is an error case
context.failure('No message defined!');
}
}
};
// [END message]
24 changes: 24 additions & 0 deletions functions/module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Google Cloud Functions module sample

This sample shows exporting a Google Cloud Function as part of a module, which
is the method one would use to store multiple function in a single source file.

## Deploy sample

This example deploys the function with an HTTP trigger.

```
gcloud alpha functions deploy helloworld --bucket <your-bucket-name> --trigger-http
```

## Test the function

```
gcloud alpha functions call helloworld
```

Running the above command should print "Hello World!".

You can also use `curl` to trigger the function:

curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/helloworld
22 changes: 22 additions & 0 deletions functions/module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2016, 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';

// [START module]
module.exports = {
helloworld: function (context, data) {
context.success('Hello World!');
}
};
// [END module]
24 changes: 24 additions & 0 deletions functions/uuid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Google Cloud Functions UUID sample

This sample shows a Google Cloud Function that uses a dependency from NPM, and
is a handy way to generate a v4 UUID from the command-line.

## Deploy sample

This example deploys the function with an HTTP trigger.

```
gcloud alpha functions deploy uuid --bucket <your-bucket-name> --trigger-http
```

## Test the function

```
gcloud alpha functions call uuid
```

Running the above command should print a generated UUID.

You can also use `curl` to trigger the function:

curl -X POST https://<your-project-region>.<your-project-id>.cloudfunctions.net/uuid
22 changes: 22 additions & 0 deletions functions/uuid/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2016, 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';

// [START uuid]
var uuid = require('node-uuid');

exports.uuid = function (context, data) {
context.success(uuid.v4());
};
// [END uuid]
21 changes: 21 additions & 0 deletions functions/uuid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "nodejs-docs-samples-functions",
"description": "Node.js samples found on https://cloud.google.com",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"contributors": [
{
"name": "Jason Dobry",
"email": "jason.dobry@gmail.com"
}
],
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"dependencies": {
"node-uuid": "^1.4.7"
}
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
"mocha": "mocha --timeout 10000 --recursive",
"cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 30000 --recursive",
"coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls",
"deps_datastore": "cd datastore; npm i; cd ../..",
"deps_pubsub": "cd pubsub; npm i; cd ../..",
"deps_storage": "cd storage; npm i; cd ../..",
"deps_prediction": "cd prediction; npm i; cd ../..",
"deps_logging": "cd logging; npm i; cd ../..",
"deps_datastore": "cd datastore; npm i; cd ../",
"deps_pubsub": "cd pubsub; npm i; cd ../",
"deps_storage": "cd storage; npm i; cd ../",
"deps_prediction": "cd prediction; npm i; cd ../",
"deps_logging": "cd logging; npm i; cd ../",
"deps_functions": "cd functions/uuid; npm i; cd ../..",
"deps_sendgrid": "cd computeengine/sendgrid; npm i; cd ../..",
"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 ../..;",
"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",
"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",
"test": "npm run jshint && npm run cover"
},
"devDependencies": {
Expand Down
33 changes: 33 additions & 0 deletions test/functions/helloworld.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2016, 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';

var assert = require('assert');

var helloworldSample = require('../../functions/helloworld');

describe('functions/helloworld', function () {
it('should return a hello world message', function (done) {
helloworldSample.helloworld({
success: function (result) {
try {
assert.equal(result, 'Hello World!');
done();
} catch (err) {
done(err);
}
}
});
});
});
Loading

0 comments on commit e300b24

Please sign in to comment.