Skip to content

Commit 2203209

Browse files
callmehiphopstephenplusplus
authored andcommitted
pubsub: convert iam to use gax (#2744)
* pubsub: convert iam to use gax * remove dependency on common-grpc * pin grpc to 1.7.1
1 parent 25af76e commit 2203209

File tree

7 files changed

+127
-117
lines changed

7 files changed

+127
-117
lines changed

packages/common-grpc/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"dot-prop": "^2.4.0",
4343
"duplexify": "^3.5.1",
4444
"extend": "^3.0.0",
45-
"grpc": "^1.6.0",
45+
"grpc": "^1.7.1",
4646
"is": "^3.2.0",
4747
"modelo": "^4.2.0",
4848
"retry-request": "^3.0.0",

packages/pubsub/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
],
5353
"dependencies": {
5454
"@google-cloud/common": "^0.13.0",
55-
"@google-cloud/common-grpc": "^0.4.2",
5655
"arrify": "^1.0.0",
5756
"async-each": "^1.0.1",
5857
"extend": "^3.0.0",

packages/pubsub/src/connection-pool.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var arrify = require('arrify');
2424
var common = require('@google-cloud/common');
2525
var each = require('async-each');
2626
var events = require('events');
27-
var grpc = require('@google-cloud/common-grpc').grpc;
27+
var grpc = require('google-gax').grpc().grpc;
2828
var is = require('is');
2929
var util = require('util');
3030
var uuid = require('uuid');

packages/pubsub/src/iam.js

+46-51
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@
2222

2323
var arrify = require('arrify');
2424
var common = require('@google-cloud/common');
25-
var commonGrpc = require('@google-cloud/common-grpc');
2625
var is = require('is');
27-
var path = require('path');
28-
var util = require('util');
2926

3027
/*! Developer Documentation
3128
*
3229
* @param {module:pubsub} pubsub - PubSub Object.
33-
* @param {object} config - Configuration object.
34-
* @param {string} config.baseUrl - The base URL to apply to API requests.
35-
* @param {string} config.id - The name of the topic or subscription.
30+
* @param {string} id - The name of the topic or subscription.
3631
*/
3732
/**
3833
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
@@ -68,32 +63,16 @@ var util = require('util');
6863
* // subscription.iam
6964
*/
7065
function IAM(pubsub, id) {
71-
var config = {
72-
baseUrl: 'pubsub.googleapis.com',
73-
protosDir: path.resolve(__dirname, '../protos'),
74-
protoServices: {
75-
IAMPolicy: {
76-
path: 'google/iam/v1/iam_policy.proto',
77-
service: 'iam.v1'
78-
}
79-
},
80-
scopes: [
81-
'https://www.googleapis.com/auth/pubsub',
82-
'https://www.googleapis.com/auth/cloud-platform'
83-
],
84-
packageJson: require('../package.json')
85-
};
86-
66+
this.pubsub = pubsub;
67+
this.request = pubsub.request.bind(pubsub);
8768
this.id = id;
88-
89-
commonGrpc.Service.call(this, config, pubsub.options);
9069
}
9170

92-
util.inherits(IAM, commonGrpc.Service);
93-
9471
/**
9572
* Get the IAM policy
9673
*
74+
* @param {object=} gaxOptions - Request configuration options, outlined
75+
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
9776
* @param {function} callback - The callback function.
9877
* @param {?error} callback.err - An error returned while making this request.
9978
* @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);
11594
* var apiResponse = data[1];
11695
* });
11796
*/
118-
IAM.prototype.getPolicy = function(callback) {
119-
var protoOpts = {
120-
service: 'IAMPolicy',
121-
method: 'getIamPolicy'
122-
};
97+
IAM.prototype.getPolicy = function(gaxOpts, callback) {
98+
if (is.fn(gaxOpts)) {
99+
callback = gaxOpts;
100+
gaxOpts = null;
101+
}
123102

124103
var reqOpts = {
125104
resource: this.id
126105
};
127106

128-
this.request(protoOpts, reqOpts, callback);
107+
this.request({
108+
client: 'subscriberClient',
109+
method: 'getIamPolicy',
110+
reqOpts: reqOpts,
111+
gaxOpts: gaxOpts
112+
}, callback);
129113
};
130114

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

176-
var protoOpts = {
177-
service: 'IAMPolicy',
178-
method: 'setIamPolicy'
179-
};
162+
if (is.fn(gaxOpts)) {
163+
callback = gaxOpts;
164+
gaxOpts = null;
165+
}
180166

181167
var reqOpts = {
182168
resource: this.id,
183-
policy: policy
169+
policy
184170
};
185171

186-
this.request(protoOpts, reqOpts, callback);
172+
this.request({
173+
client: 'subscriberClient',
174+
method: 'setIamPolicy',
175+
reqOpts: reqOpts,
176+
gaxOpts: gaxOpts
177+
}, callback);
187178
};
188179

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

249-
permissions = arrify(permissions);
250-
251-
var protoOpts = {
252-
service: 'IAMPolicy',
253-
method: 'testIamPermissions'
254-
};
242+
if (is.fn(gaxOpts)) {
243+
callback = gaxOpts;
244+
gaxOpts = null;
245+
}
255246

256247
var reqOpts = {
257248
resource: this.id,
258-
permissions: permissions
249+
permissions: arrify(permissions)
259250
};
260251

261-
this.request(protoOpts, reqOpts, function(err, resp) {
252+
this.request({
253+
client: 'subscriberClient',
254+
method: 'testIamPermissions',
255+
reqOpts: reqOpts,
256+
gaxOpts: gaxOpts
257+
}, function(err, resp) {
262258
if (err) {
263259
callback(err, null, resp);
264260
return;
265261
}
266262

267263
var availablePermissions = arrify(resp.permissions);
268-
269-
var permissionsHash = permissions.reduce(function(acc, permission) {
264+
var permissionHash = permissions.reduce(function(acc, permission) {
270265
acc[permission] = availablePermissions.indexOf(permission) > -1;
271266
return acc;
272267
}, {});
273268

274-
callback(null, permissionsHash, resp);
269+
callback(null, permissionHash, resp);
275270
});
276271
};
277272

packages/pubsub/system-test/pubsub.js

-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ describe('pubsub', function() {
513513
assert.ifError(err);
514514

515515
assert.deepEqual(policy.bindings, []);
516-
assert.strictEqual(policy.etag, 'ACAB');
517516
assert.strictEqual(policy.version, 0);
518517

519518
done();

packages/pubsub/test/connection-pool.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ var fakeUtil = extend({}, common.util);
3232
var fakeUuid = extend({}, uuid);
3333
var fakeGrpc = extend({}, grpc);
3434

35+
function fakeGaxGrpc() {
36+
return {
37+
grpc: fakeGrpc
38+
};
39+
}
40+
3541
var v1Override;
3642
function fakeV1(options) {
3743
return (v1Override || v1)(options);
@@ -95,8 +101,8 @@ describe('ConnectionPool', function() {
95101
'@google-cloud/common': {
96102
util: fakeUtil
97103
},
98-
'@google-cloud/common-grpc': {
99-
grpc: fakeGrpc
104+
'google-gax': {
105+
grpc: fakeGaxGrpc
100106
},
101107
uuid: fakeUuid,
102108
'./v1': fakeV1

0 commit comments

Comments
 (0)