iamService.js 8.8 KB

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