Skip to content

Implement Service & Service Object for PubSub #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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