Skip to content

Commit fc9bd69

Browse files
committed
fix: add logic to check for IAM RPC methods within the service before adding mixin to avoid generating duplicate methods
1 parent 14bb528 commit fc9bd69

8 files changed

Lines changed: 118 additions & 28 deletions

File tree

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/_iam.njk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
IamClient is created for the client in the constructor.
66
[setIamPolicy, getIamPolicy, testIamPermission] methods are created which is calling the corresponding methods from IamClient in google-gax.
77
-#}
8+
{{ getIamPolicy() }}
9+
{{ setIamPolicy() }}
10+
{{ testIamPermissions() }}
11+
{%- endmacro -%}
12+
13+
{%- macro getIamPolicy() -%}
814
/**
915
* Gets the access control policy for a resource. Returns an empty policy
1016
* if the resource exists and does not have a policy set.
@@ -47,7 +53,9 @@
4753
):Promise<[IamProtos.google.iam.v1.Policy]> {
4854
return this.iamClient.getIamPolicy(request, options, callback);
4955
}
56+
{%- endmacro -%}
5057

58+
{%- macro setIamPolicy() -%}
5159
/**
5260
* Returns permissions that a caller has on the specified resource. If the
5361
* resource does not exist, this will return an empty set of
@@ -94,7 +102,9 @@
94102
):Promise<[IamProtos.google.iam.v1.Policy]> {
95103
return this.iamClient.setIamPolicy(request, options, callback);
96104
}
105+
{%- endmacro -%}
97106

107+
{%- macro testIamPermissions() -%}
98108
/**
99109
* Returns permissions that a caller has on the specified resource. If the
100110
* resource does not exist, this will return an empty set of

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/_locations.njk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
[getLocation, listLocations] methods are created which is calling the corresponding methods from LocationsClient in google-gax. Note that since the listLocations method is a paginated
77
method, we default to using listLocationsAsync for ease of use.
88
-#}
9+
{{ getLocation() }}
10+
{{ listLocationsAsync() }}
11+
{%- endmacro -%}
12+
13+
{%- macro getLocation() -%}
914
/**
1015
* Gets information about a location.
1116
*
@@ -45,7 +50,9 @@
4550
): Promise<LocationProtos.google.cloud.location.ILocation> {
4651
return this.locationsClient.getLocation(request, options, callback);
4752
}
53+
{%- endmacro -%}
4854

55+
{%- macro listLocationsAsync() -%}
4956
/**
5057
* Lists information about the supported locations for this service. Returns an iterable object.
5158
*

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/src/$version/$service_client.ts.njk

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ limitations under the License.
2727
{% import "../../_namer.njk" as namer -%}
2828
{{- namer.initialize(id, service) -}}
2929
{%- set autoPopulated = false -%}
30+
{%- set methodNamesStr = "|" -%}
3031
{%- for method in service.method -%}
3132
{%- if method.autoPopulatedFields|length !== 0 %}
3233
{%- set autoPopulated = true -%}
3334
{%- endif %}
35+
{%- set methodNamesStr = methodNamesStr + method.name.toCamelCase() + "|" -%}
3436
{%- endfor -%}
37+
{%- set generateIam = service.IAMPolicyMixin > 0 and ('|getIamPolicy|' not in methodNamesStr or '|setIamPolicy|' not in methodNamesStr or '|testIamPermissions|' not in methodNamesStr) -%}
38+
{%- set generateLocation = service.LocationMixin > 0 and ('|getLocation|' not in methodNamesStr or '|listLocations|' not in methodNamesStr) -%}
3539
import {% if (not api.legacyProtoLoad) and (autoPopulated === false) %}type {% endif %}* as gax from 'google-gax';
3640
import type {Callback, CallOptions, Descriptors, ClientOptions
3741
{%- if service.longRunning.length > 0 or service.LongRunningOperationsMixin > 0 %}, GrpcClientOptions{%- endif -%}
3842
{%- if service.longRunning.length > 0 or service.diregapicLRO.length > 0 %}, LROperation{%- endif -%}
3943
{%- if service.paging.length > 0 %}, PaginationCallback, GaxCall{%- endif -%}
40-
{%- if service.IAMPolicyMixin > 0 %}, IamClient, IamProtos{%- endif -%}
41-
{%- if service.LocationMixin > 0 %}, LocationsClient, LocationProtos{%- endif -%}
44+
{%- if generateIam %}, IamClient, IamProtos{%- endif -%}
45+
{%- if generateLocation %}, LocationsClient, LocationProtos{%- endif -%}
4246
} from 'google-gax';
4347
{% if service.paging.length > 0 or service.streaming.length > 0 -%}
4448
{% set importStreamJoiner = joiner(', ') -%}
@@ -93,10 +97,10 @@ export class {{ service.name }}Client {
9397
};
9498
warn: (code: string, message: string, warnType?: string) => void;
9599
innerApiCalls: {[name: string]: Function};
96-
{%- if service.IAMPolicyMixin > 0 %}
100+
{%- if generateIam %}
97101
iamClient: IamClient;
98102
{%- endif %}
99-
{%- if service.LocationMixin > 0 %}
103+
{%- if generateLocation %}
100104
locationsClient: LocationsClient;
101105
{%- endif -%}
102106
{%- if service.pathTemplates.length > 0 %}
@@ -222,11 +226,11 @@ export class {{ service.name }}Client {
222226
if (servicePath === this._servicePath) {
223227
this.auth.defaultScopes = staticMembers.{{ id.get("scopes") }};
224228
}
225-
{%- if service.IAMPolicyMixin > 0 %}
229+
{%- if generateIam %}
226230
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);
227231
{% endif %}
228232

229-
{%- if service.LocationMixin > 0 %}
233+
{%- if generateLocation %}
230234
this.locationsClient = new this._gaxModule.LocationsClient(
231235
this._gaxGrpc,
232236
opts
@@ -1102,11 +1106,24 @@ export class {{ service.name }}Client {
11021106
}
11031107
{%- endif %}
11041108
{%- endfor %}
1105-
{%- if service.IAMPolicyMixin > 0 %}
1106-
{{ iam.iamServiceMethods()}}
1109+
{%- if generateIam %}
1110+
{%- if '|getIamPolicy|' not in methodNamesStr %}
1111+
{{ iam.getIamPolicy()}}
1112+
{%- endif %}
1113+
{%- if '|setIamPolicy|' not in methodNamesStr %}
1114+
{{ iam.setIamPolicy()}}
1115+
{%- endif %}
1116+
{%- if '|testIamPermissions|' not in methodNamesStr %}
1117+
{{ iam.testIamPermissions()}}
1118+
{%- endif %}
11071119
{% endif -%}
1108-
{%- if service.LocationMixin > 0 %}
1109-
{{ location.locationServiceMethods()}}
1120+
{%- if generateLocation %}
1121+
{%- if '|getLocation|' not in methodNamesStr %}
1122+
{{ location.getLocation() }}
1123+
{%- endif %}
1124+
{%- if '|listLocations|' not in methodNamesStr %}
1125+
{{ location.listLocationsAsync() }}
1126+
{%- endif %}
11101127
{% endif -%}
11111128
{%- if service.LongRunningOperationsMixin > 0 %}
11121129
{{ operations.operationsServiceMethods()}}
@@ -1165,10 +1182,10 @@ export class {{ service.name }}Client {
11651182
this._log.info('ending gRPC channel');
11661183
this._terminated = true;
11671184
stub.close();
1168-
{%- if service.IAMPolicyMixin > 0 %}
1185+
{%- if generateIam %}
11691186
this.iamClient.close().catch(err => {throw err});
11701187
{%- endif %}
1171-
{%- if service.LocationMixin > 0 %}
1188+
{%- if generateLocation %}
11721189
this.locationsClient.close().catch(err => {throw err});
11731190
{%- endif %}
11741191
{%- if service.longRunning.length > 0 or service.LongRunningOperationsMixin > 0 %}

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/test/gapic_$service_$version.ts.njk

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ limitations under the License.
2020
{{license.license(commonParameters.copyrightYear)}}
2121
{% import "../_namer.njk" as namer -%}
2222
{{- namer.initialize(id, service) -}}
23+
{%- set methodNamesStr = "|" -%}
24+
{%- for method in service.method -%}
25+
{%- set methodNamesStr = methodNamesStr + method.name.toCamelCase() + "|" -%}
26+
{%- endfor -%}
2327
import * as protos from '../protos/protos';
2428
import * as assert from 'assert';
2529
import * as sinon from 'sinon';
@@ -1089,6 +1093,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
10891093
{%- if (service.IAMPolicyMixin) > 0 %}
10901094
{%- set IAMmethods = ['getIamPolicy', 'setIamPolicy', 'testIamPermissions'] %}
10911095
{%- for method in IAMmethods %}
1096+
{%- if ('|' + method + '|') not in methodNamesStr %}
10921097
describe('{{ method }}', () => {
10931098
it('invokes {{ method }} without error', async () => {
10941099
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1204,10 +1209,12 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
12041209
.getCall(0).calledWith(request, expectedOptions, undefined));
12051210
});
12061211
});
1212+
{%- endif %}
12071213
{%- endfor %}
12081214
{%- endif %}
12091215
{%- if service.LocationMixin > 0 %}
1210-
{%- set method = 'getLocation' %}
1216+
{%- if '|getLocation|' not in methodNamesStr %}
1217+
{%- set method = 'getLocation' %}
12111218
describe('{{ method }}', () => {
12121219
it('invokes {{ method }} without error', async () => {
12131220
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1314,7 +1321,9 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
13141321
.getCall(0).calledWith(request, expectedOptions, undefined));
13151322
});
13161323
});
1317-
{%- set method = 'listLocations' %}
1324+
{%- endif %}
1325+
{%- if '|listLocations|' not in methodNamesStr %}
1326+
{%- set method = 'listLocations' %}
13181327
describe('{{ method }}Async', () => {
13191328
it('uses async iteration with {{method}} without error', async () => {
13201329
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({{- util.initClientOptions(api.rest) -}});
@@ -1395,6 +1404,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
13951404
);
13961405
});
13971406
});
1407+
{%- endif %}
13981408
{%- endif %}
13991409
{%- if (service.LongRunningOperationsMixin) > 0 %}
14001410
{%- set Operationsmethods = ['getOperation', 'cancelOperation', 'deleteOperation'] %}

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/_iam.njk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
IamClient is created for the client in the constructor.
66
[setIamPolicy, getIamPolicy, testIamPermission] methods are created which is calling the corresponding methods from IamClient in google-gax.
77
-#}
8+
{{ getIamPolicy() }}
9+
{{ setIamPolicy() }}
10+
{{ testIamPermissions() }}
11+
{%- endmacro -%}
12+
13+
{%- macro getIamPolicy() -%}
814
/**
915
* Gets the access control policy for a resource. Returns an empty policy
1016
* if the resource exists and does not have a policy set.
@@ -47,7 +53,9 @@
4753
):Promise<[IamProtos.google.iam.v1.Policy]> {
4854
return this.iamClient.getIamPolicy(request, options, callback);
4955
}
56+
{%- endmacro -%}
5057

58+
{%- macro setIamPolicy() -%}
5159
/**
5260
* Returns permissions that a caller has on the specified resource. If the
5361
* resource does not exist, this will return an empty set of
@@ -94,7 +102,9 @@
94102
):Promise<[IamProtos.google.iam.v1.Policy]> {
95103
return this.iamClient.setIamPolicy(request, options, callback);
96104
}
105+
{%- endmacro -%}
97106

107+
{%- macro testIamPermissions() -%}
98108
/**
99109
* Returns permissions that a caller has on the specified resource. If the
100110
* resource does not exist, this will return an empty set of

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/_locations.njk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
[getLocation, listLocations] methods are created which is calling the corresponding methods from LocationsClient in google-gax. Note that since the listLocations method is a paginated
77
method, we default to using listLocationsAsync for ease of use.
88
-#}
9+
{{ getLocation() }}
10+
{{ listLocationsAsync() }}
11+
{%- endmacro -%}
12+
13+
{%- macro getLocation() -%}
914
/**
1015
* Gets information about a location.
1116
*
@@ -45,7 +50,9 @@
4550
): Promise<LocationProtos.google.cloud.location.ILocation> {
4651
return this.locationsClient.getLocation(request, options, callback);
4752
}
53+
{%- endmacro -%}
4854

55+
{%- macro listLocationsAsync() -%}
4956
/**
5057
* Lists information about the supported locations for this service. Returns an iterable object.
5158
*

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/esm/src/$version/$service_client.ts.njk

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ limitations under the License.
2626
{% import "../../../_operations.njk" as operations -%}
2727
{% import "../../../_namer.njk" as namer -%}
2828
{{- namer.initialize(id, service) -}}
29+
{%- set methodNamesStr = "|" -%}
30+
{%- for method in service.method -%}
31+
{%- set methodNamesStr = methodNamesStr + method.name.toCamelCase() + "|" -%}
32+
{%- endfor -%}
33+
{%- set generateIam = service.IAMPolicyMixin > 0 and ('|getIamPolicy|' not in methodNamesStr or '|setIamPolicy|' not in methodNamesStr or '|testIamPermissions|' not in methodNamesStr) -%}
34+
{%- set generateLocation = service.LocationMixin > 0 and ('|getLocation|' not in methodNamesStr or '|listLocations|' not in methodNamesStr) -%}
2935
import * as gax from 'google-gax';
3036
import type {Callback, CallOptions, Descriptors, ClientOptions
3137
{%- if service.longRunning.length > 0 or service.LongRunningOperationsMixin > 0 %}, GrpcClientOptions{%- endif -%}
3238
{%- if service.longRunning.length > 0 or service.diregapicLRO.length > 0 %}, LROperation{%- endif -%}
3339
{%- if service.paging.length > 0 %}, PaginationCallback, GaxCall{%- endif -%}
34-
{%- if service.IAMPolicyMixin > 0 %}, IamClient, IamProtos{%- endif -%}
35-
{%- if service.LocationMixin > 0 %}, LocationsClient, LocationProtos{%- endif -%}
40+
{%- if generateIam %}, IamClient, IamProtos{%- endif -%}
41+
{%- if generateLocation %}, LocationsClient, LocationProtos{%- endif -%}
3642
} from 'google-gax';
3743
{% if service.paging.length > 0 or service.streaming.length > 0 -%}
3844
{% set importStreamJoiner = joiner(', ') -%}
@@ -99,10 +105,10 @@ export class {{ service.name }}Client {
99105
};
100106
warn: (code: string, message: string, warnType?: string) => void;
101107
innerApiCalls: {[name: string]: Function};
102-
{%- if service.IAMPolicyMixin > 0 %}
108+
{%- if generateIam %}
103109
iamClient: IamClient;
104110
{%- endif %}
105-
{%- if service.LocationMixin > 0 %}
111+
{%- if generateLocation %}
106112
locationsClient: LocationsClient;
107113
{%- endif -%}
108114
{%- if service.pathTemplates.length > 0 %}
@@ -229,11 +235,11 @@ export class {{ service.name }}Client {
229235
if (servicePath === this._servicePath) {
230236
this.auth.defaultScopes = staticMembers.{{ id.get("scopes") }};
231237
}
232-
{%- if service.IAMPolicyMixin > 0 %}
238+
{%- if generateIam %}
233239
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);
234240
{% endif %}
235241

236-
{%- if service.LocationMixin > 0 %}
242+
{%- if generateLocation %}
237243
this.locationsClient = new this._gaxModule.LocationsClient(
238244
this._gaxGrpc,
239245
opts
@@ -1109,11 +1115,24 @@ export class {{ service.name }}Client {
11091115
}
11101116
{%- endif %}
11111117
{%- endfor %}
1112-
{%- if service.IAMPolicyMixin > 0 %}
1113-
{{ iam.iamServiceMethods()}}
1118+
{%- if generateIam %}
1119+
{%- if '|getIamPolicy|' not in methodNamesStr %}
1120+
{{ iam.getIamPolicy()}}
1121+
{%- endif %}
1122+
{%- if '|setIamPolicy|' not in methodNamesStr %}
1123+
{{ iam.setIamPolicy()}}
1124+
{%- endif %}
1125+
{%- if '|testIamPermissions|' not in methodNamesStr %}
1126+
{{ iam.testIamPermissions()}}
1127+
{%- endif %}
11141128
{% endif -%}
1115-
{%- if service.LocationMixin > 0 %}
1116-
{{ location.locationServiceMethods()}}
1129+
{%- if generateLocation %}
1130+
{%- if '|getLocation|' not in methodNamesStr %}
1131+
{{ location.getLocation() }}
1132+
{%- endif %}
1133+
{%- if '|listLocations|' not in methodNamesStr %}
1134+
{{ location.listLocationsAsync() }}
1135+
{%- endif %}
11171136
{% endif -%}
11181137
{%- if service.LongRunningOperationsMixin > 0 %}
11191138
{{ operations.operationsServiceMethods()}}
@@ -1172,10 +1191,10 @@ export class {{ service.name }}Client {
11721191
this._log.info('ending gRPC channel');
11731192
this._terminated = true;
11741193
stub.close();
1175-
{%- if service.IAMPolicyMixin > 0 %}
1194+
{%- if generateIam %}
11761195
this.iamClient.close().catch(err => {throw err});
11771196
{%- endif %}
1178-
{%- if service.LocationMixin > 0 %}
1197+
{%- if generateLocation %}
11791198
this.locationsClient.close().catch(err => {throw err});
11801199
{%- endif %}
11811200
{%- if service.longRunning.length > 0 or service.LongRunningOperationsMixin > 0 %}

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/esm/test/gapic_$service_$version.ts.njk

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ limitations under the License.
2020
{{license.license(commonParameters.copyrightYear)}}
2121
{% import "../../_namer.njk" as namer -%}
2222
{{- namer.initialize(id, service) -}}
23+
{%- set methodNamesStr = "|" -%}
24+
{%- for method in service.method -%}
25+
{%- set methodNamesStr = methodNamesStr + method.name.toCamelCase() + "|" -%}
26+
{%- endfor -%}
2327
// @ts-ignore
2428
import * as protos from '../../protos/protos.js';
2529
import assert from 'assert';
@@ -1095,6 +1099,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
10951099
{%- if (service.IAMPolicyMixin) > 0 %}
10961100
{%- set IAMmethods = ['getIamPolicy', 'setIamPolicy', 'testIamPermissions'] %}
10971101
{%- for method in IAMmethods %}
1102+
{%- if ('|' + method + '|') not in methodNamesStr %}
10981103
describe('{{ method }}', () => {
10991104
it('invokes {{ method }} without error', async () => {
11001105
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1210,10 +1215,12 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
12101215
.getCall(0).calledWith(request, expectedOptions, undefined));
12111216
});
12121217
});
1218+
{%- endif %}
12131219
{%- endfor %}
12141220
{%- endif %}
12151221
{%- if service.LocationMixin > 0 %}
1216-
{%- set method = 'getLocation' %}
1222+
{%- if '|getLocation|' not in methodNamesStr %}
1223+
{%- set method = 'getLocation' %}
12171224
describe('{{ method }}', () => {
12181225
it('invokes {{ method }} without error', async () => {
12191226
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1320,7 +1327,9 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
13201327
.getCall(0).calledWith(request, expectedOptions, undefined));
13211328
});
13221329
});
1323-
{%- set method = 'listLocations' %}
1330+
{%- endif %}
1331+
{%- if '|listLocations|' not in methodNamesStr %}
1332+
{%- set method = 'listLocations' %}
13241333
describe('{{ method }}Async', () => {
13251334
it('uses async iteration with {{method}} without error', async () => {
13261335
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({{- util.initClientOptions(api.rest) -}});
@@ -1401,6 +1410,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
14011410
);
14021411
});
14031412
});
1413+
{%- endif %}
14041414
{%- endif %}
14051415
{%- if (service.LongRunningOperationsMixin) > 0 %}
14061416
{%- set Operationsmethods = ['getOperation', 'cancelOperation', 'deleteOperation'] %}

0 commit comments

Comments
 (0)