diff --git a/package.json b/package.json index 0c7f0782a..abc23c297 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/connection-pool.js b/src/connection-pool.js index aafe4a639..a4616c417 100644 --- a/src/connection-pool.js +++ b/src/connection-pool.js @@ -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'); diff --git a/src/iam.js b/src/iam.js index a64a32fb8..c0b81953a 100644 --- a/src/iam.js +++ b/src/iam.js @@ -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) @@ -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). @@ -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); }; /** @@ -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. @@ -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); }; /** @@ -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 @@ -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); }); }; diff --git a/system-test/pubsub.js b/system-test/pubsub.js index 81eb3f854..fcef40d62 100644 --- a/system-test/pubsub.js +++ b/system-test/pubsub.js @@ -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(); diff --git a/test/connection-pool.js b/test/connection-pool.js index a220fb5fe..c26708b62 100644 --- a/test/connection-pool.js +++ b/test/connection-pool.js @@ -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); @@ -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 diff --git a/test/iam.js b/test/iam.js index 25dde7ed8..7b07a8312 100644 --- a/test/iam.js +++ b/test/iam.js @@ -18,9 +18,6 @@ var assert = require('assert'); var extend = require('extend'); -var GrpcService = require('@google-cloud/common-grpc').Service; -var nodeutil = require('util'); -var path = require('path'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; @@ -33,19 +30,13 @@ var fakeUtil = extend({}, util, { } }); -function FakeGrpcService() { - this.calledWith_ = arguments; - GrpcService.apply(this, arguments); -} - -nodeutil.inherits(FakeGrpcService, GrpcService); - describe('IAM', function() { var IAM; var iam; var PUBSUB = { - options: {} + options: {}, + request: util.noop }; var ID = 'id'; @@ -53,9 +44,6 @@ describe('IAM', function() { IAM = proxyquire('../src/iam.js', { '@google-cloud/common': { util: fakeUtil - }, - '@google-cloud/common-grpc': { - Service: FakeGrpcService } }); }); @@ -65,58 +53,62 @@ describe('IAM', function() { }); describe('initialization', function() { - it('should inherit from GrpcService', function() { - assert(iam instanceof GrpcService); - - var config = iam.calledWith_[0]; - var options = iam.calledWith_[1]; - - assert.strictEqual(config.baseUrl, 'pubsub.googleapis.com'); - - var protosDir = path.resolve(__dirname, '../protos'); - assert.strictEqual(config.protosDir, protosDir); + it('should localize pubsub', function() { + assert.strictEqual(iam.pubsub, PUBSUB); + }); - assert.deepStrictEqual(config.protoServices, { - IAMPolicy: { - path: 'google/iam/v1/iam_policy.proto', - service: 'iam.v1' + it('should localize pubsub#request', function() { + var fakeRequest = function() {}; + var fakePubsub = { + request: { + bind: function(context) { + assert.strictEqual(context, fakePubsub); + return fakeRequest; + } } - }); + }; + var iam = new IAM(fakePubsub, ID); - assert.deepEqual(config.scopes, [ - 'https://www.googleapis.com/auth/pubsub', - 'https://www.googleapis.com/auth/cloud-platform' - ]); - assert.deepEqual(config.packageJson, require('../package.json')); + assert.strictEqual(iam.request, fakeRequest); + }); - assert.strictEqual(options, PUBSUB.options); + it('should localize the ID', function() { + assert.strictEqual(iam.id, ID); }); it('should promisify all the things', function() { assert(promisified); }); - - it('should localize the ID', function() { - assert.strictEqual(iam.id, ID); - }); }); describe('getPolicy', function() { it('should make the correct API request', function(done) { - iam.request = function(protoOpts, reqOpts, callback) { - assert.strictEqual(protoOpts.service, 'IAMPolicy'); - assert.strictEqual(protoOpts.method, 'getIamPolicy'); - - assert.strictEqual(reqOpts.resource, iam.id); + iam.request = function(config, callback) { + assert.strictEqual(config.client, 'subscriberClient'); + assert.strictEqual(config.method, 'getIamPolicy'); + assert.strictEqual(config.reqOpts.resource, iam.id); callback(); // done() }; iam.getPolicy(done); }); + + it('should accept gax options', function(done) { + var gaxOpts = {}; + + iam.request = function(config) { + assert.strictEqual(config.gaxOpts, gaxOpts); + done(); + }; + + iam.getPolicy(gaxOpts, assert.ifError); + }); }); describe('setPolicy', function() { + var policy = { etag: 'ACAB' }; + it('should throw an error if a policy is not supplied', function() { assert.throws(function() { iam.setPolicy(util.noop); @@ -124,20 +116,28 @@ describe('IAM', function() { }); it('should make the correct API request', function(done) { - var policy = { etag: 'ACAB' }; - - iam.request = function(protoOpts, reqOpts, callback) { - assert.strictEqual(protoOpts.service, 'IAMPolicy'); - assert.strictEqual(protoOpts.method, 'setIamPolicy'); - - assert.strictEqual(reqOpts.resource, iam.id); - assert.strictEqual(reqOpts.policy, policy); + iam.request = function(config, callback) { + assert.strictEqual(config.client, 'subscriberClient'); + assert.strictEqual(config.method, 'setIamPolicy'); + assert.strictEqual(config.reqOpts.resource, iam.id); + assert.strictEqual(config.reqOpts.policy, policy); callback(); // done() }; iam.setPolicy(policy, done); }); + + it('should accept gax options', function(done) { + var gaxOpts = {}; + + iam.request = function(config) { + assert.strictEqual(config.gaxOpts, gaxOpts); + done(); + }; + + iam.setPolicy(policy, gaxOpts, assert.ifError); + }); }); describe('testPermissions', function() { @@ -150,12 +150,11 @@ describe('IAM', function() { it('should make the correct API request', function(done) { var permissions = 'storage.bucket.list'; - iam.request = function(protoOpts, reqOpts) { - assert.strictEqual(protoOpts.service, 'IAMPolicy'); - assert.strictEqual(protoOpts.method, 'testIamPermissions'); - - assert.strictEqual(reqOpts.resource, iam.id); - assert.deepEqual(reqOpts.permissions, [permissions]); + iam.request = function(config) { + assert.strictEqual(config.client, 'subscriberClient'); + assert.strictEqual(config.method, 'testIamPermissions'); + assert.strictEqual(config.reqOpts.resource, iam.id); + assert.deepEqual(config.reqOpts.permissions, [permissions]); done(); }; @@ -163,12 +162,24 @@ describe('IAM', function() { iam.testPermissions(permissions, assert.ifError); }); + it('should accept gax options', function(done) { + var permissions = 'storage.bucket.list'; + var gaxOpts = {}; + + iam.request = function(config) { + assert.strictEqual(config.gaxOpts, gaxOpts); + done(); + }; + + iam.testPermissions(permissions, gaxOpts, assert.ifError); + }); + it('should send an error back if the request fails', function(done) { var permissions = ['storage.bucket.list']; var error = new Error('Error.'); var apiResponse = {}; - iam.request = function(protoOpts, reqOpts, callback) { + iam.request = function(config, callback) { callback(error, apiResponse); }; @@ -189,7 +200,7 @@ describe('IAM', function() { permissions: ['storage.bucket.consume'] }; - iam.request = function(protoOpts, reqOpts, callback) { + iam.request = function(config, callback) { callback(null, apiResponse); };