Skip to content

Commit 6395c27

Browse files
Implement Service & Service Object for PubSub
1 parent 7374122 commit 6395c27

File tree

10 files changed

+1571
-1277
lines changed

10 files changed

+1571
-1277
lines changed

lib/pubsub/iam.js

+45-24
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020

2121
'use strict';
2222

23-
var is = require('is');
2423
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');
2532

2633
/*! Developer Documentation
2734
*
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.
3039
*/
3140
/**
3241
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
@@ -66,11 +75,19 @@ var arrify = require('arrify');
6675
* var subscription = pubsub.subscription('my-subscription');
6776
* // subscription.iam
6877
*/
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+
});
7287
}
7388

89+
nodeutil.inherits(IAM, ServiceObject);
90+
7491
/**
7592
* Get the IAM policy
7693
*
@@ -90,9 +107,9 @@ function IAM(pubsub, resource) {
90107
* subscription.iam.getPolicy(function(err, policy, apiResponse) {});
91108
*/
92109
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) {
96113
if (err) {
97114
callback(err, null, resp);
98115
return;
@@ -138,15 +155,16 @@ IAM.prototype.getPolicy = function(callback) {
138155
*/
139156
IAM.prototype.setPolicy = function(policy, callback) {
140157
if (!is.object(policy)) {
141-
throw new Error('A policy is required');
158+
throw new Error('A policy object is required.');
142159
}
143160

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) {
150168
if (err) {
151169
callback(err, null, resp);
152170
return;
@@ -207,23 +225,26 @@ IAM.prototype.setPolicy = function(policy, callback) {
207225
*/
208226
IAM.prototype.testPermissions = function(permissions, callback) {
209227
if (!is.array(permissions) && !is.string(permissions)) {
210-
throw new Error('Permissions are required');
228+
throw new Error('Permissions are required.');
211229
}
212230

213-
var path = this.resource + ':testIamPermissions';
214-
var body = {
215-
permissions: arrify(permissions)
216-
};
231+
permissions = arrify(permissions);
217232

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) {
219240
if (err) {
220241
callback(err, null, resp);
221242
return;
222243
}
223244

224-
var availablePermissions = resp.permissions || [];
245+
var availablePermissions = arrify(resp.permissions);
225246

226-
var permissionsHash = body.permissions.reduce(function(acc, permission) {
247+
var permissionsHash = permissions.reduce(function(acc, permission) {
227248
acc[permission] = availablePermissions.indexOf(permission) > -1;
228249
return acc;
229250
}, {});

0 commit comments

Comments
 (0)