Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

run.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setInvokerUpdate = exports.setInvokerCreate = exports.getIamPolicy = exports.setIamPolicy = exports.replaceService = exports.serviceIsResolved = exports.updateService = exports.getService = exports.gcpIds = exports.LOCATION_LABEL = void 0;
  4. const apiv2_1 = require("../apiv2");
  5. const error_1 = require("../error");
  6. const api_1 = require("../api");
  7. const proto = require("./proto");
  8. const throttler_1 = require("../throttler/throttler");
  9. const logger_1 = require("../logger");
  10. const API_VERSION = "v1";
  11. const client = new apiv2_1.Client({
  12. urlPrefix: api_1.runOrigin,
  13. auth: true,
  14. apiVersion: API_VERSION,
  15. });
  16. exports.LOCATION_LABEL = "cloud.googleapis.com/location";
  17. function gcpIds(service) {
  18. var _a;
  19. return {
  20. serviceId: service.metadata.name,
  21. projectNumber: service.metadata.namespace,
  22. region: ((_a = service.metadata.labels) === null || _a === void 0 ? void 0 : _a[exports.LOCATION_LABEL]) || "unknown-region",
  23. };
  24. }
  25. exports.gcpIds = gcpIds;
  26. async function getService(name) {
  27. try {
  28. const response = await client.get(name);
  29. return response.body;
  30. }
  31. catch (err) {
  32. throw new error_1.FirebaseError(`Failed to fetch Run service ${name}`, {
  33. original: err,
  34. });
  35. }
  36. }
  37. exports.getService = getService;
  38. async function updateService(name, service) {
  39. delete service.status;
  40. service = await exports.replaceService(name, service);
  41. let retry = 0;
  42. while (!exports.serviceIsResolved(service)) {
  43. await (0, throttler_1.backoff)(retry, 2, 30);
  44. retry = retry + 1;
  45. service = await exports.getService(name);
  46. }
  47. return service;
  48. }
  49. exports.updateService = updateService;
  50. function serviceIsResolved(service) {
  51. var _a, _b, _c, _d, _e;
  52. if (((_a = service.status) === null || _a === void 0 ? void 0 : _a.observedGeneration) !== service.metadata.generation) {
  53. logger_1.logger.debug(`Service ${service.metadata.name} is not resolved because` +
  54. `observed generation ${(_b = service.status) === null || _b === void 0 ? void 0 : _b.observedGeneration} does not ` +
  55. `match spec generation ${service.metadata.generation}`);
  56. return false;
  57. }
  58. const readyCondition = (_d = (_c = service.status) === null || _c === void 0 ? void 0 : _c.conditions) === null || _d === void 0 ? void 0 : _d.find((condition) => {
  59. return condition.type === "Ready";
  60. });
  61. if ((readyCondition === null || readyCondition === void 0 ? void 0 : readyCondition.status) === "Unknown") {
  62. logger_1.logger.debug(`Waiting for service ${service.metadata.name} to be ready. ` +
  63. `Status is ${JSON.stringify((_e = service.status) === null || _e === void 0 ? void 0 : _e.conditions)}`);
  64. return false;
  65. }
  66. else if ((readyCondition === null || readyCondition === void 0 ? void 0 : readyCondition.status) === "True") {
  67. return true;
  68. }
  69. logger_1.logger.debug(`Service ${service.metadata.name} has unexpected ready status ${JSON.stringify(readyCondition)}. It may have failed rollout.`);
  70. throw new error_1.FirebaseError(`Unexpected Status ${readyCondition === null || readyCondition === void 0 ? void 0 : readyCondition.status} for service ${service.metadata.name}`);
  71. }
  72. exports.serviceIsResolved = serviceIsResolved;
  73. async function replaceService(name, service) {
  74. try {
  75. const response = await client.put(name, service);
  76. return response.body;
  77. }
  78. catch (err) {
  79. throw new error_1.FirebaseError(`Failed to replace Run service ${name}`, {
  80. original: err,
  81. });
  82. }
  83. }
  84. exports.replaceService = replaceService;
  85. async function setIamPolicy(name, policy, httpClient = client) {
  86. try {
  87. await httpClient.post(`${name}:setIamPolicy`, {
  88. policy,
  89. updateMask: proto.fieldMasks(policy).join(","),
  90. });
  91. }
  92. catch (err) {
  93. throw new error_1.FirebaseError(`Failed to set the IAM Policy on the Service ${name}`, {
  94. original: err,
  95. });
  96. }
  97. }
  98. exports.setIamPolicy = setIamPolicy;
  99. async function getIamPolicy(serviceName, httpClient = client) {
  100. try {
  101. const response = await httpClient.get(`${serviceName}:getIamPolicy`);
  102. return response.body;
  103. }
  104. catch (err) {
  105. throw new error_1.FirebaseError(`Failed to get the IAM Policy on the Service ${serviceName}`, {
  106. original: err,
  107. });
  108. }
  109. }
  110. exports.getIamPolicy = getIamPolicy;
  111. async function setInvokerCreate(projectId, serviceName, invoker, httpClient = client) {
  112. if (invoker.length === 0) {
  113. throw new error_1.FirebaseError("Invoker cannot be an empty array");
  114. }
  115. const invokerMembers = proto.getInvokerMembers(invoker, projectId);
  116. const invokerRole = "roles/run.invoker";
  117. const bindings = [{ role: invokerRole, members: invokerMembers }];
  118. const policy = {
  119. bindings: bindings,
  120. etag: "",
  121. version: 3,
  122. };
  123. await setIamPolicy(serviceName, policy, httpClient);
  124. }
  125. exports.setInvokerCreate = setInvokerCreate;
  126. async function setInvokerUpdate(projectId, serviceName, invoker, httpClient = client) {
  127. var _a;
  128. if (invoker.length === 0) {
  129. throw new error_1.FirebaseError("Invoker cannot be an empty array");
  130. }
  131. const invokerMembers = proto.getInvokerMembers(invoker, projectId);
  132. const invokerRole = "roles/run.invoker";
  133. const currentPolicy = await getIamPolicy(serviceName, httpClient);
  134. const currentInvokerBinding = (_a = currentPolicy.bindings) === null || _a === void 0 ? void 0 : _a.find((binding) => binding.role === invokerRole);
  135. if (currentInvokerBinding &&
  136. JSON.stringify(currentInvokerBinding.members.sort()) === JSON.stringify(invokerMembers.sort())) {
  137. return;
  138. }
  139. const bindings = (currentPolicy.bindings || []).filter((binding) => binding.role !== invokerRole);
  140. bindings.push({
  141. role: invokerRole,
  142. members: invokerMembers,
  143. });
  144. const policy = {
  145. bindings: bindings,
  146. etag: currentPolicy.etag || "",
  147. version: 3,
  148. };
  149. await setIamPolicy(serviceName, policy, httpClient);
  150. }
  151. exports.setInvokerUpdate = setInvokerUpdate;