1313
1414'use strict' ;
1515
16- // [START auth ]
16+ // [START setup ]
1717// By default, the client will authenticate using the service account file
1818// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
1919// the project specified by the GCLOUD_PROJECT environment variable. See
2020// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
2121var PubSub = require ( '@google-cloud/pubsub' ) ;
22+ // [END setup]
2223
23- // Instantiate a pubsub client
24- var pubsub = PubSub ( ) ;
25- // [END auth]
26-
27- // [START get_topic_policy]
28- /**
29- * Retrieve a topic's IAM policy.
30- *
31- * @param {string } topicName The name of the topic.
32- * @param {function } callback The callback function.
33- */
3424function getTopicPolicy ( topicName , callback ) {
35- if ( ! topicName ) {
36- return callback ( new Error ( '"topicName" is required!' ) ) ;
37- }
38-
39- // Grab a reference to an existing topic
25+ var pubsub = PubSub ( ) ;
4026 var topic = pubsub . topic ( topicName ) ;
4127
4228 // Retrieve the IAM policy for the topic
43- topic . iam . getPolicy ( function ( err , policy ) {
29+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=iam.getPolicy
30+ topic . iam . getPolicy ( function ( err , policy , apiResponse ) {
4431 if ( err ) {
4532 return callback ( err ) ;
4633 }
4734
48- console . log ( 'Got topic policy: ' , policy ) ;
49- return callback ( null , policy ) ;
35+ console . log ( 'Got policy for topic: %s ' , topicName ) ;
36+ return callback ( null , policy , apiResponse ) ;
5037 } ) ;
5138}
52- // [END get_topic_policy]
5339
54- // [START get_subscription_policy]
55- /**
56- * Retrieve a subcription's IAM policy.
57- *
58- * @param {string } subscriptionName The name of the subscription.
59- * @param {function } callback The callback function.
60- */
6140function getSubscriptionPolicy ( subscriptionName , callback ) {
62- if ( ! subscriptionName ) {
63- return callback ( new Error ( '"subscriptionName" is required!' ) ) ;
64- }
65-
66- // Grab a reference to an existing subscription
41+ var pubsub = PubSub ( ) ;
6742 var subscription = pubsub . subscription ( subscriptionName ) ;
6843
6944 // Retrieve the IAM policy for the subscription
70- subscription . iam . getPolicy ( function ( err , policy ) {
45+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/subscription?method=iam.getPolicy
46+ subscription . iam . getPolicy ( function ( err , policy , apiResponse ) {
7147 if ( err ) {
7248 return callback ( err ) ;
7349 }
7450
75- console . log ( 'Got subscription policy: ' , policy ) ;
76- return callback ( null , policy ) ;
51+ console . log ( 'Got policy for subscription: %s ' , subscriptionName ) ;
52+ return callback ( null , policy , apiResponse ) ;
7753 } ) ;
7854}
79- // [END get_subscription_policy]
8055
81- // [START set_topic_policy]
82- /**
83- * Set a topic's IAM policy.
84- *
85- * @param {string } topicName The name of the topic.
86- * @param {function } callback The callback function.
87- */
8856function setTopicPolicy ( topicName , callback ) {
89- if ( ! topicName ) {
90- return callback ( new Error ( '"topicName" is required!' ) ) ;
91- }
92-
93- // Grab a reference to an existing topic
57+ var pubsub = PubSub ( ) ;
9458 var topic = pubsub . topic ( topicName ) ;
9559
9660 // Policy update
@@ -104,29 +68,19 @@ function setTopicPolicy (topicName, callback) {
10468 } ;
10569
10670 // Set the IAM policy for the specified topic
107- topic . iam . setPolicy ( newPolicy , function ( err , policy ) {
71+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=iam.setPolicy
72+ topic . iam . setPolicy ( newPolicy , function ( err , updatedPolicy , apiResponse ) {
10873 if ( err ) {
10974 return callback ( err ) ;
11075 }
11176
11277 console . log ( 'Updated policy for topic: %s' , topicName ) ;
113- return callback ( null , policy ) ;
78+ return callback ( null , updatedPolicy , apiResponse ) ;
11479 } ) ;
11580}
116- // [END set_topic_policy]
11781
118- // [START set_subscription_policy]
119- /**
120- * @param {string } subscriptionName Name of the subscription whose policy is to
121- * be updated.
122- * @param {function } callback The callback function.
123- */
12482function setSubscriptionPolicy ( subscriptionName , callback ) {
125- if ( ! subscriptionName ) {
126- return callback ( new Error ( '"subscriptionName" is required!' ) ) ;
127- }
128-
129- // Grab a reference to an existing subscription
83+ var pubsub = PubSub ( ) ;
13084 var subscription = pubsub . subscription ( subscriptionName ) ;
13185
13286 // Policy update
@@ -140,30 +94,19 @@ function setSubscriptionPolicy (subscriptionName, callback) {
14094 } ;
14195
14296 // Set the IAM policy for the specified subscription
143- subscription . iam . setPolicy ( newPolicy , function ( err , policy ) {
97+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/subscription?method=iam.setPolicy
98+ subscription . iam . setPolicy ( newPolicy , function ( err , updatedPolicy , apiResponse ) {
14499 if ( err ) {
145100 return callback ( err ) ;
146101 }
147102
148103 console . log ( 'Updated policy for subscription: %s' , subscriptionName ) ;
149- return callback ( null , policy ) ;
104+ return callback ( null , updatedPolicy , apiResponse ) ;
150105 } ) ;
151106}
152- // [END set_subscription_policy]
153107
154- // [START test_topic_permissions]
155- /**
156- * Test a topic's IAM permissions.
157- *
158- * @param {string } topicName The name of the topic.
159- * @param {function } callback The callback function.
160- */
161108function testTopicPermissions ( topicName , callback ) {
162- if ( ! topicName ) {
163- return callback ( new Error ( '"topicName" is required!' ) ) ;
164- }
165-
166- // Grab a reference to an existing topic
109+ var pubsub = PubSub ( ) ;
167110 var topic = pubsub . topic ( topicName ) ;
168111
169112 var permissionsToTest = [
@@ -173,30 +116,19 @@ function testTopicPermissions (topicName, callback) {
173116 ] ;
174117
175118 // Test the IAM policy for the specified topic
176- topic . iam . testPermissions ( permissionsToTest , function ( err , permissions ) {
119+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=iam.testPermissions
120+ topic . iam . testPermissions ( permissionsToTest , function ( err , permissions , apiResponse ) {
177121 if ( err ) {
178122 return callback ( err ) ;
179123 }
180124
181125 console . log ( 'Tested permissions for topic: %s' , topicName ) ;
182- return callback ( null , permissions ) ;
126+ return callback ( null , permissions , apiResponse ) ;
183127 } ) ;
184128}
185- // [END test_topic_permissions]
186129
187- // [START test_subscription_permissions]
188- /**
189- * Test a subcription's IAM permissions.
190- *
191- * @param {string } subscriptionName The name of the subscription.
192- * @param {function } callback The callback function.
193- */
194130function testSubscriptionPermissions ( subscriptionName , callback ) {
195- if ( ! subscriptionName ) {
196- return callback ( new Error ( '"subscriptionName" is required!' ) ) ;
197- }
198-
199- // Grab a reference to an existing subscription
131+ var pubsub = PubSub ( ) ;
200132 var subscription = pubsub . subscription ( subscriptionName ) ;
201133
202134 var permissionsToTest = [
@@ -205,16 +137,16 @@ function testSubscriptionPermissions (subscriptionName, callback) {
205137 ] ;
206138
207139 // Test the IAM policy for the specified subscription
208- subscription . iam . testPermissions ( permissionsToTest , function ( err , permissions ) {
140+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/subscription?method=iam.testPermissions
141+ subscription . iam . testPermissions ( permissionsToTest , function ( err , permissions , apiResponse ) {
209142 if ( err ) {
210143 return callback ( err ) ;
211144 }
212145
213146 console . log ( 'Tested permissions for subscription: %s' , subscriptionName ) ;
214- return callback ( null , permissions ) ;
147+ return callback ( null , permissions , apiResponse ) ;
215148 } ) ;
216149}
217- // [END test_subscription_permissions]
218150
219151// [START usage]
220152function printUsage ( ) {
0 commit comments