Skip to content

Commit

Permalink
feat: load protos from JSON, grpc-fallback support
Browse files Browse the repository at this point in the history
* [CHANGE ME] Re-generated  to pick up changes in the API or client library generator.

* fixes

* fix webpack.config.js
  • Loading branch information
yoshi-automation authored and alexander-fenster committed Sep 3, 2019
1 parent fbd6ba3 commit 43cc80e
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 98 deletions.
134 changes: 67 additions & 67 deletions packages/google-cloud-oslogin/protos/protos.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,6 @@
"nested": {
"oslogin": {
"nested": {
"common": {
"options": {
"csharp_namespace": "Google.Cloud.OsLogin.Common",
"go_package": "google.golang.org/genproto/googleapis/cloud/oslogin/common;common",
"java_outer_classname": "OsLoginProto",
"java_package": "com.google.cloud.oslogin.common",
"php_namespace": "Google\\Cloud\\OsLogin\\Common"
},
"nested": {
"PosixAccount": {
"fields": {
"primary": {
"type": "bool",
"id": 1
},
"username": {
"type": "string",
"id": 2
},
"uid": {
"type": "int64",
"id": 3
},
"gid": {
"type": "int64",
"id": 4
},
"homeDirectory": {
"type": "string",
"id": 5
},
"shell": {
"type": "string",
"id": 6
},
"gecos": {
"type": "string",
"id": 7
},
"systemId": {
"type": "string",
"id": 8
},
"accountId": {
"type": "string",
"id": 9
}
}
},
"SshPublicKey": {
"fields": {
"key": {
"type": "string",
"id": 1
},
"expirationTimeUsec": {
"type": "int64",
"id": 2
},
"fingerprint": {
"type": "string",
"id": 3
}
}
}
}
},
"v1beta": {
"options": {
"csharp_namespace": "Google.Cloud.OsLogin.V1Beta",
Expand Down Expand Up @@ -226,6 +159,73 @@
}
}
}
},
"common": {
"options": {
"csharp_namespace": "Google.Cloud.OsLogin.Common",
"go_package": "google.golang.org/genproto/googleapis/cloud/oslogin/common;common",
"java_outer_classname": "OsLoginProto",
"java_package": "com.google.cloud.oslogin.common",
"php_namespace": "Google\\Cloud\\OsLogin\\Common"
},
"nested": {
"PosixAccount": {
"fields": {
"primary": {
"type": "bool",
"id": 1
},
"username": {
"type": "string",
"id": 2
},
"uid": {
"type": "int64",
"id": 3
},
"gid": {
"type": "int64",
"id": 4
},
"homeDirectory": {
"type": "string",
"id": 5
},
"shell": {
"type": "string",
"id": 6
},
"gecos": {
"type": "string",
"id": 7
},
"systemId": {
"type": "string",
"id": 8
},
"accountId": {
"type": "string",
"id": 9
}
}
},
"SshPublicKey": {
"fields": {
"key": {
"type": "string",
"id": 1
},
"expirationTimeUsec": {
"type": "int64",
"id": 2
},
"fingerprint": {
"type": "string",
"id": 3
}
}
}
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions packages/google-cloud-oslogin/src/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// Set a flag that we are running in a browser bundle.
global.isBrowser = true;

// Re-export all exports from ./index.js.
module.exports = require('./index');
1 change: 0 additions & 1 deletion packages/google-cloud-oslogin/src/service_proto_list.json

This file was deleted.

75 changes: 50 additions & 25 deletions packages/google-cloud-oslogin/src/v1beta/os_login_service_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ class OsLoginServiceClient {
opts = opts || {};
this._descriptors = {};

if (global.isBrowser) {
// If we're in browser, we use gRPC fallback.
opts.fallback = true;
}

// If we are in browser, we are already using fallback because of the
// "browser" field in package.json.
// But if we were explicitly requested to use fallback, let's do it now.
const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax;

const servicePath =
opts.servicePath || opts.apiEndpoint || this.constructor.servicePath;

Expand All @@ -77,39 +87,54 @@ class OsLoginServiceClient {
// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = this.constructor.scopes;
const gaxGrpc = new gax.GrpcClient(opts);
const gaxGrpc = new gaxModule.GrpcClient(opts);

// Save the auth object to the client, for use by other methods.
this.auth = gaxGrpc.auth;

// Determine the client header string.
const clientHeader = [
`gl-node/${process.versions.node}`,
`grpc/${gaxGrpc.grpcVersion}`,
`gax/${gax.version}`,
`gapic/${VERSION}`,
];
const clientHeader = [];

if (typeof process !== 'undefined' && 'versions' in process) {
clientHeader.push(`gl-node/${process.versions.node}`);
}
clientHeader.push(`gax/${gaxModule.version}`);
if (opts.fallback) {
clientHeader.push(`gl-web/${gaxModule.version}`);
} else {
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
}
clientHeader.push(`gapic/${VERSION}`);
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
}

// Load the applicable protos.
// For Node.js, pass the path to JSON proto file.
// For browsers, pass the JSON content.

const nodejsProtoPath = path.join(
__dirname,
'..',
'..',
'protos',
'protos.json'
);
const protos = gaxGrpc.loadProto(
path.join(__dirname, '..', '..', 'protos'),
['google/cloud/oslogin/v1beta/oslogin.proto']
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
);

// This API contains "path templates"; forward-slash-separated
// identifiers to uniquely identify resources within the API.
// Create useful helper objects for these.
this._pathTemplates = {
fingerprintPathTemplate: new gax.PathTemplate(
fingerprintPathTemplate: new gaxModule.PathTemplate(
'users/{user}/sshPublicKeys/{fingerprint}'
),
projectPathTemplate: new gax.PathTemplate(
projectPathTemplate: new gaxModule.PathTemplate(
'users/{user}/projects/{project}'
),
userPathTemplate: new gax.PathTemplate('users/{user}'),
userPathTemplate: new gaxModule.PathTemplate('users/{user}'),
};

// Put together the default options sent with requests.
Expand All @@ -128,7 +153,9 @@ class OsLoginServiceClient {
// Put together the "service stub" for
// google.cloud.oslogin.v1beta.OsLoginService.
const osLoginServiceStub = gaxGrpc.createStub(
protos.google.cloud.oslogin.v1beta.OsLoginService,
opts.fallback
? protos.lookupService('google.cloud.oslogin.v1beta.OsLoginService')
: protos.google.cloud.oslogin.v1beta.OsLoginService,
opts
);

Expand All @@ -143,18 +170,16 @@ class OsLoginServiceClient {
'updateSshPublicKey',
];
for (const methodName of osLoginServiceStubMethods) {
this._innerApiCalls[methodName] = gax.createApiCall(
osLoginServiceStub.then(
stub =>
function() {
const args = Array.prototype.slice.call(arguments, 0);
return stub[methodName].apply(stub, args);
},
err =>
function() {
throw err;
}
),
const innerCallPromise = osLoginServiceStub.then(
stub => (...args) => {
return stub[methodName].apply(stub, args);
},
err => () => {
throw err;
}
);
this._innerApiCalls[methodName] = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
null
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"../../protos/google/cloud/oslogin/v1beta/oslogin.proto"
]
10 changes: 5 additions & 5 deletions packages/google-cloud-oslogin/synth.metadata
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"updateTime": "2019-08-28T11:17:40.429490Z",
"updateTime": "2019-08-31T11:14:55.697953Z",
"sources": [
{
"generator": {
"name": "artman",
"version": "0.35.1",
"dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384"
"version": "0.36.1",
"dockerImage": "googleapis/artman@sha256:7c20f006c7a62d9d782e2665647d52290c37a952ef3cd134624d5dd62b3f71bd"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "dbd38035c35083507e2f0b839985cf17e212cb1c",
"internalRef": "265796259"
"sha": "82809578652607c8ee29d9e199c21f28f81a03e0",
"internalRef": "266247326"
}
},
{
Expand Down
7 changes: 7 additions & 0 deletions packages/google-cloud-oslogin/test/gapic-v1beta.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe('OsLoginServiceClient', () => {
assert(client);
});

it('should create a client with gRPC fallback', () => {
const client = new osLoginModule.v1beta.OsLoginServiceClient({
fallback: true,
});
assert(client);
});

describe('deletePosixAccount', () => {
it('invokes deletePosixAccount without error', done => {
const client = new osLoginModule.v1beta.OsLoginServiceClient({
Expand Down
46 changes: 46 additions & 0 deletions packages/google-cloud-oslogin/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module.exports = {
entry: './src/browser.js',
output: {
library: 'os-login',
filename: './os-login.js',
},
node: {
child_process: 'empty',
fs: 'empty',
crypto: 'empty',
},
resolve: {
extensions: ['.js', '.json'],
},
module: {
rules: [
{
test: /node_modules[\\/]retry-request[\\/]/,
use: 'null-loader',
},
{
test: /node_modules[\\/]https-proxy-agent[\\/]/,
use: 'null-loader',
},
{
test: /node_modules[\\/]gtoken[\\/]/,
use: 'null-loader',
},
],
},
mode: 'production',
};

0 comments on commit 43cc80e

Please sign in to comment.