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 | import { ServiceObject } from './nodejs-common/index.js';
|
15 | import { promisifyAll } from '@google-cloud/promisify';
|
16 | /**
|
17 | * The API-formatted resource description of the notification.
|
18 | *
|
19 | * Note: This is not guaranteed to be up-to-date when accessed. To get the
|
20 | * latest record, call the `getMetadata()` method.
|
21 | *
|
22 | * @name Notification#metadata
|
23 | * @type {object}
|
24 | */
|
25 | /**
|
26 | * A Notification object is created from your {@link Bucket} object using
|
27 | * {@link Bucket#notification}. Use it to interact with Cloud Pub/Sub
|
28 | * notifications.
|
29 | *
|
30 | * See {@link https://cloud.google.com/storage/docs/pubsub-notifications| Cloud Pub/Sub Notifications for Google Cloud Storage}
|
31 | *
|
32 | * @class
|
33 | * @hideconstructor
|
34 | *
|
35 | * @param {Bucket} bucket The bucket instance this notification is attached to.
|
36 | * @param {string} id The ID of the notification.
|
37 | *
|
38 | * @example
|
39 | * ```
|
40 | * const {Storage} = require('@google-cloud/storage');
|
41 | * const storage = new Storage();
|
42 | * const myBucket = storage.bucket('my-bucket');
|
43 | *
|
44 | * const notification = myBucket.notification('1');
|
45 | * ```
|
46 | */
|
47 | class Notification extends ServiceObject {
|
48 | constructor(bucket, id) {
|
49 | const requestQueryObject = {};
|
50 | const methods = {
|
51 | /**
|
52 | * Creates a notification subscription for the bucket.
|
53 | *
|
54 | * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/insert| Notifications: insert}
|
55 | * @method Notification#create
|
56 | *
|
57 | * @param {Topic|string} topic The Cloud PubSub topic to which this
|
58 | * subscription publishes. If the project ID is omitted, the current
|
59 | * project ID will be used.
|
60 | *
|
61 | * Acceptable formats are:
|
62 | * - `projects/grape-spaceship-123/topics/my-topic`
|
63 | *
|
64 | * - `my-topic`
|
65 | * @param {CreateNotificationRequest} [options] Metadata to set for
|
66 | * the notification.
|
67 | * @param {CreateNotificationCallback} [callback] Callback function.
|
68 | * @returns {Promise<CreateNotificationResponse>}
|
69 | * @throws {Error} If a valid topic is not provided.
|
70 | *
|
71 | * @example
|
72 | * ```
|
73 | * const {Storage} = require('@google-cloud/storage');
|
74 | * const storage = new Storage();
|
75 | * const myBucket = storage.bucket('my-bucket');
|
76 | * const notification = myBucket.notification('1');
|
77 | *
|
78 | * notification.create(function(err, notification, apiResponse) {
|
79 | * if (!err) {
|
80 | * // The notification was created successfully.
|
81 | * }
|
82 | * });
|
83 | *
|
84 | * //-
|
85 | * // If the callback is omitted, we'll return a Promise.
|
86 | * //-
|
87 | * notification.create().then(function(data) {
|
88 | * const notification = data[0];
|
89 | * const apiResponse = data[1];
|
90 | * });
|
91 | * ```
|
92 | */
|
93 | create: true,
|
94 | /**
|
95 | * @typedef {array} DeleteNotificationResponse
|
96 | * @property {object} 0 The full API response.
|
97 | */
|
98 | /**
|
99 | * Permanently deletes a notification subscription.
|
100 | *
|
101 | * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/delete| Notifications: delete API Documentation}
|
102 | *
|
103 | * @param {object} [options] Configuration options.
|
104 | * @param {string} [options.userProject] The ID of the project which will be
|
105 | * billed for the request.
|
106 | * @param {DeleteNotificationCallback} [callback] Callback function.
|
107 | * @returns {Promise<DeleteNotificationResponse>}
|
108 | *
|
109 | * @example
|
110 | * ```
|
111 | * const {Storage} = require('@google-cloud/storage');
|
112 | * const storage = new Storage();
|
113 | * const myBucket = storage.bucket('my-bucket');
|
114 | * const notification = myBucket.notification('1');
|
115 | *
|
116 | * notification.delete(function(err, apiResponse) {});
|
117 | *
|
118 | * //-
|
119 | * // If the callback is omitted, we'll return a Promise.
|
120 | * //-
|
121 | * notification.delete().then(function(data) {
|
122 | * const apiResponse = data[0];
|
123 | * });
|
124 | *
|
125 | * ```
|
126 | * @example <caption>include:samples/deleteNotification.js</caption>
|
127 | * region_tag:storage_delete_bucket_notification
|
128 | * Another example:
|
129 | */
|
130 | delete: {
|
131 | reqOpts: {
|
132 | qs: requestQueryObject,
|
133 | },
|
134 | },
|
135 | /**
|
136 | * Get a notification and its metadata if it exists.
|
137 | *
|
138 | * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/get| Notifications: get API Documentation}
|
139 | *
|
140 | * @param {object} [options] Configuration options.
|
141 | * See {@link Bucket#createNotification} for create options.
|
142 | * @param {boolean} [options.autoCreate] Automatically create the object if
|
143 | * it does not exist. Default: `false`.
|
144 | * @param {string} [options.userProject] The ID of the project which will be
|
145 | * billed for the request.
|
146 | * @param {GetNotificationCallback} [callback] Callback function.
|
147 | * @return {Promise<GetNotificationCallback>}
|
148 | *
|
149 | * @example
|
150 | * ```
|
151 | * const {Storage} = require('@google-cloud/storage');
|
152 | * const storage = new Storage();
|
153 | * const myBucket = storage.bucket('my-bucket');
|
154 | * const notification = myBucket.notification('1');
|
155 | *
|
156 | * notification.get(function(err, notification, apiResponse) {
|
157 | * // `notification.metadata` has been populated.
|
158 | * });
|
159 | *
|
160 | * //-
|
161 | * // If the callback is omitted, we'll return a Promise.
|
162 | * //-
|
163 | * notification.get().then(function(data) {
|
164 | * const notification = data[0];
|
165 | * const apiResponse = data[1];
|
166 | * });
|
167 | * ```
|
168 | */
|
169 | get: {
|
170 | reqOpts: {
|
171 | qs: requestQueryObject,
|
172 | },
|
173 | },
|
174 | /**
|
175 | * Get the notification's metadata.
|
176 | *
|
177 | * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/get| Notifications: get API Documentation}
|
178 | *
|
179 | * @param {object} [options] Configuration options.
|
180 | * @param {string} [options.userProject] The ID of the project which will be
|
181 | * billed for the request.
|
182 | * @param {GetNotificationMetadataCallback} [callback] Callback function.
|
183 | * @returns {Promise<GetNotificationMetadataResponse>}
|
184 | *
|
185 | * @example
|
186 | * ```
|
187 | * const {Storage} = require('@google-cloud/storage');
|
188 | * const storage = new Storage();
|
189 | * const myBucket = storage.bucket('my-bucket');
|
190 | * const notification = myBucket.notification('1');
|
191 | *
|
192 | * notification.getMetadata(function(err, metadata, apiResponse) {});
|
193 | *
|
194 | * //-
|
195 | * // If the callback is omitted, we'll return a Promise.
|
196 | * //-
|
197 | * notification.getMetadata().then(function(data) {
|
198 | * const metadata = data[0];
|
199 | * const apiResponse = data[1];
|
200 | * });
|
201 | *
|
202 | * ```
|
203 | * @example <caption>include:samples/getMetadataNotifications.js</caption>
|
204 | * region_tag:storage_print_pubsub_bucket_notification
|
205 | * Another example:
|
206 | */
|
207 | getMetadata: {
|
208 | reqOpts: {
|
209 | qs: requestQueryObject,
|
210 | },
|
211 | },
|
212 | /**
|
213 | * @typedef {array} NotificationExistsResponse
|
214 | * @property {boolean} 0 Whether the notification exists or not.
|
215 | */
|
216 | /**
|
217 | * @callback NotificationExistsCallback
|
218 | * @param {?Error} err Request error, if any.
|
219 | * @param {boolean} exists Whether the notification exists or not.
|
220 | */
|
221 | /**
|
222 | * Check if the notification exists.
|
223 | *
|
224 | * @method Notification#exists
|
225 | * @param {NotificationExistsCallback} [callback] Callback function.
|
226 | * @returns {Promise<NotificationExistsResponse>}
|
227 | *
|
228 | * @example
|
229 | * ```
|
230 | * const {Storage} = require('@google-cloud/storage');
|
231 | * const storage = new Storage();
|
232 | * const myBucket = storage.bucket('my-bucket');
|
233 | * const notification = myBucket.notification('1');
|
234 | *
|
235 | * notification.exists(function(err, exists) {});
|
236 | *
|
237 | * //-
|
238 | * // If the callback is omitted, we'll return a Promise.
|
239 | * //-
|
240 | * notification.exists().then(function(data) {
|
241 | * const exists = data[0];
|
242 | * });
|
243 | * ```
|
244 | */
|
245 | exists: true,
|
246 | };
|
247 | super({
|
248 | parent: bucket,
|
249 | baseUrl: '/notificationConfigs',
|
250 | id: id.toString(),
|
251 | createMethod: bucket.createNotification.bind(bucket),
|
252 | methods,
|
253 | });
|
254 | }
|
255 | }
|
256 | /*! Developer Documentation
|
257 | *
|
258 | * All async methods (except for streams) will return a Promise in the event
|
259 | * that a callback is omitted.
|
260 | */
|
261 | promisifyAll(Notification);
|
262 | /**
|
263 | * Reference to the {@link Notification} class.
|
264 | * @name module:@google-cloud/storage.Notification
|
265 | * @see Notification
|
266 | */
|
267 | export { Notification };
|