Skip to content

Commit 5e5f307

Browse files
authored
Update Cloud Functions GCS hello world sample. (#380)
1 parent 4ea4f39 commit 5e5f307

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

functions/helloworld/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ exports.helloPubSub = function (event, callback) {
9696
*/
9797
exports.helloGCS = function (event, callback) {
9898
const file = event.data;
99-
const isDelete = file.resourceState === 'not_exists';
10099

101-
if (isDelete) {
100+
if (file.resourceState === 'not_exists') {
102101
console.log(`File ${file.name} deleted.`);
103-
} else {
102+
} else if (file.metageneration === 1) {
103+
// metageneration attribute is updated on metadata changes.
104+
// on create value is 1
104105
console.log(`File ${file.name} uploaded.`);
106+
} else {
107+
console.log(`File ${file.name} metadata updated.`);
105108
}
106109

107110
callback();

functions/helloworld/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
},
1919
"dependencies": {
2020
"@google-cloud/debug-agent": "1.0.0",
21-
"pug": "2.0.0-beta.12",
21+
"pug": "2.0.0-rc.1",
2222
"safe-buffer": "5.0.1"
2323
},
2424
"devDependencies": {
25-
"@google-cloud/nodejs-repo-tools": "1.4.7",
25+
"@google-cloud/nodejs-repo-tools": "1.4.12",
2626
"ava": "0.19.1",
2727
"proxyquire": "1.7.11",
28-
"sinon": "2.1.0"
28+
"sinon": "2.2.0"
2929
},
3030
"cloud-repo-tools": {
3131
"requiresKeyFile": true,

functions/helloworld/test/index.test.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,26 @@ test.serial(`helloGCS: should print uploaded message`, (t) => {
142142
program.helloGCS({
143143
data: {
144144
name: `foo`,
145-
resourceState: `exists`
145+
resourceState: `exists`,
146+
metageneration: 1
147+
}
148+
}, callback);
149+
150+
t.deepEqual(console.log.callCount, 1);
151+
t.deepEqual(console.log.firstCall.args, [expectedMsg]);
152+
t.deepEqual(callback.callCount, 1);
153+
t.deepEqual(callback.firstCall.args, []);
154+
});
155+
156+
test.serial(`helloGCS: should print metadata updated message`, (t) => {
157+
const expectedMsg = `File foo metadata updated.`;
158+
const callback = sinon.stub();
159+
160+
program.helloGCS({
161+
data: {
162+
name: `foo`,
163+
resourceState: `exists`,
164+
metageneration: 2
146165
}
147166
}, callback);
148167

functions/helloworld/yarn.lock

+24-15
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@
7676
source-map "^0.5.1"
7777
split "^1.0.0"
7878

79-
"@google-cloud/[email protected].7":
80-
version "1.4.7"
81-
resolved "https://registry.yarnpkg.com/@google-cloud/nodejs-repo-tools/-/nodejs-repo-tools-1.4.7.tgz#1e3838af75386d99317f892ff1288e217a4a4b11"
79+
"@google-cloud/[email protected].12":
80+
version "1.4.12"
81+
resolved "https://registry.yarnpkg.com/@google-cloud/nodejs-repo-tools/-/nodejs-repo-tools-1.4.12.tgz#41b9dc143daeb0e2714e884a7f56fd0730159735"
8282
dependencies:
8383
ava "0.19.1"
8484
colors "1.1.2"
@@ -2728,9 +2728,9 @@ pug-lexer@^3.1.0:
27282728
is-expression "^3.0.0"
27292729
pug-error "^1.3.2"
27302730

2731-
pug-linker@^2.0.3:
2732-
version "2.0.3"
2733-
resolved "https://registry.yarnpkg.com/pug-linker/-/pug-linker-2.0.3.tgz#b331ffa25737dde69c127b56c10ff17fae766dca"
2731+
pug-linker@^3.0.0:
2732+
version "3.0.0"
2733+
resolved "https://registry.yarnpkg.com/pug-linker/-/pug-linker-3.0.0.tgz#2416f06aaa1a9720e3b3261d1e8ca72f6f5123d2"
27342734
dependencies:
27352735
pug-error "^1.3.2"
27362736
pug-walk "^1.1.2"
@@ -2763,14 +2763,14 @@ pug-walk@^1.1.2:
27632763
version "1.1.2"
27642764
resolved "https://registry.yarnpkg.com/pug-walk/-/pug-walk-1.1.2.tgz#debb24c070a95d5fc2aebf3589261833d2332e3b"
27652765

2766-
2767-
version "2.0.0-beta.12"
2768-
resolved "https://registry.yarnpkg.com/pug/-/pug-2.0.0-beta.12.tgz#493037cbfbd97817ce0a4b3190f341cd556874bc"
2766+
2767+
version "2.0.0-rc.1"
2768+
resolved "https://registry.yarnpkg.com/pug/-/pug-2.0.0-rc.1.tgz#723c08b3349bb9b96865b9ed7d3e379e247cd984"
27692769
dependencies:
27702770
pug-code-gen "^1.1.1"
27712771
pug-filters "^2.1.2"
27722772
pug-lexer "^3.1.0"
2773-
pug-linker "^2.0.3"
2773+
pug-linker "^3.0.0"
27742774
pug-load "^2.0.6"
27752775
pug-parser "^2.0.2"
27762776
pug-runtime "^2.0.3"
@@ -3079,6 +3079,19 @@ [email protected]:
30793079
text-encoding "0.6.4"
30803080
type-detect "^4.0.0"
30813081

3082+
3083+
version "2.2.0"
3084+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-2.2.0.tgz#3b1b42ff5defcbf51a52a62aca6d61171b9fd262"
3085+
dependencies:
3086+
diff "^3.1.0"
3087+
formatio "1.2.0"
3088+
lolex "^1.6.0"
3089+
native-promise-only "^0.8.1"
3090+
path-to-regexp "^1.7.0"
3091+
samsam "^1.1.3"
3092+
text-encoding "0.6.4"
3093+
type-detect "^4.0.0"
3094+
30823095
slash@^1.0.0:
30833096
version "1.0.0"
30843097
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
@@ -3519,14 +3532,10 @@ with@^5.0.0:
35193532
acorn "^3.1.0"
35203533
acorn-globals "^3.0.0"
35213534

3522-
3535+
[email protected], wordwrap@~0.0.2:
35233536
version "0.0.2"
35243537
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
35253538

3526-
wordwrap@~0.0.2:
3527-
version "0.0.3"
3528-
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
3529-
35303539
wrap-ansi@^2.0.0:
35313540
version "2.1.0"
35323541
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-2.1.0.tgz#d8fc3d284dd05794fe84973caecdd1cf824fdd85"

0 commit comments

Comments
 (0)