Skip to content

Commit

Permalink
Merge pull request #944 from stephenplusplus/spp--ServiceObject-PubSub
Browse files Browse the repository at this point in the history
Implement Service & Service Object for PubSub
  • Loading branch information
callmehiphop committed Nov 14, 2015
2 parents 2336713 + 6395c27 commit 2872df8
Show file tree
Hide file tree
Showing 10 changed files with 1,571 additions and 1,277 deletions.
69 changes: 45 additions & 24 deletions lib/pubsub/iam.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@

'use strict';

var is = require('is');
var arrify = require('arrify');
var is = require('is');
var nodeutil = require('util');

/**
* @type {module:common/serviceObject}
* @private
*/
var ServiceObject = require('../common/service-object.js');

/*! Developer Documentation
*
* @param {module:pubsub} pubsub - PubSub Object
* @param {string} resource - topic or subscription name
* @param {module:pubsub} pubsub - PubSub Object.
* @param {object} config - Configuration object.
* @param {string} config.baseUrl - The base URL to apply to API requests.
* @param {string} config.id - The name of the topic or subscription.
*/
/**
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
Expand Down Expand Up @@ -66,11 +75,19 @@ var arrify = require('arrify');
* var subscription = pubsub.subscription('my-subscription');
* // subscription.iam
*/
function IAM(pubsub, resource) {
this.resource = resource;
this.makeReq_ = pubsub.makeReq_.bind(pubsub);
function IAM(pubsub, scope) {
ServiceObject.call(this, {
parent: pubsub,
baseUrl: scope.baseUrl,
id: scope.id,
methods: {
// Nothing needed other than the `request` method.
}
});
}

nodeutil.inherits(IAM, ServiceObject);

/**
* Get the IAM policy
*
Expand All @@ -90,9 +107,9 @@ function IAM(pubsub, resource) {
* subscription.iam.getPolicy(function(err, policy, apiResponse) {});
*/
IAM.prototype.getPolicy = function(callback) {
var path = this.resource + ':getIamPolicy';

this.makeReq_('GET', path, null, null, function(err, resp) {
this.request({
uri: ':getIamPolicy'
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand Down Expand Up @@ -138,15 +155,16 @@ IAM.prototype.getPolicy = function(callback) {
*/
IAM.prototype.setPolicy = function(policy, callback) {
if (!is.object(policy)) {
throw new Error('A policy is required');
throw new Error('A policy object is required.');
}

var path = this.resource + ':setIamPolicy';
var body = {
policy: policy
};

this.makeReq_('POST', path, null, body, function(err, resp) {
this.request({
method: 'POST',
uri: ':setIamPolicy',
json: {
policy: policy
}
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand Down Expand Up @@ -207,23 +225,26 @@ IAM.prototype.setPolicy = function(policy, callback) {
*/
IAM.prototype.testPermissions = function(permissions, callback) {
if (!is.array(permissions) && !is.string(permissions)) {
throw new Error('Permissions are required');
throw new Error('Permissions are required.');
}

var path = this.resource + ':testIamPermissions';
var body = {
permissions: arrify(permissions)
};
permissions = arrify(permissions);

this.makeReq_('POST', path, null, body, function(err, resp) {
this.request({
method: 'POST',
uri: ':testIamPermissions',
json: {
permissions: permissions
}
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

var availablePermissions = resp.permissions || [];
var availablePermissions = arrify(resp.permissions);

var permissionsHash = body.permissions.reduce(function(acc, permission) {
var permissionsHash = permissions.reduce(function(acc, permission) {
acc[permission] = availablePermissions.indexOf(permission) > -1;
return acc;
}, {});
Expand Down
Loading

0 comments on commit 2872df8

Please sign in to comment.