Skip to content

Commit 5987eab

Browse files
authored
feat(storage): migrate notifications samples and tests (#4267)
* feat(storage): introduce notification samples and tests for migration * fix(storage): apply code review suggestions for IAM permissions
1 parent aa83d93 commit 5987eab

5 files changed

Lines changed: 350 additions & 0 deletions

File tree

storage/createNotification.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on files with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
const uuid = require('uuid');
25+
26+
function main(
27+
bucketName = 'my-bucket',
28+
topic = `nodejs-storage-samples-${uuid.v4()}`
29+
) {
30+
// [START storage_create_bucket_notifications]
31+
/**
32+
* TODO(developer): Uncomment the following lines before running the sample.
33+
*/
34+
// The ID of your GCS bucket
35+
// const bucketName = 'your-unique-bucket-name';
36+
37+
// The name of a topic
38+
// const topic = 'my-topic';
39+
40+
// Imports the Google Cloud client library
41+
const {Storage} = require('@google-cloud/storage');
42+
43+
// Creates a client
44+
const storage = new Storage();
45+
46+
async function createNotification() {
47+
try {
48+
// Creates a notification
49+
await storage.bucket(bucketName).createNotification(topic);
50+
51+
console.log('Notification subscription created.');
52+
} catch (error) {
53+
console.error(
54+
'Error executing create notification:',
55+
error.message || error
56+
);
57+
}
58+
}
59+
60+
createNotification();
61+
// [END storage_create_bucket_notifications]
62+
}
63+
main(...process.argv.slice(2));

storage/deleteNotification.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on files with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(bucketName = 'my-bucket', notificationId = '1') {
26+
// [START storage_delete_bucket_notification]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// The ID of the notification
34+
// const notificationId = '1';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
async function deleteNotification() {
43+
try {
44+
// Deletes the notification from the bucket
45+
await storage.bucket(bucketName).notification(notificationId).delete();
46+
47+
console.log(`Notification ${notificationId} deleted.`);
48+
} catch (error) {
49+
console.error(
50+
'Error executing delete notification:',
51+
error.message || error
52+
);
53+
}
54+
}
55+
56+
deleteNotification();
57+
// [END storage_delete_bucket_notification]
58+
}
59+
main(...process.argv.slice(2));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on files with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(bucketName = 'my-bucket', notificationId = '1') {
26+
// [START storage_print_pubsub_bucket_notification]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// The ID of the notification
34+
// const notificationId = '1';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
async function getMetadata() {
43+
try {
44+
// Get the notification metadata
45+
const [metadata] = await storage
46+
.bucket(bucketName)
47+
.notification(notificationId)
48+
.getMetadata();
49+
50+
console.log(`ID: ${metadata.id}`);
51+
console.log(`Topic: ${metadata.topic}`);
52+
console.log(`Event Types: ${metadata.event_types}`);
53+
console.log(`Custom Attributes: ${metadata.custom_attributes}`);
54+
console.log(`Payload Format: ${metadata.payload_format}`);
55+
console.log(`Object Name Prefix: ${metadata.object_name_prefix}`);
56+
console.log(`Etag: ${metadata.etag}`);
57+
console.log(`Self Link: ${metadata.selfLink}`);
58+
console.log(`Kind: ${metadata.kind}`);
59+
} catch (error) {
60+
console.error('Error executing get metadata:', error.message || error);
61+
}
62+
}
63+
64+
getMetadata();
65+
// [END storage_print_pubsub_bucket_notification]
66+
}
67+
main(...process.argv.slice(2));

storage/listNotifications.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on files with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_list_bucket_notifications]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function listNotifications() {
40+
try {
41+
// Lists notifications in the bucket
42+
const [notifications] = await storage
43+
.bucket(bucketName)
44+
.getNotifications();
45+
46+
console.log('Notifications:');
47+
notifications.forEach(notification => {
48+
console.log(notification.id);
49+
});
50+
} catch (error) {
51+
console.error(
52+
'Error executing list notifications:',
53+
error.message || error
54+
);
55+
}
56+
}
57+
58+
listNotifications();
59+
// [END storage_list_bucket_notifications]
60+
}
61+
main(...process.argv.slice(2));
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2019 Google LLC
2+
//
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+
15+
'use strict';
16+
17+
const {PubSub} = require('@google-cloud/pubsub');
18+
const {Storage} = require('@google-cloud/storage');
19+
const {assert} = require('chai');
20+
const {before, after, it} = require('mocha');
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const storage = new Storage();
27+
const bucketName = `nodejs-storage-samples-${uuid.v4()}`;
28+
const bucket = storage.bucket(bucketName);
29+
const notificationId = '1';
30+
const notification = bucket.notification(notificationId);
31+
const topicName = `nodejs-storage-samples-${uuid.v4()}`;
32+
const pubsub = new PubSub();
33+
const topic = pubsub.topic(topicName);
34+
35+
before(async () => {
36+
await bucket.create();
37+
await topic.create();
38+
const [gcsServiceAccount] = await storage.getServiceAccount();
39+
await topic.iam.setPolicy({
40+
bindings: [
41+
{
42+
role: 'roles/pubsub.editor',
43+
members: [`serviceAccount:${gcsServiceAccount.emailAddress}`],
44+
},
45+
],
46+
});
47+
});
48+
49+
after(async () => {
50+
await bucket.delete().catch(console.error);
51+
await topic.delete().catch(console.error);
52+
});
53+
54+
it('should create a notification', async () => {
55+
const output = execSync(
56+
`node createNotification.js ${bucketName} ${topicName}`
57+
);
58+
assert.match(output, /Notification subscription created./);
59+
const [exists] = await notification.exists();
60+
assert.strictEqual(exists, true);
61+
});
62+
63+
it('should list notifications', async () => {
64+
const output = execSync(`node listNotifications.js ${bucketName}`);
65+
assert.match(output, /Notifications:/);
66+
assert.match(output, new RegExp(notificationId));
67+
});
68+
69+
it('should get metadata', async () => {
70+
const metadata = await notification.getMetadata();
71+
const output = execSync(
72+
`node getMetadataNotifications.js ${bucketName} ${notificationId}`
73+
);
74+
assert.match(output, /ID:/);
75+
assert.match(output, new RegExp(metadata.id));
76+
assert.match(output, /Topic:/);
77+
assert.match(output, new RegExp(metadata.topic));
78+
assert.match(output, /Event Types:/);
79+
assert.match(output, new RegExp(metadata.event_types));
80+
assert.match(output, /Custom Attributes:/);
81+
assert.match(output, new RegExp(metadata.custom_attributes));
82+
assert.match(output, /Payload Format:/);
83+
assert.match(output, new RegExp(metadata.payload_format));
84+
assert.match(output, /Object Name Prefix:/);
85+
assert.match(output, new RegExp(metadata.object_name_prefix));
86+
assert.match(output, /Etag:/);
87+
assert.match(output, /Self Link:/);
88+
assert.match(output, new RegExp(metadata.selfLink));
89+
assert.match(output, /Kind:/);
90+
assert.match(output, new RegExp(metadata.kind));
91+
});
92+
93+
it('should delete a notification', async () => {
94+
const output = execSync(
95+
`node deleteNotification.js ${bucketName} ${notificationId}`
96+
);
97+
assert.match(output, new RegExp(`Notification ${notificationId} deleted.`));
98+
const [exists] = await notification.exists();
99+
assert.strictEqual(exists, false);
100+
});

0 commit comments

Comments
 (0)