UNPKG

9.25 kBJavaScriptView Raw
1"use strict";
2// Copyright 2020 Google LLC
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// ** This file is automatically generated by gapic-generator-typescript. **
17// ** https://github.com/googleapis/gapic-generator-typescript **
18// ** All changes to this file may be overwritten. **
19Object.defineProperty(exports, "__esModule", { value: true });
20exports.IamClient = void 0;
21const grpc_1 = require("./grpc");
22const fallback_1 = require("./fallback");
23const createApiCall_1 = require("./createApiCall");
24const routingHeader = require("./routingHeader");
25const gapicConfig = require("./iam_policy_service_client_config.json");
26const fallback = require("./fallback");
27let version = require('../../package.json').version;
28const jsonProtos = require("../protos/iam_service.json");
29/**
30 * Google Cloud IAM Client.
31 * This is manually written for providing methods [setIamPolicy, getIamPolicy, testIamPerssion] to the generated client.
32 */
33class IamClient {
34 constructor(gaxGrpc,
35 // eslint-disable-next-line @typescript-eslint/no-explicit-any
36 options) {
37 this._terminated = false;
38 this.descriptors = { page: {}, stream: {}, longrunning: {} };
39 this.innerApiCalls = {};
40 this.gaxGrpc = gaxGrpc;
41 // Ensure that options include the service address and port.
42 const opts = Object.assign({
43 servicePath: options.servicePath,
44 port: options.port,
45 clientConfig: options.clientConfig,
46 apiEndpoint: options.apiEndpoint,
47 fallback: options.fallback,
48 }, options);
49 version = opts.fallback ? fallback.version : version;
50 this._gaxGrpc = opts.fallback
51 ? new fallback_1.GrpcClient(opts)
52 : new grpc_1.GrpcClient(opts);
53 opts.scopes = this.constructor.scopes;
54 // Save options to use in initialize() method.
55 this._opts = opts;
56 // Save the auth object to the client, for use by other methods.
57 this.auth = gaxGrpc.auth;
58 // Determine the client header string.
59 const clientHeader = [`gax/${version}`, `gapic/${version}`];
60 if (typeof process !== 'undefined' && 'versions' in process) {
61 clientHeader.push(`gl-node/${process.versions.node}`);
62 }
63 else {
64 clientHeader.push(`gl-web/${version}`);
65 }
66 if (!opts.fallback) {
67 clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
68 }
69 if (opts.libName && opts.libVersion) {
70 clientHeader.push(`${opts.libName}/${opts.libVersion}`);
71 }
72 // Load the applicable protos.
73 this._protos = this._gaxGrpc.loadProtoJSON(jsonProtos);
74 // Put together the default options sent with requests.
75 this._defaults = gaxGrpc.constructSettings('google.iam.v1.IAMPolicy', gapicConfig, opts.clientConfig || {}, { 'x-goog-api-client': clientHeader.join(' ') });
76 this.innerApiCalls = {};
77 }
78 /**
79 * Initialize the client.
80 * Performs asynchronous operations (such as authentication) and prepares the client.
81 * This function will be called automatically when any class method is called for the
82 * first time, but if you need to initialize it before calling an actual method,
83 * feel free to call initialize() directly.
84 *
85 * You can await on this method if you want to make sure the client is initialized.
86 *
87 * @returns {Promise} A promise that resolves to an authenticated service stub.
88 */
89 initialize() {
90 // If the client stub promise is already initialized, return immediately.
91 if (this.iamPolicyStub) {
92 return this.iamPolicyStub;
93 }
94 // Put together the "service stub" for
95 // google.iam.v1.IAMPolicy.
96 this.iamPolicyStub = this.gaxGrpc.createStub(this._opts.fallback
97 ? this._protos.lookupService('google.iam.v1.IAMPolicy')
98 : this._protos.google.iam.v1.IAMPolicy, this._opts);
99 // Iterate over each of the methods that the service provides
100 // and create an API call method for each.
101 const iamPolicyStubMethods = [
102 'getIamPolicy',
103 'setIamPolicy',
104 'testIamPermissions',
105 ];
106 for (const methodName of iamPolicyStubMethods) {
107 const innerCallPromise = this.iamPolicyStub.then(stub => (...args) => {
108 if (this._terminated) {
109 return Promise.reject('The client has already been closed.');
110 }
111 const func = stub[methodName];
112 return func.apply(stub, args);
113 }, (err) => () => {
114 throw err;
115 });
116 this.innerApiCalls[methodName] = createApiCall_1.createApiCall(innerCallPromise, this._defaults[methodName], this.descriptors.page[methodName]);
117 }
118 return this.iamPolicyStub;
119 }
120 /**
121 * The DNS address for this API service.
122 */
123 static get servicePath() {
124 return 'cloudkms.googleapis.com';
125 }
126 /**
127 * The DNS address for this API service - same as servicePath(),
128 * exists for compatibility reasons.
129 */
130 static get apiEndpoint() {
131 return 'cloudkms.googleapis.com';
132 }
133 /**
134 * The port for this API service.
135 */
136 static get port() {
137 return 443;
138 }
139 /**
140 * The scopes needed to make gRPC calls for every method defined
141 * in this service.
142 */
143 static get scopes() {
144 return [
145 'https://www.googleapis.com/auth/cloud-platform',
146 'https://www.googleapis.com/auth/cloudkms',
147 ];
148 }
149 getProjectId(callback) {
150 if (this.auth && 'getProjectId' in this.auth) {
151 return this.auth.getProjectId(callback);
152 }
153 if (callback) {
154 callback(new Error('Cannot determine project ID.'));
155 }
156 else {
157 return Promise.reject('Cannot determine project ID.');
158 }
159 }
160 getIamPolicy(request, optionsOrCallback, callback) {
161 let options;
162 if (optionsOrCallback instanceof Function && callback === undefined) {
163 callback = optionsOrCallback;
164 options = {};
165 }
166 else {
167 options = optionsOrCallback;
168 }
169 request = request || {};
170 options = options || {};
171 options.otherArgs = options.otherArgs || {};
172 options.otherArgs.headers = options.otherArgs.headers || {};
173 options.otherArgs.headers['x-goog-request-params'] =
174 routingHeader.fromParams({
175 resource: request.resource,
176 });
177 this.initialize();
178 return this.innerApiCalls.getIamPolicy(request, options, callback);
179 }
180 setIamPolicy(request, optionsOrCallback, callback) {
181 let options;
182 if (optionsOrCallback instanceof Function && callback === undefined) {
183 callback = optionsOrCallback;
184 options = {};
185 }
186 else {
187 options = optionsOrCallback;
188 }
189 request = request || {};
190 options = options || {};
191 options.otherArgs = options.otherArgs || {};
192 options.otherArgs.headers = options.otherArgs.headers || {};
193 options.otherArgs.headers['x-goog-request-params'] =
194 routingHeader.fromParams({
195 resource: request.resource,
196 });
197 this.initialize();
198 return this.innerApiCalls.setIamPolicy(request, options, callback);
199 }
200 testIamPermissions(request, optionsOrCallback, callback) {
201 let options;
202 if (optionsOrCallback instanceof Function && callback === undefined) {
203 callback = optionsOrCallback;
204 options = {};
205 }
206 else {
207 options = optionsOrCallback;
208 }
209 request = request || {};
210 options = options || {};
211 options.otherArgs = options.otherArgs || {};
212 options.otherArgs.headers = options.otherArgs.headers || {};
213 options.otherArgs.headers['x-goog-request-params'] =
214 routingHeader.fromParams({
215 resource: request.resource,
216 });
217 this.initialize();
218 return this.innerApiCalls.testIamPermissions(request, options, callback);
219 }
220 /**
221 * Terminate the GRPC channel and close the client.
222 *
223 * The client will no longer be usable and all future behavior is undefined.
224 */
225 close() {
226 this.initialize();
227 if (!this._terminated) {
228 return this.iamPolicyStub.then(stub => {
229 this._terminated = true;
230 stub.close();
231 });
232 }
233 return Promise.resolve();
234 }
235}
236exports.IamClient = IamClient;
237//# sourceMappingURL=iamService.js.map
\No newline at end of file