Skip to content

Commit

Permalink
pubsub: convert iam to use gax (#2744)
Browse files Browse the repository at this point in the history
* pubsub: convert iam to use gax

* remove dependency on common-grpc

* pin grpc to 1.7.1
  • Loading branch information
callmehiphop authored and stephenplusplus committed Nov 16, 2017
1 parent 82b4674 commit c43f414
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 116 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
],
"dependencies": {
"@google-cloud/common": "^0.13.0",
"@google-cloud/common-grpc": "^0.4.2",
"arrify": "^1.0.0",
"async-each": "^1.0.1",
"extend": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var arrify = require('arrify');
var common = require('@google-cloud/common');
var each = require('async-each');
var events = require('events');
var grpc = require('@google-cloud/common-grpc').grpc;
var grpc = require('google-gax').grpc().grpc;
var is = require('is');
var util = require('util');
var uuid = require('uuid');
Expand Down
97 changes: 46 additions & 51 deletions src/iam.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,12 @@

var arrify = require('arrify');
var common = require('@google-cloud/common');
var commonGrpc = require('@google-cloud/common-grpc');
var is = require('is');
var path = require('path');
var util = require('util');

/*! Developer Documentation
*
* @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.
* @param {string} id - The name of the topic or subscription.
*/
/**
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
Expand Down Expand Up @@ -68,32 +63,16 @@ var util = require('util');
* // subscription.iam
*/
function IAM(pubsub, id) {
var config = {
baseUrl: 'pubsub.googleapis.com',
protosDir: path.resolve(__dirname, '../protos'),
protoServices: {
IAMPolicy: {
path: 'google/iam/v1/iam_policy.proto',
service: 'iam.v1'
}
},
scopes: [
'https://www.googleapis.com/auth/pubsub',
'https://www.googleapis.com/auth/cloud-platform'
],
packageJson: require('../package.json')
};

this.pubsub = pubsub;
this.request = pubsub.request.bind(pubsub);
this.id = id;

commonGrpc.Service.call(this, config, pubsub.options);
}

util.inherits(IAM, commonGrpc.Service);

/**
* Get the IAM policy
*
* @param {object=} gaxOptions - Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {object} callback.policy - The [policy](https://cloud.google.com/pubsub/docs/reference/rest/Shared.Types/Policy).
Expand All @@ -115,17 +94,22 @@ util.inherits(IAM, commonGrpc.Service);
* var apiResponse = data[1];
* });
*/
IAM.prototype.getPolicy = function(callback) {
var protoOpts = {
service: 'IAMPolicy',
method: 'getIamPolicy'
};
IAM.prototype.getPolicy = function(gaxOpts, callback) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}

var reqOpts = {
resource: this.id
};

this.request(protoOpts, reqOpts, callback);
this.request({
client: 'subscriberClient',
method: 'getIamPolicy',
reqOpts: reqOpts,
gaxOpts: gaxOpts
}, callback);
};

/**
Expand All @@ -137,6 +121,8 @@ IAM.prototype.getPolicy = function(callback) {
* @param {array=} policy.bindings - Bindings associate members with roles.
* @param {object[]=} policy.rules - Rules to be applied to the policy.
* @param {string=} policy.etag - Etags are used to perform a read-modify-write.
* @param {object=} gaxOptions - Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {object} callback.policy - The updated policy.
Expand Down Expand Up @@ -168,22 +154,27 @@ IAM.prototype.getPolicy = function(callback) {
* var apiResponse = data[1];
* });
*/
IAM.prototype.setPolicy = function(policy, callback) {
IAM.prototype.setPolicy = function(policy, gaxOpts, callback) {
if (!is.object(policy)) {
throw new Error('A policy object is required.');
}

var protoOpts = {
service: 'IAMPolicy',
method: 'setIamPolicy'
};
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}

var reqOpts = {
resource: this.id,
policy: policy
policy
};

this.request(protoOpts, reqOpts, callback);
this.request({
client: 'subscriberClient',
method: 'setIamPolicy',
reqOpts: reqOpts,
gaxOpts: gaxOpts
}, callback);
};

/**
Expand All @@ -194,6 +185,8 @@ IAM.prototype.setPolicy = function(policy, callback) {
* @throws {Error} If permissions are not provided.
*
* @param {string|string[]} permissions - The permission(s) to test for.
* @param {object=} gaxOptions - Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {array} callback.permissions - A subset of permissions that the caller
Expand Down Expand Up @@ -241,37 +234,39 @@ IAM.prototype.setPolicy = function(policy, callback) {
* var apiResponse = data[1];
* });
*/
IAM.prototype.testPermissions = function(permissions, callback) {
IAM.prototype.testPermissions = function(permissions, gaxOpts, callback) {
if (!is.array(permissions) && !is.string(permissions)) {
throw new Error('Permissions are required.');
}

permissions = arrify(permissions);

var protoOpts = {
service: 'IAMPolicy',
method: 'testIamPermissions'
};
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}

var reqOpts = {
resource: this.id,
permissions: permissions
permissions: arrify(permissions)
};

this.request(protoOpts, reqOpts, function(err, resp) {
this.request({
client: 'subscriberClient',
method: 'testIamPermissions',
reqOpts: reqOpts,
gaxOpts: gaxOpts
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

var availablePermissions = arrify(resp.permissions);

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

callback(null, permissionsHash, resp);
callback(null, permissionHash, resp);
});
};

Expand Down
1 change: 0 additions & 1 deletion system-test/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ describe('pubsub', function() {
assert.ifError(err);

assert.deepEqual(policy.bindings, []);
assert.strictEqual(policy.etag, 'ACAB');
assert.strictEqual(policy.version, 0);

done();
Expand Down
10 changes: 8 additions & 2 deletions test/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ var fakeUtil = extend({}, common.util);
var fakeUuid = extend({}, uuid);
var fakeGrpc = extend({}, grpc);

function fakeGaxGrpc() {
return {
grpc: fakeGrpc
};
}

var v1Override;
function fakeV1(options) {
return (v1Override || v1)(options);
Expand Down Expand Up @@ -95,8 +101,8 @@ describe('ConnectionPool', function() {
'@google-cloud/common': {
util: fakeUtil
},
'@google-cloud/common-grpc': {
grpc: fakeGrpc
'google-gax': {
grpc: fakeGaxGrpc
},
uuid: fakeUuid,
'./v1': fakeV1
Expand Down
Loading

0 comments on commit c43f414

Please sign in to comment.