20
20
21
21
'use strict' ;
22
22
23
- var is = require ( 'is' ) ;
24
23
var arrify = require ( 'arrify' ) ;
24
+ var is = require ( 'is' ) ;
25
+ var nodeutil = require ( 'util' ) ;
26
+
27
+ /**
28
+ * @type {module:common/serviceObject }
29
+ * @private
30
+ */
31
+ var ServiceObject = require ( '../common/service-object.js' ) ;
25
32
26
33
/*! Developer Documentation
27
34
*
28
- * @param {module:pubsub } pubsub - PubSub Object
29
- * @param {string } resource - topic or subscription name
35
+ * @param {module:pubsub } pubsub - PubSub Object.
36
+ * @param {object } config - Configuration object.
37
+ * @param {string } config.baseUrl - The base URL to apply to API requests.
38
+ * @param {string } config.id - The name of the topic or subscription.
30
39
*/
31
40
/**
32
41
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
@@ -66,11 +75,19 @@ var arrify = require('arrify');
66
75
* var subscription = pubsub.subscription('my-subscription');
67
76
* // subscription.iam
68
77
*/
69
- function IAM ( pubsub , resource ) {
70
- this . resource = resource ;
71
- this . makeReq_ = pubsub . makeReq_ . bind ( pubsub ) ;
78
+ function IAM ( pubsub , scope ) {
79
+ ServiceObject . call ( this , {
80
+ parent : pubsub ,
81
+ baseUrl : scope . baseUrl ,
82
+ id : scope . id ,
83
+ methods : {
84
+ // Nothing needed other than the `request` method.
85
+ }
86
+ } ) ;
72
87
}
73
88
89
+ nodeutil . inherits ( IAM , ServiceObject ) ;
90
+
74
91
/**
75
92
* Get the IAM policy
76
93
*
@@ -90,9 +107,9 @@ function IAM(pubsub, resource) {
90
107
* subscription.iam.getPolicy(function(err, policy, apiResponse) {});
91
108
*/
92
109
IAM . prototype . getPolicy = function ( callback ) {
93
- var path = this . resource + ':getIamPolicy' ;
94
-
95
- this . makeReq_ ( 'GET' , path , null , null , function ( err , resp ) {
110
+ this . request ( {
111
+ uri : ':getIamPolicy'
112
+ } , function ( err , resp ) {
96
113
if ( err ) {
97
114
callback ( err , null , resp ) ;
98
115
return ;
@@ -138,15 +155,16 @@ IAM.prototype.getPolicy = function(callback) {
138
155
*/
139
156
IAM . prototype . setPolicy = function ( policy , callback ) {
140
157
if ( ! is . object ( policy ) ) {
141
- throw new Error ( 'A policy is required' ) ;
158
+ throw new Error ( 'A policy object is required. ' ) ;
142
159
}
143
160
144
- var path = this . resource + ':setIamPolicy' ;
145
- var body = {
146
- policy : policy
147
- } ;
148
-
149
- this . makeReq_ ( 'POST' , path , null , body , function ( err , resp ) {
161
+ this . request ( {
162
+ method : 'POST' ,
163
+ uri : ':setIamPolicy' ,
164
+ json : {
165
+ policy : policy
166
+ }
167
+ } , function ( err , resp ) {
150
168
if ( err ) {
151
169
callback ( err , null , resp ) ;
152
170
return ;
@@ -207,23 +225,26 @@ IAM.prototype.setPolicy = function(policy, callback) {
207
225
*/
208
226
IAM . prototype . testPermissions = function ( permissions , callback ) {
209
227
if ( ! is . array ( permissions ) && ! is . string ( permissions ) ) {
210
- throw new Error ( 'Permissions are required' ) ;
228
+ throw new Error ( 'Permissions are required. ' ) ;
211
229
}
212
230
213
- var path = this . resource + ':testIamPermissions' ;
214
- var body = {
215
- permissions : arrify ( permissions )
216
- } ;
231
+ permissions = arrify ( permissions ) ;
217
232
218
- this . makeReq_ ( 'POST' , path , null , body , function ( err , resp ) {
233
+ this . request ( {
234
+ method : 'POST' ,
235
+ uri : ':testIamPermissions' ,
236
+ json : {
237
+ permissions : permissions
238
+ }
239
+ } , function ( err , resp ) {
219
240
if ( err ) {
220
241
callback ( err , null , resp ) ;
221
242
return ;
222
243
}
223
244
224
- var availablePermissions = resp . permissions || [ ] ;
245
+ var availablePermissions = arrify ( resp . permissions ) ;
225
246
226
- var permissionsHash = body . permissions . reduce ( function ( acc , permission ) {
247
+ var permissionsHash = permissions . reduce ( function ( acc , permission ) {
227
248
acc [ permission ] = availablePermissions . indexOf ( permission ) > - 1 ;
228
249
return acc ;
229
250
} , { } ) ;
0 commit comments