Skip to content

Commit 59a78d4

Browse files
feat: clean proto list, add decoded function for longrunning method (#327)
1 parent 099090a commit 59a78d4

File tree

10 files changed

+267
-96
lines changed

10 files changed

+267
-96
lines changed

packages/google-cloud-asset/src/v1/asset_service_client.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as path from 'path';
2828

2929
import * as protos from '../../protos/protos';
3030
import * as gapicConfig from './asset_service_client_config.json';
31-
31+
import {operationsProtos} from 'google-gax';
3232
const version = require('../../../package.json').version;
3333

3434
/**
@@ -997,6 +997,42 @@ export class AssetServiceClient {
997997
this.initialize();
998998
return this.innerApiCalls.exportAssets(request, options, callback);
999999
}
1000+
/**
1001+
* Check the status of the long running operation returned by the exportAssets() method.
1002+
* @param {String} name
1003+
* The operation name that will be passed.
1004+
* @returns {Promise} - The promise which resolves to an object.
1005+
* The decoded operation object has result and metadata field to get information from.
1006+
*
1007+
* @example:
1008+
* const decodedOperation = await checkExportAssetsProgress(name);
1009+
* console.log(decodedOperation.result);
1010+
* console.log(decodedOperation.done);
1011+
* console.log(decodedOperation.metadata);
1012+
*
1013+
*/
1014+
async checkExportAssetsProgress(
1015+
name: string
1016+
): Promise<
1017+
LROperation<
1018+
protos.google.cloud.asset.v1.ExportAssetsResponse,
1019+
protos.google.cloud.asset.v1.ExportAssetsRequest
1020+
>
1021+
> {
1022+
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1023+
{name}
1024+
);
1025+
const [operation] = await this.operationsClient.getOperation(request);
1026+
const decodeOperation = new gax.Operation(
1027+
operation,
1028+
this.descriptors.longrunning.exportAssets,
1029+
gax.createDefaultBackoffSettings()
1030+
);
1031+
return decodeOperation as LROperation<
1032+
protos.google.cloud.asset.v1.ExportAssetsResponse,
1033+
protos.google.cloud.asset.v1.ExportAssetsRequest
1034+
>;
1035+
}
10001036
// --------------------
10011037
// -- Path templates --
10021038
// --------------------

packages/google-cloud-asset/src/v1beta1/asset_service_client.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as path from 'path';
2828

2929
import * as protos from '../../protos/protos';
3030
import * as gapicConfig from './asset_service_client_config.json';
31-
31+
import {operationsProtos} from 'google-gax';
3232
const version = require('../../../package.json').version;
3333

3434
/**
@@ -548,6 +548,42 @@ export class AssetServiceClient {
548548
this.initialize();
549549
return this.innerApiCalls.exportAssets(request, options, callback);
550550
}
551+
/**
552+
* Check the status of the long running operation returned by the exportAssets() method.
553+
* @param {String} name
554+
* The operation name that will be passed.
555+
* @returns {Promise} - The promise which resolves to an object.
556+
* The decoded operation object has result and metadata field to get information from.
557+
*
558+
* @example:
559+
* const decodedOperation = await checkExportAssetsProgress(name);
560+
* console.log(decodedOperation.result);
561+
* console.log(decodedOperation.done);
562+
* console.log(decodedOperation.metadata);
563+
*
564+
*/
565+
async checkExportAssetsProgress(
566+
name: string
567+
): Promise<
568+
LROperation<
569+
protos.google.cloud.asset.v1beta1.ExportAssetsResponse,
570+
protos.google.cloud.asset.v1beta1.ExportAssetsRequest
571+
>
572+
> {
573+
const request = new operationsProtos.google.longrunning.GetOperationRequest(
574+
{name}
575+
);
576+
const [operation] = await this.operationsClient.getOperation(request);
577+
const decodeOperation = new gax.Operation(
578+
operation,
579+
this.descriptors.longrunning.exportAssets,
580+
gax.createDefaultBackoffSettings()
581+
);
582+
return decodeOperation as LROperation<
583+
protos.google.cloud.asset.v1beta1.ExportAssetsResponse,
584+
protos.google.cloud.asset.v1beta1.ExportAssetsRequest
585+
>;
586+
}
551587

552588
/**
553589
* Terminate the GRPC channel and close the client.

packages/google-cloud-asset/src/v1p4beta1/asset_service_client.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as path from 'path';
2828

2929
import * as protos from '../../protos/protos';
3030
import * as gapicConfig from './asset_service_client_config.json';
31-
31+
import {operationsProtos} from 'google-gax';
3232
const version = require('../../../package.json').version;
3333

3434
/**
@@ -512,6 +512,42 @@ export class AssetServiceClient {
512512
callback
513513
);
514514
}
515+
/**
516+
* Check the status of the long running operation returned by the exportIamPolicyAnalysis() method.
517+
* @param {String} name
518+
* The operation name that will be passed.
519+
* @returns {Promise} - The promise which resolves to an object.
520+
* The decoded operation object has result and metadata field to get information from.
521+
*
522+
* @example:
523+
* const decodedOperation = await checkExportIamPolicyAnalysisProgress(name);
524+
* console.log(decodedOperation.result);
525+
* console.log(decodedOperation.done);
526+
* console.log(decodedOperation.metadata);
527+
*
528+
*/
529+
async checkExportIamPolicyAnalysisProgress(
530+
name: string
531+
): Promise<
532+
LROperation<
533+
protos.google.cloud.asset.v1p4beta1.ExportIamPolicyAnalysisResponse,
534+
protos.google.cloud.asset.v1p4beta1.ExportIamPolicyAnalysisRequest
535+
>
536+
> {
537+
const request = new operationsProtos.google.longrunning.GetOperationRequest(
538+
{name}
539+
);
540+
const [operation] = await this.operationsClient.getOperation(request);
541+
const decodeOperation = new gax.Operation(
542+
operation,
543+
this.descriptors.longrunning.exportIamPolicyAnalysis,
544+
gax.createDefaultBackoffSettings()
545+
);
546+
return decodeOperation as LROperation<
547+
protos.google.cloud.asset.v1p4beta1.ExportIamPolicyAnalysisResponse,
548+
protos.google.cloud.asset.v1p4beta1.ExportIamPolicyAnalysisRequest
549+
>;
550+
}
515551

516552
/**
517553
* Terminate the GRPC channel and close the client.

packages/google-cloud-asset/synth.metadata

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
"git": {
55
"name": ".",
66
"remote": "[email protected]:googleapis/nodejs-asset.git",
7-
"sha": "8d72abc44abe6fb805190cf49bcb2f692d77c407"
7+
"sha": "321bc3ae0c187f29231037705a52ca4b805659ba"
88
}
99
},
1010
{
1111
"git": {
1212
"name": "googleapis",
1313
"remote": "https://github.com/googleapis/googleapis.git",
14-
"sha": "e0f9d9e1f9de890db765be46f45ca8490723e3eb",
15-
"internalRef": "309824146"
14+
"sha": "1b5a8d2bbb69c5a04db26bd546d2888e609c6bab",
15+
"internalRef": "309845930"
1616
}
1717
},
1818
{
1919
"git": {
2020
"name": "synthtool",
2121
"remote": "https://github.com/googleapis/synthtool.git",
22-
"sha": "a2c1c4f89a5e220e7b39420ebea33623c7c72804"
22+
"sha": "ab883569eb0257bbf16a6d825fd018b3adde3912"
2323
}
2424
}
2525
],

packages/google-cloud-asset/synth.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,4 @@
5252
source_location='build/src', versions=versions, default_version='v1')
5353
s.copy(templates)
5454

55-
# Extra proto dependencies make the *_proto_list.json have some common files
56-
# that conflict with the same common files imported from gax. This is likely
57-
# a generator problem that needs to be fixed when we start handling synth.py
58-
# custom fixes.
59-
proto_lists = [
60-
f'src/{version}/asset_service_proto_list.json' for version in versions]
61-
remove_proto_keywords = ['/google/api',
62-
'/google/protobuf', '/google/rpc', '/google/type']
63-
for file in proto_lists:
64-
with open(file, 'r') as f:
65-
items = json.load(f)
66-
content = [item for item in items if all(
67-
[(x not in item) for x in remove_proto_keywords])]
68-
new_file = json.dumps(content, indent=2) + '\n'
69-
with open(file, 'w') as f:
70-
f.write(new_file)
71-
7255
node.postprocess_gapic_library()

packages/google-cloud-asset/test/gapic_asset_service_v1.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {SinonStub} from 'sinon';
2323
import {describe, it} from 'mocha';
2424
import * as assetserviceModule from '../src';
2525

26-
import {protobuf, LROperation} from 'google-gax';
26+
import {protobuf, LROperation, operationsProtos} from 'google-gax';
2727

2828
function generateSampleMessage<T extends object>(instance: T) {
2929
const filledObject = (instance.constructor as typeof protobuf.Message).toObject(
@@ -268,9 +268,10 @@ describe('v1.AssetServiceClient', () => {
268268
undefined,
269269
expectedError
270270
);
271-
await assert.rejects(async () => {
272-
await client.batchGetAssetsHistory(request);
273-
}, expectedError);
271+
await assert.rejects(
272+
client.batchGetAssetsHistory(request),
273+
expectedError
274+
);
274275
assert(
275276
(client.innerApiCalls.batchGetAssetsHistory as SinonStub)
276277
.getCall(0)
@@ -382,9 +383,7 @@ describe('v1.AssetServiceClient', () => {
382383
undefined,
383384
expectedError
384385
);
385-
await assert.rejects(async () => {
386-
await client.createFeed(request);
387-
}, expectedError);
386+
await assert.rejects(client.createFeed(request), expectedError);
388387
assert(
389388
(client.innerApiCalls.createFeed as SinonStub)
390389
.getCall(0)
@@ -493,9 +492,7 @@ describe('v1.AssetServiceClient', () => {
493492
};
494493
const expectedError = new Error('expected');
495494
client.innerApiCalls.getFeed = stubSimpleCall(undefined, expectedError);
496-
await assert.rejects(async () => {
497-
await client.getFeed(request);
498-
}, expectedError);
495+
await assert.rejects(client.getFeed(request), expectedError);
499496
assert(
500497
(client.innerApiCalls.getFeed as SinonStub)
501498
.getCall(0)
@@ -604,9 +601,7 @@ describe('v1.AssetServiceClient', () => {
604601
};
605602
const expectedError = new Error('expected');
606603
client.innerApiCalls.listFeeds = stubSimpleCall(undefined, expectedError);
607-
await assert.rejects(async () => {
608-
await client.listFeeds(request);
609-
}, expectedError);
604+
await assert.rejects(client.listFeeds(request), expectedError);
610605
assert(
611606
(client.innerApiCalls.listFeeds as SinonStub)
612607
.getCall(0)
@@ -721,9 +716,7 @@ describe('v1.AssetServiceClient', () => {
721716
undefined,
722717
expectedError
723718
);
724-
await assert.rejects(async () => {
725-
await client.updateFeed(request);
726-
}, expectedError);
719+
await assert.rejects(client.updateFeed(request), expectedError);
727720
assert(
728721
(client.innerApiCalls.updateFeed as SinonStub)
729722
.getCall(0)
@@ -835,9 +828,7 @@ describe('v1.AssetServiceClient', () => {
835828
undefined,
836829
expectedError
837830
);
838-
await assert.rejects(async () => {
839-
await client.deleteFeed(request);
840-
}, expectedError);
831+
await assert.rejects(client.deleteFeed(request), expectedError);
841832
assert(
842833
(client.innerApiCalls.deleteFeed as SinonStub)
843834
.getCall(0)
@@ -957,9 +948,7 @@ describe('v1.AssetServiceClient', () => {
957948
undefined,
958949
expectedError
959950
);
960-
await assert.rejects(async () => {
961-
await client.exportAssets(request);
962-
}, expectedError);
951+
await assert.rejects(client.exportAssets(request), expectedError);
963952
assert(
964953
(client.innerApiCalls.exportAssets as SinonStub)
965954
.getCall(0)
@@ -992,15 +981,51 @@ describe('v1.AssetServiceClient', () => {
992981
expectedError
993982
);
994983
const [operation] = await client.exportAssets(request);
995-
await assert.rejects(async () => {
996-
await operation.promise();
997-
}, expectedError);
984+
await assert.rejects(operation.promise(), expectedError);
998985
assert(
999986
(client.innerApiCalls.exportAssets as SinonStub)
1000987
.getCall(0)
1001988
.calledWith(request, expectedOptions, undefined)
1002989
);
1003990
});
991+
992+
it('invokes checkExportAssetsProgress without error', async () => {
993+
const client = new assetserviceModule.v1.AssetServiceClient({
994+
credentials: {client_email: 'bogus', private_key: 'bogus'},
995+
projectId: 'bogus',
996+
});
997+
client.initialize();
998+
const expectedResponse = generateSampleMessage(
999+
new operationsProtos.google.longrunning.Operation()
1000+
);
1001+
expectedResponse.name = 'test';
1002+
expectedResponse.response = {type_url: 'url', value: Buffer.from('')};
1003+
expectedResponse.metadata = {type_url: 'url', value: Buffer.from('')};
1004+
1005+
client.operationsClient.getOperation = stubSimpleCall(expectedResponse);
1006+
const decodedOperation = await client.checkExportAssetsProgress(
1007+
expectedResponse.name
1008+
);
1009+
assert.deepStrictEqual(decodedOperation.name, expectedResponse.name);
1010+
assert(decodedOperation.metadata);
1011+
assert((client.operationsClient.getOperation as SinonStub).getCall(0));
1012+
});
1013+
1014+
it('invokes checkExportAssetsProgress with error', async () => {
1015+
const client = new assetserviceModule.v1.AssetServiceClient({
1016+
credentials: {client_email: 'bogus', private_key: 'bogus'},
1017+
projectId: 'bogus',
1018+
});
1019+
client.initialize();
1020+
const expectedError = new Error('expected');
1021+
1022+
client.operationsClient.getOperation = stubSimpleCall(
1023+
undefined,
1024+
expectedError
1025+
);
1026+
await assert.rejects(client.checkExportAssetsProgress(''), expectedError);
1027+
assert((client.operationsClient.getOperation as SinonStub).getCall(0));
1028+
});
10041029
});
10051030

10061031
describe('Path templates', () => {

0 commit comments

Comments
 (0)