Skip to content

Commit 02e99ac

Browse files
committed
Refactor Cloud Functions samples.
1 parent 2cf8055 commit 02e99ac

38 files changed

+2065
-2242
lines changed

functions/background/index.js

+41-37
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,72 @@
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.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

16-
// [START helloworld]
18+
// [START functions_background_helloworld]
1719
/**
1820
* Background Cloud Function.
1921
*
20-
* @param {Object} context Cloud Function context.
21-
* @param {Object} data Request data, provided by a trigger.
22-
* @param {string} data.message Message, provided by the trigger.
22+
* @param {object} event The Cloud Functions event.
23+
* @param {object} event.data The event data.
24+
* @param {function} The callback function.
2325
*/
24-
exports.helloWorld = function helloWorld (context, data) {
25-
if (data.message === undefined) {
26-
// This is an error case, "message" is required
27-
context.failure('No message defined!');
26+
exports.helloWorld = function helloWorld (event, callback) {
27+
if (!event.data.myMessage) {
28+
// This is an error case, "myMessage" is required
29+
callback(new Error('No message defined!'));
2830
} else {
2931
// Everything is ok
30-
console.log(data.message);
31-
context.success();
32+
console.log(event.data.myMessage);
33+
callback();
3234
}
3335
};
34-
// [END helloworld]
35-
36-
// [START helloPromise]
37-
var request = require('request-promise');
36+
// [END functions_background_helloworld]
3837

38+
// [START functions_background_promise]
3939
/**
4040
* Background Cloud Function that returns a Promise. Note that we don't pass
41-
* a "context" argument to the function.
41+
* a "callback" argument to the function.
4242
*
43-
* @param {Object} data Request data, provided by a trigger.
43+
* @param {object} event The Cloud Functions event.
44+
* @param {object} event.data The event data.
4445
* @returns {Promise}
4546
*/
46-
exports.helloPromise = function helloPromise (data) {
47+
exports.helloPromise = function helloPromise (event) {
48+
const request = require('request-promise');
49+
4750
return request({
48-
uri: data.endpoint
51+
uri: event.data.endpoint
4952
});
5053
};
51-
// [END helloPromise]
54+
// [END functions_background_promise]
5255

53-
// [START helloSynchronous]
56+
// [START functions_background_synchronous]
5457
/**
5558
* Background Cloud Function that returns synchronously. Note that we don't pass
56-
* a "context" argument to the function.
59+
* a "callback" argument to the function.
5760
*
58-
* @param {Object} data Request data, provided by a trigger.
61+
* @param {object} event The Cloud Functions event.
62+
* @param {object} event.data The event data.
5963
*/
60-
exports.helloSynchronous = function helloSynchronous (data) {
64+
exports.helloSynchronous = function helloSynchronous (event) {
6165
// This function returns synchronously
62-
if (data.something === true) {
66+
if (event.data.something === true) {
6367
return 'Something is true!';
6468
} else {
6569
throw new Error('Something was not true!');
6670
}
6771
};
68-
// [END helloSynchronous]
72+
// [END functions_background_synchronous]

functions/background/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
1010
},
1111
"dependencies": {
12-
"request-promise": "^3.0.0"
12+
"request": "^2.75.0",
13+
"request-promise": "^4.1.1"
1314
},
1415
"devDependencies": {
15-
"mocha": "^2.5.3"
16+
"mocha": "^3.1.2"
1617
}
1718
}
+68-65
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
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.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

16-
var proxyquire = require('proxyquire').noCallThru();
18+
const proxyquire = require(`proxyquire`).noCallThru();
1719

1820
function getSample () {
19-
var requestPromise = sinon.stub().returns(new Promise(function (resolve) {
20-
resolve('test');
21-
}));
21+
const requestPromise = sinon.stub().returns(Promise.resolve(`test`));
22+
2223
return {
23-
sample: proxyquire('../', {
24+
program: proxyquire(`../`, {
2425
'request-promise': requestPromise
2526
}),
2627
mocks: {
@@ -29,62 +30,64 @@ function getSample () {
2930
};
3031
}
3132

32-
function getMockContext () {
33-
return {
34-
success: sinon.stub(),
35-
failure: sinon.stub()
36-
};
37-
}
33+
describe(`functions:background`, () => {
34+
it(`should echo message`, () => {
35+
const event = {
36+
data: {
37+
myMessage: `hi`
38+
}
39+
};
40+
const sample = getSample();
41+
const callback = sinon.stub();
3842

39-
describe('functions:background', function () {
40-
it('should echo message', function () {
41-
var expectedMsg = 'hi';
42-
var context = getMockContext();
43-
var backgroundSample = getSample();
44-
backgroundSample.sample.helloWorld(context, {
45-
message: expectedMsg
46-
});
43+
sample.program.helloWorld(event, callback);
4744

48-
assert(context.success.calledOnce);
49-
assert.equal(context.failure.called, false);
50-
assert(console.log.calledWith(expectedMsg));
45+
assert.equal(console.log.callCount, 1);
46+
assert.deepEqual(console.log.firstCall.args, [event.data.myMessage]);
47+
assert.equal(callback.callCount, 1);
48+
assert.deepEqual(callback.firstCall.args, []);
5149
});
52-
it('should say no message was provided', function () {
53-
var expectedMsg = 'No message defined!';
54-
var context = getMockContext();
55-
var backgroundSample = getSample();
56-
backgroundSample.sample.helloWorld(context, {});
5750

58-
assert(context.failure.calledOnce);
59-
assert(context.failure.firstCall.args[0] === expectedMsg);
60-
assert.equal(context.success.called, false);
51+
it(`should say no message was provided`, () => {
52+
const error = new Error(`No message defined!`);
53+
const callback = sinon.stub();
54+
const sample = getSample();
55+
sample.program.helloWorld({ data: {} }, callback);
56+
57+
assert.equal(callback.callCount, 1);
58+
assert.deepEqual(callback.firstCall.args, [error]);
6159
});
62-
it('should make a promise request', function (done) {
63-
var backgroundSample = getSample();
64-
backgroundSample.sample.helloPromise({
65-
endpoint: 'foo.com'
66-
}).then(function (result) {
67-
assert.deepEqual(backgroundSample.mocks.requestPromise.firstCall.args[0], {
68-
uri: 'foo.com'
60+
61+
it(`should make a promise request`, () => {
62+
const sample = getSample();
63+
const event = {
64+
data: {
65+
endpoint: `foo.com`
66+
}
67+
};
68+
69+
return sample.program.helloPromise(event)
70+
.then((result) => {
71+
assert.deepEqual(sample.mocks.requestPromise.firstCall.args, [{ uri: `foo.com` }]);
72+
assert.equal(result, `test`);
6973
});
70-
assert.equal(result, 'test');
71-
done();
72-
}, function () {
73-
assert.fail();
74-
});
7574
});
76-
it('should return synchronously', function () {
77-
var backgroundSample = getSample();
78-
assert(backgroundSample.sample.helloSynchronous({
79-
something: true
80-
}) === 'Something is true!');
75+
76+
it(`should return synchronously`, () => {
77+
assert.equal(getSample().program.helloSynchronous({
78+
data: {
79+
something: true
80+
}
81+
}), `Something is true!`);
8182
});
82-
it('should throw an error', function () {
83-
var backgroundSample = getSample();
84-
assert.throws(function () {
85-
backgroundSample.sample.helloSynchronous({
86-
something: false
83+
84+
it(`should throw an error`, () => {
85+
assert.throws(() => {
86+
getSample().program.helloSynchronous({
87+
data: {
88+
something: false
89+
}
8790
});
88-
}, Error, 'Something was not true!');
91+
}, Error, `Something was not true!`);
8992
});
9093
});

functions/datastore/README.md

+50-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
# Google Cloud Functions Cloud Datastore sample
44

5-
This recipe shows you how to read and write an entity in Datastore from a Cloud Function.
5+
This recipe shows you how to read and write an entity in Cloud Datastore from a
6+
Cloud Function.
67

78
View the [source code][code].
89

@@ -20,46 +21,75 @@ Functions for your project.
2021

2122
1. Create a Cloud Storage Bucket to stage our deployment:
2223

23-
gsutil mb gs://[YOUR_BUCKET_NAME]
24+
gsutil mb gs://YOUR_BUCKET_NAME
2425

25-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
26+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
2627

2728
1. Ensure the Cloud Datastore API is enabled:
2829

2930
[Click here to enable the Cloud Datastore API](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com&redirect=https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/datastore)
3031

31-
1. Deploy the "ds-get" function with an HTTP trigger:
32+
1. Deploy the "get" function with an HTTP trigger:
3233

33-
gcloud alpha functions deploy ds-get --bucket [YOUR_BUCKET_NAME] --trigger-http --entry-point get
34+
gcloud alpha functions deploy get --stage-bucket YOUR_BUCKET_NAME --trigger-http
3435

35-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
36+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
3637

37-
1. Deploy the "ds-set" function with an HTTP trigger:
38+
1. Deploy the "set" function with an HTTP trigger:
3839

39-
gcloud alpha functions deploy ds-set --bucket [YOUR_BUCKET_NAME] --trigger-http --entry-point set
40+
gcloud alpha functions deploy set --stage-bucket YOUR_BUCKET_NAME --trigger-http
4041

41-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
42+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
4243

43-
1. Deploy the "ds-del" function with an HTTP trigger:
44+
1. Deploy the "del" function with an HTTP trigger:
4445

45-
gcloud alpha functions deploy ds-del --bucket [YOUR_BUCKET_NAME] --trigger-http --entry-point del
46+
gcloud alpha functions deploy del --stage-bucket YOUR_BUCKET_NAME --trigger-http
4647

47-
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
48+
* Replace `YOUR_BUCKET_NAME` with the name of your Cloud Storage Bucket.
4849

49-
1. Call the "ds-set" function to create a new entity:
50+
1. Call the "set" function to create a new entity:
5051

51-
gcloud alpha functions call ds-set --data '{"kind":"gcf-test","key":"foobar","value":{"message":"Hello World!"}}'
52+
gcloud alpha functions call set --data '{"kind":"Task","key":"sampletask1","value":{"description":"Buy milk"}}'
5253

53-
1. Call the "ds-get" function to read the newly created entity:
54+
or
5455

55-
gcloud alpha functions call ds-get --data '{"kind":"gcf-test","key":"foobar"}'
56+
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1","value":{"description":"Buy milk"}}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/set"
5657

57-
1. Call the "ds-del" function to delete the entity:
58+
* Replace `[YOUR_REGION]` with the region where your function is deployed.
59+
* Replace `[YOUR_PROJECT_ID]` with your Google Cloud Platform project ID.
5860

59-
gcloud alpha functions call ds-del --data '{"kind":"gcf-test","key":"foobar"}'
61+
1. Call the "get" function to read the newly created entity:
6062

61-
1. Call the "ds-get" function again to verify it was deleted:
63+
gcloud alpha functions call get --data '{"kind":"Task","key":"sampletask1"}'
64+
65+
or
66+
67+
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/get"
68+
69+
* Replace `[YOUR_REGION]` with the region where your function is deployed.
70+
* Replace `[YOUR_PROJECT_ID]` with your Google Cloud Platform project ID.
71+
72+
1. Call the "del" function to delete the entity:
73+
74+
gcloud alpha functions call del --data '{"kind":"Task","key":"sampletask1"}'
75+
76+
or
77+
78+
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/del"
79+
80+
* Replace `[YOUR_REGION]` with the region where your function is deployed.
81+
* Replace `[YOUR_PROJECT_ID]` with your Google Cloud Platform project ID.
82+
83+
1. Call the "get" function again to verify it was deleted:
84+
85+
gcloud alpha functions call get --data '{"kind":"Task","key":"sampletask1"}'
86+
87+
or
88+
89+
curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/get"
90+
91+
* Replace `[YOUR_REGION]` with the region where your function is deployed.
92+
* Replace `[YOUR_PROJECT_ID]` with your Google Cloud Platform project ID.
6293

63-
gcloud alpha functions call ds-get --data '{"kind":"gcf-test","key":"foobar"}'
6494

6595
[quickstart]: https://cloud.google.com/functions/quickstart

0 commit comments

Comments
 (0)