22
22
23
23
var arrify = require ( 'arrify' ) ;
24
24
var common = require ( '@google-cloud/common' ) ;
25
- var commonGrpc = require ( '@google-cloud/common-grpc' ) ;
26
25
var is = require ( 'is' ) ;
27
- var path = require ( 'path' ) ;
28
- var util = require ( 'util' ) ;
29
26
30
27
/*! Developer Documentation
31
28
*
32
29
* @param {module:pubsub } pubsub - PubSub Object.
33
- * @param {object } config - Configuration object.
34
- * @param {string } config.baseUrl - The base URL to apply to API requests.
35
- * @param {string } config.id - The name of the topic or subscription.
30
+ * @param {string } id - The name of the topic or subscription.
36
31
*/
37
32
/**
38
33
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
@@ -68,32 +63,16 @@ var util = require('util');
68
63
* // subscription.iam
69
64
*/
70
65
function IAM ( pubsub , id ) {
71
- var config = {
72
- baseUrl : 'pubsub.googleapis.com' ,
73
- protosDir : path . resolve ( __dirname , '../protos' ) ,
74
- protoServices : {
75
- IAMPolicy : {
76
- path : 'google/iam/v1/iam_policy.proto' ,
77
- service : 'iam.v1'
78
- }
79
- } ,
80
- scopes : [
81
- 'https://www.googleapis.com/auth/pubsub' ,
82
- 'https://www.googleapis.com/auth/cloud-platform'
83
- ] ,
84
- packageJson : require ( '../package.json' )
85
- } ;
86
-
66
+ this . pubsub = pubsub ;
67
+ this . request = pubsub . request . bind ( pubsub ) ;
87
68
this . id = id ;
88
-
89
- commonGrpc . Service . call ( this , config , pubsub . options ) ;
90
69
}
91
70
92
- util . inherits ( IAM , commonGrpc . Service ) ;
93
-
94
71
/**
95
72
* Get the IAM policy
96
73
*
74
+ * @param {object= } gaxOptions - Request configuration options, outlined
75
+ * here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
97
76
* @param {function } callback - The callback function.
98
77
* @param {?error } callback.err - An error returned while making this request.
99
78
* @param {object } callback.policy - The [policy](https://cloud.google.com/pubsub/docs/reference/rest/Shared.Types/Policy).
@@ -115,17 +94,22 @@ util.inherits(IAM, commonGrpc.Service);
115
94
* var apiResponse = data[1];
116
95
* });
117
96
*/
118
- IAM . prototype . getPolicy = function ( callback ) {
119
- var protoOpts = {
120
- service : 'IAMPolicy' ,
121
- method : 'getIamPolicy'
122
- } ;
97
+ IAM . prototype . getPolicy = function ( gaxOpts , callback ) {
98
+ if ( is . fn ( gaxOpts ) ) {
99
+ callback = gaxOpts ;
100
+ gaxOpts = null ;
101
+ }
123
102
124
103
var reqOpts = {
125
104
resource : this . id
126
105
} ;
127
106
128
- this . request ( protoOpts , reqOpts , callback ) ;
107
+ this . request ( {
108
+ client : 'subscriberClient' ,
109
+ method : 'getIamPolicy' ,
110
+ reqOpts : reqOpts ,
111
+ gaxOpts : gaxOpts
112
+ } , callback ) ;
129
113
} ;
130
114
131
115
/**
@@ -137,6 +121,8 @@ IAM.prototype.getPolicy = function(callback) {
137
121
* @param {array= } policy.bindings - Bindings associate members with roles.
138
122
* @param {object[]= } policy.rules - Rules to be applied to the policy.
139
123
* @param {string= } policy.etag - Etags are used to perform a read-modify-write.
124
+ * @param {object= } gaxOptions - Request configuration options, outlined
125
+ * here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
140
126
* @param {function } callback - The callback function.
141
127
* @param {?error } callback.err - An error returned while making this request.
142
128
* @param {object } callback.policy - The updated policy.
@@ -168,22 +154,27 @@ IAM.prototype.getPolicy = function(callback) {
168
154
* var apiResponse = data[1];
169
155
* });
170
156
*/
171
- IAM . prototype . setPolicy = function ( policy , callback ) {
157
+ IAM . prototype . setPolicy = function ( policy , gaxOpts , callback ) {
172
158
if ( ! is . object ( policy ) ) {
173
159
throw new Error ( 'A policy object is required.' ) ;
174
160
}
175
161
176
- var protoOpts = {
177
- service : 'IAMPolicy' ,
178
- method : 'setIamPolicy'
179
- } ;
162
+ if ( is . fn ( gaxOpts ) ) {
163
+ callback = gaxOpts ;
164
+ gaxOpts = null ;
165
+ }
180
166
181
167
var reqOpts = {
182
168
resource : this . id ,
183
- policy : policy
169
+ policy
184
170
} ;
185
171
186
- this . request ( protoOpts , reqOpts , callback ) ;
172
+ this . request ( {
173
+ client : 'subscriberClient' ,
174
+ method : 'setIamPolicy' ,
175
+ reqOpts : reqOpts ,
176
+ gaxOpts : gaxOpts
177
+ } , callback ) ;
187
178
} ;
188
179
189
180
/**
@@ -194,6 +185,8 @@ IAM.prototype.setPolicy = function(policy, callback) {
194
185
* @throws {Error } If permissions are not provided.
195
186
*
196
187
* @param {string|string[] } permissions - The permission(s) to test for.
188
+ * @param {object= } gaxOptions - Request configuration options, outlined
189
+ * here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
197
190
* @param {function } callback - The callback function.
198
191
* @param {?error } callback.err - An error returned while making this request.
199
192
* @param {array } callback.permissions - A subset of permissions that the caller
@@ -241,37 +234,39 @@ IAM.prototype.setPolicy = function(policy, callback) {
241
234
* var apiResponse = data[1];
242
235
* });
243
236
*/
244
- IAM . prototype . testPermissions = function ( permissions , callback ) {
237
+ IAM . prototype . testPermissions = function ( permissions , gaxOpts , callback ) {
245
238
if ( ! is . array ( permissions ) && ! is . string ( permissions ) ) {
246
239
throw new Error ( 'Permissions are required.' ) ;
247
240
}
248
241
249
- permissions = arrify ( permissions ) ;
250
-
251
- var protoOpts = {
252
- service : 'IAMPolicy' ,
253
- method : 'testIamPermissions'
254
- } ;
242
+ if ( is . fn ( gaxOpts ) ) {
243
+ callback = gaxOpts ;
244
+ gaxOpts = null ;
245
+ }
255
246
256
247
var reqOpts = {
257
248
resource : this . id ,
258
- permissions : permissions
249
+ permissions : arrify ( permissions )
259
250
} ;
260
251
261
- this . request ( protoOpts , reqOpts , function ( err , resp ) {
252
+ this . request ( {
253
+ client : 'subscriberClient' ,
254
+ method : 'testIamPermissions' ,
255
+ reqOpts : reqOpts ,
256
+ gaxOpts : gaxOpts
257
+ } , function ( err , resp ) {
262
258
if ( err ) {
263
259
callback ( err , null , resp ) ;
264
260
return ;
265
261
}
266
262
267
263
var availablePermissions = arrify ( resp . permissions ) ;
268
-
269
- var permissionsHash = permissions . reduce ( function ( acc , permission ) {
264
+ var permissionHash = permissions . reduce ( function ( acc , permission ) {
270
265
acc [ permission ] = availablePermissions . indexOf ( permission ) > - 1 ;
271
266
return acc ;
272
267
} , { } ) ;
273
268
274
- callback ( null , permissionsHash , resp ) ;
269
+ callback ( null , permissionHash , resp ) ;
275
270
} ) ;
276
271
} ;
277
272
0 commit comments