From bbdbceff453f3571403b9d6e750c76f9ab9782b3 Mon Sep 17 00:00:00 2001
From: "gcf-merge-on-green[bot]"
<60162190+gcf-merge-on-green[bot]@users.noreply.github.com>
Date: Fri, 6 Mar 2020 00:22:08 +0000
Subject: [PATCH] feat: deferred client initialization (#152)
This PR includes changes from https://github.com/googleapis/gapic-generator-typescript/pull/317
that will move the asynchronous initialization and authentication from the client constructor
to an `initialize()` method. This method will be automatically called when the first RPC call
is performed.
The client library usage has not changed, there is no need to update any code.
If you want to make sure the client is authenticated _before_ the first RPC call, you can do
```js
await client.initialize();
```
manually before calling any client method.
---
.../src/v1/container_analysis_client.ts | 99 ++++++++++++-------
.../container_analysis_v1_beta1_client.ts | 84 +++++++++++-----
.../src/v1beta1/grafeas_v1_beta1_client.ts | 95 +++++++++++++-----
.../synth.metadata | 8 +-
.../synth.py | 33 +------
.../test/gapic-container_analysis-v1.ts | 28 ++++++
...pic-container_analysis_v1_beta1-v1beta1.ts | 44 +++++++++
7 files changed, 274 insertions(+), 117 deletions(-)
diff --git a/packages/google-devtools-containeranalysis/src/v1/container_analysis_client.ts b/packages/google-devtools-containeranalysis/src/v1/container_analysis_client.ts
index cff1628a115..5322aa4c583 100644
--- a/packages/google-devtools-containeranalysis/src/v1/container_analysis_client.ts
+++ b/packages/google-devtools-containeranalysis/src/v1/container_analysis_client.ts
@@ -54,9 +54,13 @@ export class ContainerAnalysisClient {
private _innerApiCalls: {[name: string]: Function};
private _pathTemplates: {[name: string]: gax.PathTemplate};
private _terminated = false;
+ private _opts: ClientOptions;
+ private _gaxModule: typeof gax | typeof gax.fallback;
+ private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
+ private _protos: {};
+ private _defaults: {[method: string]: gax.CallSettings};
auth: gax.GoogleAuth;
- opts: ClientOptions;
- containerAnalysisStub: Promise<{[name: string]: Function}>;
+ containerAnalysisStub?: Promise<{[name: string]: Function}>;
/**
* Construct an instance of ContainerAnalysisClient.
@@ -80,8 +84,6 @@ export class ContainerAnalysisClient {
* app is running in an environment which supports
* {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
* your project ID will be detected automatically.
- * @param {function} [options.promise] - Custom promise module to use instead
- * of native Promises.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
*/
@@ -111,25 +113,28 @@ export class ContainerAnalysisClient {
// 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 = !isBrowser && opts.fallback ? gax.fallback : gax;
+ this._gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;
// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = (this.constructor as typeof ContainerAnalysisClient).scopes;
- const gaxGrpc = new gaxModule.GrpcClient(opts);
+ this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
+
+ // Save options to use in initialize() method.
+ this._opts = opts;
// Save the auth object to the client, for use by other methods.
- this.auth = gaxGrpc.auth as gax.GoogleAuth;
+ this.auth = this._gaxGrpc.auth as gax.GoogleAuth;
// Determine the client header string.
- const clientHeader = [`gax/${gaxModule.version}`, `gapic/${version}`];
+ const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
if (typeof process !== 'undefined' && 'versions' in process) {
clientHeader.push(`gl-node/${process.versions.node}`);
} else {
- clientHeader.push(`gl-web/${gaxModule.version}`);
+ clientHeader.push(`gl-web/${this._gaxModule.version}`);
}
if (!opts.fallback) {
- clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
+ clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
}
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
@@ -145,7 +150,7 @@ export class ContainerAnalysisClient {
'protos',
'protos.json'
);
- const protos = gaxGrpc.loadProto(
+ this._protos = this._gaxGrpc.loadProto(
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
);
@@ -153,16 +158,16 @@ export class ContainerAnalysisClient {
// identifiers to uniquely identify resources within the API.
// Create useful helper objects for these.
this._pathTemplates = {
- notePathTemplate: new gaxModule.PathTemplate(
+ notePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/notes/{note}'
),
- occurrencePathTemplate: new gaxModule.PathTemplate(
+ occurrencePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/occurrences/{occurrence}'
),
};
// Put together the default options sent with requests.
- const defaults = gaxGrpc.constructSettings(
+ this._defaults = this._gaxGrpc.constructSettings(
'google.devtools.containeranalysis.v1.ContainerAnalysis',
gapicConfig as gax.ClientConfig,
opts.clientConfig || {},
@@ -173,18 +178,36 @@ export class ContainerAnalysisClient {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this._innerApiCalls = {};
+ }
+
+ /**
+ * Initialize the client.
+ * Performs asynchronous operations (such as authentication) and prepares the client.
+ * This function will be called automatically when any class method is called for the
+ * first time, but if you need to initialize it before calling an actual method,
+ * feel free to call initialize() directly.
+ *
+ * You can await on this method if you want to make sure the client is initialized.
+ *
+ * @returns {Promise} A promise that resolves to an authenticated service stub.
+ */
+ initialize() {
+ // If the client stub promise is already initialized, return immediately.
+ if (this.containerAnalysisStub) {
+ return this.containerAnalysisStub;
+ }
// Put together the "service stub" for
// google.devtools.containeranalysis.v1.ContainerAnalysis.
- this.containerAnalysisStub = gaxGrpc.createStub(
- opts.fallback
- ? (protos as protobuf.Root).lookupService(
+ this.containerAnalysisStub = this._gaxGrpc.createStub(
+ this._opts.fallback
+ ? (this._protos as protobuf.Root).lookupService(
'google.devtools.containeranalysis.v1.ContainerAnalysis'
)
: // tslint:disable-next-line no-any
- (protos as any).google.devtools.containeranalysis.v1
+ (this._protos as any).google.devtools.containeranalysis.v1
.ContainerAnalysis,
- opts
+ this._opts
) as Promise<{[method: string]: Function}>;
// Iterate over each of the methods that the service provides
@@ -208,9 +231,9 @@ export class ContainerAnalysisClient {
}
);
- const apiCall = gaxModule.createApiCall(
+ const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
- defaults[methodName],
+ this._defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
@@ -224,7 +247,8 @@ export class ContainerAnalysisClient {
return apiCall(argument, callOptions, callback);
};
}
- this.opts = opts;
+
+ return this.containerAnalysisStub;
}
/**
@@ -351,6 +375,7 @@ export class ContainerAnalysisClient {
] = gax.routingHeader.fromParams({
resource: request.resource || '',
});
+ this.initialize();
return this._innerApiCalls.setIamPolicy(request, options, callback);
}
getIamPolicy(
@@ -427,6 +452,7 @@ export class ContainerAnalysisClient {
] = gax.routingHeader.fromParams({
resource: request.resource || '',
});
+ this.initialize();
return this._innerApiCalls.getIamPolicy(request, options, callback);
}
testIamPermissions(
@@ -502,6 +528,7 @@ export class ContainerAnalysisClient {
] = gax.routingHeader.fromParams({
resource: request.resource || '',
});
+ this.initialize();
return this._innerApiCalls.testIamPermissions(request, options, callback);
}
@@ -544,18 +571,6 @@ export class ContainerAnalysisClient {
matchNoteFromNoteName(noteName: string) {
return this._pathTemplates.notePathTemplate.match(noteName).note;
}
- /**
- * Returns an instance of a @google-cloud/grafeas client, configured to
- * connect to Google Cloud's Container Analysis API. For documentation
- * on this client, see:
- * https://googleapis.dev/nodejs/grafeas/latest/index.html
- *
- * @returns {GrafeasClient} - An instance of a Grafeas client.
- *
- */
- getGrafeasClient() {
- return new GrafeasClient(this.opts);
- }
/**
* Return a fully-qualified occurrence resource name string.
@@ -601,12 +616,26 @@ export class ContainerAnalysisClient {
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise {
+ this.initialize();
if (!this._terminated) {
- return this.containerAnalysisStub.then(stub => {
+ return this.containerAnalysisStub!.then(stub => {
this._terminated = true;
stub.close();
});
}
return Promise.resolve();
}
+
+ /**
+ * Returns an instance of a @google-cloud/grafeas client, configured to
+ * connect to Google Cloud's Container Analysis API. For documentation
+ * on this client, see:
+ * https://googleapis.dev/nodejs/grafeas/latest/index.html
+ *
+ * @returns {GrafeasClient} - An instance of a Grafeas client.
+ *
+ */
+ getGrafeasClient() {
+ return new GrafeasClient(this._opts);
+ }
}
diff --git a/packages/google-devtools-containeranalysis/src/v1beta1/container_analysis_v1_beta1_client.ts b/packages/google-devtools-containeranalysis/src/v1beta1/container_analysis_v1_beta1_client.ts
index 66f693e37fc..4517b9c839b 100644
--- a/packages/google-devtools-containeranalysis/src/v1beta1/container_analysis_v1_beta1_client.ts
+++ b/packages/google-devtools-containeranalysis/src/v1beta1/container_analysis_v1_beta1_client.ts
@@ -56,8 +56,13 @@ export class ContainerAnalysisV1Beta1Client {
private _innerApiCalls: {[name: string]: Function};
private _pathTemplates: {[name: string]: gax.PathTemplate};
private _terminated = false;
+ private _opts: ClientOptions;
+ private _gaxModule: typeof gax | typeof gax.fallback;
+ private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
+ private _protos: {};
+ private _defaults: {[method: string]: gax.CallSettings};
auth: gax.GoogleAuth;
- containerAnalysisV1Beta1Stub: Promise<{[name: string]: Function}>;
+ containerAnalysisV1Beta1Stub?: Promise<{[name: string]: Function}>;
/**
* Construct an instance of ContainerAnalysisV1Beta1Client.
@@ -81,8 +86,6 @@ export class ContainerAnalysisV1Beta1Client {
* app is running in an environment which supports
* {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
* your project ID will be detected automatically.
- * @param {function} [options.promise] - Custom promise module to use instead
- * of native Promises.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
*/
@@ -113,26 +116,29 @@ export class ContainerAnalysisV1Beta1Client {
// 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 = !isBrowser && opts.fallback ? gax.fallback : gax;
+ this._gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;
// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = (this
.constructor as typeof ContainerAnalysisV1Beta1Client).scopes;
- const gaxGrpc = new gaxModule.GrpcClient(opts);
+ this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
+
+ // Save options to use in initialize() method.
+ this._opts = opts;
// Save the auth object to the client, for use by other methods.
- this.auth = gaxGrpc.auth as gax.GoogleAuth;
+ this.auth = this._gaxGrpc.auth as gax.GoogleAuth;
// Determine the client header string.
- const clientHeader = [`gax/${gaxModule.version}`, `gapic/${version}`];
+ const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
if (typeof process !== 'undefined' && 'versions' in process) {
clientHeader.push(`gl-node/${process.versions.node}`);
} else {
- clientHeader.push(`gl-web/${gaxModule.version}`);
+ clientHeader.push(`gl-web/${this._gaxModule.version}`);
}
if (!opts.fallback) {
- clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
+ clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
}
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
@@ -148,7 +154,7 @@ export class ContainerAnalysisV1Beta1Client {
'protos',
'protos.json'
);
- const protos = gaxGrpc.loadProto(
+ this._protos = this._gaxGrpc.loadProto(
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
);
@@ -156,14 +162,16 @@ export class ContainerAnalysisV1Beta1Client {
// identifiers to uniquely identify resources within the API.
// Create useful helper objects for these.
this._pathTemplates = {
- notePathTemplate: new gaxModule.PathTemplate(
+ notePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/notes/{note}'
),
- occurrencePathTemplate: new gaxModule.PathTemplate(
+ occurrencePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/occurrences/{occurrence}'
),
- projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
- scanConfigPathTemplate: new gaxModule.PathTemplate(
+ projectPathTemplate: new this._gaxModule.PathTemplate(
+ 'projects/{project}'
+ ),
+ scanConfigPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/scanConfigs/{scan_config}'
),
};
@@ -172,7 +180,7 @@ export class ContainerAnalysisV1Beta1Client {
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
- listScanConfigs: new gaxModule.PageDescriptor(
+ listScanConfigs: new this._gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'scanConfigs'
@@ -180,7 +188,7 @@ export class ContainerAnalysisV1Beta1Client {
};
// Put together the default options sent with requests.
- const defaults = gaxGrpc.constructSettings(
+ this._defaults = this._gaxGrpc.constructSettings(
'google.devtools.containeranalysis.v1beta1.ContainerAnalysisV1Beta1',
gapicConfig as gax.ClientConfig,
opts.clientConfig || {},
@@ -191,18 +199,36 @@ export class ContainerAnalysisV1Beta1Client {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this._innerApiCalls = {};
+ }
+
+ /**
+ * Initialize the client.
+ * Performs asynchronous operations (such as authentication) and prepares the client.
+ * This function will be called automatically when any class method is called for the
+ * first time, but if you need to initialize it before calling an actual method,
+ * feel free to call initialize() directly.
+ *
+ * You can await on this method if you want to make sure the client is initialized.
+ *
+ * @returns {Promise} A promise that resolves to an authenticated service stub.
+ */
+ initialize() {
+ // If the client stub promise is already initialized, return immediately.
+ if (this.containerAnalysisV1Beta1Stub) {
+ return this.containerAnalysisV1Beta1Stub;
+ }
// Put together the "service stub" for
// google.devtools.containeranalysis.v1beta1.ContainerAnalysisV1Beta1.
- this.containerAnalysisV1Beta1Stub = gaxGrpc.createStub(
- opts.fallback
- ? (protos as protobuf.Root).lookupService(
+ this.containerAnalysisV1Beta1Stub = this._gaxGrpc.createStub(
+ this._opts.fallback
+ ? (this._protos as protobuf.Root).lookupService(
'google.devtools.containeranalysis.v1beta1.ContainerAnalysisV1Beta1'
)
: // tslint:disable-next-line no-any
- (protos as any).google.devtools.containeranalysis.v1beta1
+ (this._protos as any).google.devtools.containeranalysis.v1beta1
.ContainerAnalysisV1Beta1,
- opts
+ this._opts
) as Promise<{[method: string]: Function}>;
// Iterate over each of the methods that the service provides
@@ -229,9 +255,9 @@ export class ContainerAnalysisV1Beta1Client {
}
);
- const apiCall = gaxModule.createApiCall(
+ const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
- defaults[methodName],
+ this._defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
@@ -245,6 +271,8 @@ export class ContainerAnalysisV1Beta1Client {
return apiCall(argument, callOptions, callback);
};
}
+
+ return this.containerAnalysisV1Beta1Stub;
}
/**
@@ -371,6 +399,7 @@ export class ContainerAnalysisV1Beta1Client {
] = gax.routingHeader.fromParams({
resource: request.resource || '',
});
+ this.initialize();
return this._innerApiCalls.setIamPolicy(request, options, callback);
}
getIamPolicy(
@@ -447,6 +476,7 @@ export class ContainerAnalysisV1Beta1Client {
] = gax.routingHeader.fromParams({
resource: request.resource || '',
});
+ this.initialize();
return this._innerApiCalls.getIamPolicy(request, options, callback);
}
testIamPermissions(
@@ -522,6 +552,7 @@ export class ContainerAnalysisV1Beta1Client {
] = gax.routingHeader.fromParams({
resource: request.resource || '',
});
+ this.initialize();
return this._innerApiCalls.testIamPermissions(request, options, callback);
}
getScanConfig(
@@ -603,6 +634,7 @@ export class ContainerAnalysisV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.getScanConfig(request, options, callback);
}
updateScanConfig(
@@ -686,6 +718,7 @@ export class ContainerAnalysisV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.updateScanConfig(request, options, callback);
}
@@ -777,6 +810,7 @@ export class ContainerAnalysisV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.listScanConfigs(request, options, callback);
}
@@ -823,6 +857,7 @@ export class ContainerAnalysisV1Beta1Client {
parent: request.parent || '',
});
const callSettings = new gax.CallSettings(options);
+ this.initialize();
return this._descriptors.page.listScanConfigs.createStream(
this._innerApiCalls.listScanConfigs as gax.GaxCall,
request,
@@ -974,8 +1009,9 @@ export class ContainerAnalysisV1Beta1Client {
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise {
+ this.initialize();
if (!this._terminated) {
- return this.containerAnalysisV1Beta1Stub.then(stub => {
+ return this.containerAnalysisV1Beta1Stub!.then(stub => {
this._terminated = true;
stub.close();
});
diff --git a/packages/google-devtools-containeranalysis/src/v1beta1/grafeas_v1_beta1_client.ts b/packages/google-devtools-containeranalysis/src/v1beta1/grafeas_v1_beta1_client.ts
index bb5b03b1c5d..3158659c08f 100644
--- a/packages/google-devtools-containeranalysis/src/v1beta1/grafeas_v1_beta1_client.ts
+++ b/packages/google-devtools-containeranalysis/src/v1beta1/grafeas_v1_beta1_client.ts
@@ -57,8 +57,13 @@ export class GrafeasV1Beta1Client {
private _innerApiCalls: {[name: string]: Function};
private _pathTemplates: {[name: string]: gax.PathTemplate};
private _terminated = false;
+ private _opts: ClientOptions;
+ private _gaxModule: typeof gax | typeof gax.fallback;
+ private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
+ private _protos: {};
+ private _defaults: {[method: string]: gax.CallSettings};
auth: gax.GoogleAuth;
- grafeasV1Beta1Stub: Promise<{[name: string]: Function}>;
+ grafeasV1Beta1Stub?: Promise<{[name: string]: Function}>;
/**
* Construct an instance of GrafeasV1Beta1Client.
@@ -82,8 +87,6 @@ export class GrafeasV1Beta1Client {
* app is running in an environment which supports
* {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
* your project ID will be detected automatically.
- * @param {function} [options.promise] - Custom promise module to use instead
- * of native Promises.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
*/
@@ -113,25 +116,28 @@ export class GrafeasV1Beta1Client {
// 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 = !isBrowser && opts.fallback ? gax.fallback : gax;
+ this._gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;
// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = (this.constructor as typeof GrafeasV1Beta1Client).scopes;
- const gaxGrpc = new gaxModule.GrpcClient(opts);
+ this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
+
+ // Save options to use in initialize() method.
+ this._opts = opts;
// Save the auth object to the client, for use by other methods.
- this.auth = gaxGrpc.auth as gax.GoogleAuth;
+ this.auth = this._gaxGrpc.auth as gax.GoogleAuth;
// Determine the client header string.
- const clientHeader = [`gax/${gaxModule.version}`, `gapic/${version}`];
+ const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
if (typeof process !== 'undefined' && 'versions' in process) {
clientHeader.push(`gl-node/${process.versions.node}`);
} else {
- clientHeader.push(`gl-web/${gaxModule.version}`);
+ clientHeader.push(`gl-web/${this._gaxModule.version}`);
}
if (!opts.fallback) {
- clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
+ clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
}
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
@@ -147,7 +153,7 @@ export class GrafeasV1Beta1Client {
'protos',
'protos.json'
);
- const protos = gaxGrpc.loadProto(
+ this._protos = this._gaxGrpc.loadProto(
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
);
@@ -155,13 +161,13 @@ export class GrafeasV1Beta1Client {
// identifiers to uniquely identify resources within the API.
// Create useful helper objects for these.
this._pathTemplates = {
- notePathTemplate: new gaxModule.PathTemplate(
+ notePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/notes/{note}'
),
- occurrencePathTemplate: new gaxModule.PathTemplate(
+ occurrencePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/occurrences/{occurrence}'
),
- scanConfigPathTemplate: new gaxModule.PathTemplate(
+ scanConfigPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/scanConfigs/{scan_config}'
),
};
@@ -170,17 +176,17 @@ export class GrafeasV1Beta1Client {
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
- listOccurrences: new gaxModule.PageDescriptor(
+ listOccurrences: new this._gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'occurrences'
),
- listNotes: new gaxModule.PageDescriptor(
+ listNotes: new this._gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'notes'
),
- listNoteOccurrences: new gaxModule.PageDescriptor(
+ listNoteOccurrences: new this._gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'occurrences'
@@ -188,7 +194,7 @@ export class GrafeasV1Beta1Client {
};
// Put together the default options sent with requests.
- const defaults = gaxGrpc.constructSettings(
+ this._defaults = this._gaxGrpc.constructSettings(
'google.devtools.containeranalysis.v1beta1.GrafeasV1Beta1',
gapicConfig as gax.ClientConfig,
opts.clientConfig || {},
@@ -199,18 +205,36 @@ export class GrafeasV1Beta1Client {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this._innerApiCalls = {};
+ }
+
+ /**
+ * Initialize the client.
+ * Performs asynchronous operations (such as authentication) and prepares the client.
+ * This function will be called automatically when any class method is called for the
+ * first time, but if you need to initialize it before calling an actual method,
+ * feel free to call initialize() directly.
+ *
+ * You can await on this method if you want to make sure the client is initialized.
+ *
+ * @returns {Promise} A promise that resolves to an authenticated service stub.
+ */
+ initialize() {
+ // If the client stub promise is already initialized, return immediately.
+ if (this.grafeasV1Beta1Stub) {
+ return this.grafeasV1Beta1Stub;
+ }
// Put together the "service stub" for
// google.devtools.containeranalysis.v1beta1.GrafeasV1Beta1.
- this.grafeasV1Beta1Stub = gaxGrpc.createStub(
- opts.fallback
- ? (protos as protobuf.Root).lookupService(
+ this.grafeasV1Beta1Stub = this._gaxGrpc.createStub(
+ this._opts.fallback
+ ? (this._protos as protobuf.Root).lookupService(
'google.devtools.containeranalysis.v1beta1.GrafeasV1Beta1'
)
: // tslint:disable-next-line no-any
- (protos as any).google.devtools.containeranalysis.v1beta1
+ (this._protos as any).google.devtools.containeranalysis.v1beta1
.GrafeasV1Beta1,
- opts
+ this._opts
) as Promise<{[method: string]: Function}>;
// Iterate over each of the methods that the service provides
@@ -246,9 +270,9 @@ export class GrafeasV1Beta1Client {
}
);
- const apiCall = gaxModule.createApiCall(
+ const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
- defaults[methodName],
+ this._defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
@@ -262,6 +286,8 @@ export class GrafeasV1Beta1Client {
return apiCall(argument, callOptions, callback);
};
}
+
+ return this.grafeasV1Beta1Stub;
}
/**
@@ -384,6 +410,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.getOccurrence(request, options, callback);
}
deleteOccurrence(
@@ -458,6 +485,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.deleteOccurrence(request, options, callback);
}
createOccurrence(
@@ -532,6 +560,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.createOccurrence(request, options, callback);
}
batchCreateOccurrences(
@@ -607,6 +636,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.batchCreateOccurrences(
request,
options,
@@ -687,6 +717,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.updateOccurrence(request, options, callback);
}
getOccurrenceNote(
@@ -760,6 +791,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.getOccurrenceNote(request, options, callback);
}
getNote(
@@ -832,6 +864,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.getNote(request, options, callback);
}
deleteNote(
@@ -904,6 +937,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.deleteNote(request, options, callback);
}
createNote(
@@ -980,6 +1014,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.createNote(request, options, callback);
}
batchCreateNotes(
@@ -1054,6 +1089,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.batchCreateNotes(request, options, callback);
}
updateNote(
@@ -1130,6 +1166,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.updateNote(request, options, callback);
}
getVulnerabilityOccurrencesSummary(
@@ -1213,6 +1250,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.getVulnerabilityOccurrencesSummary(
request,
options,
@@ -1308,6 +1346,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.listOccurrences(request, options, callback);
}
@@ -1354,6 +1393,7 @@ export class GrafeasV1Beta1Client {
parent: request.parent || '',
});
const callSettings = new gax.CallSettings(options);
+ this.initialize();
return this._descriptors.page.listOccurrences.createStream(
this._innerApiCalls.listOccurrences as gax.GaxCall,
request,
@@ -1448,6 +1488,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
+ this.initialize();
return this._innerApiCalls.listNotes(request, options, callback);
}
@@ -1494,6 +1535,7 @@ export class GrafeasV1Beta1Client {
parent: request.parent || '',
});
const callSettings = new gax.CallSettings(options);
+ this.initialize();
return this._descriptors.page.listNotes.createStream(
this._innerApiCalls.listNotes as gax.GaxCall,
request,
@@ -1590,6 +1632,7 @@ export class GrafeasV1Beta1Client {
] = gax.routingHeader.fromParams({
name: request.name || '',
});
+ this.initialize();
return this._innerApiCalls.listNoteOccurrences(request, options, callback);
}
@@ -1636,6 +1679,7 @@ export class GrafeasV1Beta1Client {
name: request.name || '',
});
const callSettings = new gax.CallSettings(options);
+ this.initialize();
return this._descriptors.page.listNoteOccurrences.createStream(
this._innerApiCalls.listNoteOccurrences as gax.GaxCall,
request,
@@ -1764,8 +1808,9 @@ export class GrafeasV1Beta1Client {
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise {
+ this.initialize();
if (!this._terminated) {
- return this.grafeasV1Beta1Stub.then(stub => {
+ return this.grafeasV1Beta1Stub!.then(stub => {
this._terminated = true;
stub.close();
});
diff --git a/packages/google-devtools-containeranalysis/synth.metadata b/packages/google-devtools-containeranalysis/synth.metadata
index e1d36c08b8e..4a24f7e78c7 100644
--- a/packages/google-devtools-containeranalysis/synth.metadata
+++ b/packages/google-devtools-containeranalysis/synth.metadata
@@ -1,19 +1,19 @@
{
- "updateTime": "2020-02-07T19:14:58.575078Z",
+ "updateTime": "2020-03-05T23:59:18.093854Z",
"sources": [
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
- "sha": "e46f761cd6ec15a9e3d5ed4ff321a4bcba8e8585",
- "internalRef": "293710856"
+ "sha": "f0b581b5bdf803e45201ecdb3688b60e381628a8",
+ "internalRef": "299181282"
}
},
{
"template": {
"name": "node_library",
"origin": "synthtool.gcp",
- "version": "2019.10.17"
+ "version": "2020.2.4"
}
}
],
diff --git a/packages/google-devtools-containeranalysis/synth.py b/packages/google-devtools-containeranalysis/synth.py
index 3b87c2d813e..cef7454d8c6 100644
--- a/packages/google-devtools-containeranalysis/synth.py
+++ b/packages/google-devtools-containeranalysis/synth.py
@@ -56,34 +56,8 @@
"""import * as path from 'path';
const { GrafeasClient } = require('@google-cloud/grafeas');
""")
-s.replace("src/v1/container_analysis_client.ts",
-"""auth\: gax\.GoogleAuth;
-""",
-"""auth: gax.GoogleAuth;
-opts: ClientOptions;
-""")
-s.replace("src/v1/container_analysis_client.ts",
-""" \}
-
- \/\*\*
- \* The DNS address for this API service\.
- \*\/
-""",
-""" this.opts = opts;
- }
-
- /**
- * The DNS address for this API service.
- */
-""")
-s.replace("src/v1/container_analysis_client.ts",
-r""" matchNoteFromNoteName\(noteName: string\) {
- return this\._pathTemplates\.notePathTemplate\.match\(noteName\)\.note;
- }
-""",
-r""" matchNoteFromNoteName(noteName: string) {
- return this._pathTemplates.notePathTemplate.match(noteName).note;
- }
+s.replace("src/v1/container_analysis_client.ts", "^}",
+r"""
/**
* Returns an instance of a @google-cloud/grafeas client, configured to
* connect to Google Cloud's Container Analysis API. For documentation
@@ -94,8 +68,9 @@
*
*/
getGrafeasClient() {
- return new GrafeasClient(this.opts);
+ return new GrafeasClient(this._opts);
}
+}
""")
# Node.js specific cleanup
to_remove=['src/v1/grafeas_client.ts', 'src/v1/grafeas_client_config.json', 'src/v1/grafeas_proto_list.json', 'src/v1beta1/grafeas_client.ts','src/v1beta1/grafeas_client_config.json', 'src/v1beta1/grafeas_proto_list.json', 'test/gapic-grafeas_v1_beta1-v1beta1.ts', 'test/gapic-grafeas-v1.ts', 'test/gapic-grafeas-v1beta1.ts']
diff --git a/packages/google-devtools-containeranalysis/test/gapic-container_analysis-v1.ts b/packages/google-devtools-containeranalysis/test/gapic-container_analysis-v1.ts
index ebb1a171dda..2ceff01619f 100644
--- a/packages/google-devtools-containeranalysis/test/gapic-container_analysis-v1.ts
+++ b/packages/google-devtools-containeranalysis/test/gapic-container_analysis-v1.ts
@@ -83,12 +83,30 @@ describe('v1.ContainerAnalysisClient', () => {
});
assert(client);
});
+ it('has initialize method and supports deferred initialization', async () => {
+ const client = new containeranalysisModule.v1.ContainerAnalysisClient({
+ credentials: {client_email: 'bogus', private_key: 'bogus'},
+ projectId: 'bogus',
+ });
+ assert.strictEqual(client.containerAnalysisStub, undefined);
+ await client.initialize();
+ assert(client.containerAnalysisStub);
+ });
+ it('has close method', () => {
+ const client = new containeranalysisModule.v1.ContainerAnalysisClient({
+ credentials: {client_email: 'bogus', private_key: 'bogus'},
+ projectId: 'bogus',
+ });
+ client.close();
+ });
describe('setIamPolicy', () => {
it('invokes setIamPolicy without error', done => {
const client = new containeranalysisModule.v1.ContainerAnalysisClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ISetIamPolicyRequest = {};
request.resource = '';
@@ -112,6 +130,8 @@ describe('v1.ContainerAnalysisClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ISetIamPolicyRequest = {};
request.resource = '';
@@ -137,6 +157,8 @@ describe('v1.ContainerAnalysisClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.IGetIamPolicyRequest = {};
request.resource = '';
@@ -160,6 +182,8 @@ describe('v1.ContainerAnalysisClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.IGetIamPolicyRequest = {};
request.resource = '';
@@ -185,6 +209,8 @@ describe('v1.ContainerAnalysisClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ITestIamPermissionsRequest = {};
request.resource = '';
@@ -208,6 +234,8 @@ describe('v1.ContainerAnalysisClient', () => {
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ITestIamPermissionsRequest = {};
request.resource = '';
diff --git a/packages/google-devtools-containeranalysis/test/gapic-container_analysis_v1_beta1-v1beta1.ts b/packages/google-devtools-containeranalysis/test/gapic-container_analysis_v1_beta1-v1beta1.ts
index c534d813fda..71ddb042ec8 100644
--- a/packages/google-devtools-containeranalysis/test/gapic-container_analysis_v1_beta1-v1beta1.ts
+++ b/packages/google-devtools-containeranalysis/test/gapic-container_analysis_v1_beta1-v1beta1.ts
@@ -89,6 +89,26 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
);
assert(client);
});
+ it('has initialize method and supports deferred initialization', async () => {
+ const client = new containeranalysisv1beta1Module.v1beta1.ContainerAnalysisV1Beta1Client(
+ {
+ credentials: {client_email: 'bogus', private_key: 'bogus'},
+ projectId: 'bogus',
+ }
+ );
+ assert.strictEqual(client.containerAnalysisV1Beta1Stub, undefined);
+ await client.initialize();
+ assert(client.containerAnalysisV1Beta1Stub);
+ });
+ it('has close method', () => {
+ const client = new containeranalysisv1beta1Module.v1beta1.ContainerAnalysisV1Beta1Client(
+ {
+ credentials: {client_email: 'bogus', private_key: 'bogus'},
+ projectId: 'bogus',
+ }
+ );
+ client.close();
+ });
describe('setIamPolicy', () => {
it('invokes setIamPolicy without error', done => {
const client = new containeranalysisv1beta1Module.v1beta1.ContainerAnalysisV1Beta1Client(
@@ -97,6 +117,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ISetIamPolicyRequest = {};
request.resource = '';
@@ -122,6 +144,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ISetIamPolicyRequest = {};
request.resource = '';
@@ -149,6 +173,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.IGetIamPolicyRequest = {};
request.resource = '';
@@ -174,6 +200,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.IGetIamPolicyRequest = {};
request.resource = '';
@@ -201,6 +229,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ITestIamPermissionsRequest = {};
request.resource = '';
@@ -226,6 +256,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.iam.v1.ITestIamPermissionsRequest = {};
request.resource = '';
@@ -253,6 +285,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.devtools.containeranalysis.v1beta1.IGetScanConfigRequest = {};
request.name = '';
@@ -278,6 +312,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.devtools.containeranalysis.v1beta1.IGetScanConfigRequest = {};
request.name = '';
@@ -305,6 +341,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.devtools.containeranalysis.v1beta1.IUpdateScanConfigRequest = {};
request.name = '';
@@ -330,6 +368,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.devtools.containeranalysis.v1beta1.IUpdateScanConfigRequest = {};
request.name = '';
@@ -357,6 +397,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.devtools.containeranalysis.v1beta1.IListScanConfigsRequest = {};
request.parent = '';
@@ -386,6 +428,8 @@ describe('v1beta1.ContainerAnalysisV1Beta1Client', () => {
projectId: 'bogus',
}
);
+ // Initialize client before mocking
+ client.initialize();
// Mock request
const request: protosTypes.google.devtools.containeranalysis.v1beta1.IListScanConfigsRequest = {};
request.parent = '';