No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tasks.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.deleteExtensionInstanceTask = exports.configureExtensionInstanceTask = exports.updateExtensionInstanceTask = exports.createExtensionInstanceTask = exports.extensionsDeploymentHandler = void 0;
  4. const clc = require("colorette");
  5. const error_1 = require("../../error");
  6. const extensionsApi = require("../../extensions/extensionsApi");
  7. const extensionsHelper_1 = require("../../extensions/extensionsHelper");
  8. const refs = require("../../extensions/refs");
  9. const utils = require("../../utils");
  10. const isRetryable = (err) => err.status === 429 || err.status === 409;
  11. function extensionsDeploymentHandler(errorHandler) {
  12. return async (task) => {
  13. var _a, _b, _c, _d;
  14. let result;
  15. try {
  16. result = await task.run();
  17. }
  18. catch (err) {
  19. if (isRetryable(err)) {
  20. throw err;
  21. }
  22. errorHandler.record(task.spec.instanceId, task.type, (_d = (_c = (_b = (_a = err.context) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.message) !== null && _d !== void 0 ? _d : err);
  23. }
  24. return result;
  25. };
  26. }
  27. exports.extensionsDeploymentHandler = extensionsDeploymentHandler;
  28. function createExtensionInstanceTask(projectId, instanceSpec, validateOnly = false) {
  29. const run = async () => {
  30. if (instanceSpec.ref) {
  31. await extensionsApi.createInstance({
  32. projectId,
  33. instanceId: instanceSpec.instanceId,
  34. params: instanceSpec.params,
  35. extensionVersionRef: refs.toExtensionVersionRef(instanceSpec.ref),
  36. allowedEventTypes: instanceSpec.allowedEventTypes,
  37. eventarcChannel: instanceSpec.eventarcChannel,
  38. validateOnly,
  39. });
  40. }
  41. else if (instanceSpec.localPath) {
  42. const extensionSource = await (0, extensionsHelper_1.createSourceFromLocation)(projectId, instanceSpec.localPath);
  43. await extensionsApi.createInstance({
  44. projectId,
  45. instanceId: instanceSpec.instanceId,
  46. params: instanceSpec.params,
  47. extensionSource,
  48. allowedEventTypes: instanceSpec.allowedEventTypes,
  49. eventarcChannel: instanceSpec.eventarcChannel,
  50. validateOnly,
  51. });
  52. }
  53. else {
  54. throw new error_1.FirebaseError(`Tried to create extension instance ${instanceSpec.instanceId} without a ref or a local path. This should never happen.`);
  55. }
  56. printSuccess(instanceSpec.instanceId, "create", validateOnly);
  57. return;
  58. };
  59. return {
  60. run,
  61. spec: instanceSpec,
  62. type: "create",
  63. };
  64. }
  65. exports.createExtensionInstanceTask = createExtensionInstanceTask;
  66. function updateExtensionInstanceTask(projectId, instanceSpec, validateOnly = false) {
  67. const run = async () => {
  68. if (instanceSpec.ref) {
  69. await extensionsApi.updateInstanceFromRegistry({
  70. projectId,
  71. instanceId: instanceSpec.instanceId,
  72. extRef: refs.toExtensionVersionRef(instanceSpec.ref),
  73. params: instanceSpec.params,
  74. canEmitEvents: !!instanceSpec.allowedEventTypes,
  75. allowedEventTypes: instanceSpec.allowedEventTypes,
  76. eventarcChannel: instanceSpec.eventarcChannel,
  77. validateOnly,
  78. });
  79. }
  80. else if (instanceSpec.localPath) {
  81. const extensionSource = await (0, extensionsHelper_1.createSourceFromLocation)(projectId, instanceSpec.localPath);
  82. await extensionsApi.updateInstance({
  83. projectId,
  84. instanceId: instanceSpec.instanceId,
  85. extensionSource,
  86. validateOnly,
  87. canEmitEvents: !!instanceSpec.allowedEventTypes,
  88. allowedEventTypes: instanceSpec.allowedEventTypes,
  89. eventarcChannel: instanceSpec.eventarcChannel,
  90. });
  91. }
  92. else {
  93. throw new error_1.FirebaseError(`Tried to update extension instance ${instanceSpec.instanceId} without a ref or a local path. This should never happen.`);
  94. }
  95. printSuccess(instanceSpec.instanceId, "update", validateOnly);
  96. return;
  97. };
  98. return {
  99. run,
  100. spec: instanceSpec,
  101. type: "update",
  102. };
  103. }
  104. exports.updateExtensionInstanceTask = updateExtensionInstanceTask;
  105. function configureExtensionInstanceTask(projectId, instanceSpec, validateOnly = false) {
  106. const run = async () => {
  107. if (instanceSpec.ref) {
  108. await extensionsApi.configureInstance({
  109. projectId,
  110. instanceId: instanceSpec.instanceId,
  111. params: instanceSpec.params,
  112. canEmitEvents: !!instanceSpec.allowedEventTypes,
  113. allowedEventTypes: instanceSpec.allowedEventTypes,
  114. eventarcChannel: instanceSpec.eventarcChannel,
  115. validateOnly,
  116. });
  117. }
  118. else if (instanceSpec.localPath) {
  119. throw new error_1.FirebaseError(`Tried to configure extension instance ${instanceSpec.instanceId} from a local path. This should never happen.`);
  120. }
  121. else {
  122. throw new error_1.FirebaseError(`Tried to configure extension instance ${instanceSpec.instanceId} without a ref or a local path. This should never happen.`);
  123. }
  124. printSuccess(instanceSpec.instanceId, "configure", validateOnly);
  125. return;
  126. };
  127. return {
  128. run,
  129. spec: instanceSpec,
  130. type: "configure",
  131. };
  132. }
  133. exports.configureExtensionInstanceTask = configureExtensionInstanceTask;
  134. function deleteExtensionInstanceTask(projectId, instanceSpec) {
  135. const run = async () => {
  136. await extensionsApi.deleteInstance(projectId, instanceSpec.instanceId);
  137. printSuccess(instanceSpec.instanceId, "delete", false);
  138. return;
  139. };
  140. return {
  141. run,
  142. spec: instanceSpec,
  143. type: "delete",
  144. };
  145. }
  146. exports.deleteExtensionInstanceTask = deleteExtensionInstanceTask;
  147. function printSuccess(instanceId, type, validateOnly) {
  148. const action = validateOnly ? `validated ${type} for` : `${type}d`;
  149. utils.logSuccess(clc.bold(clc.green("extensions")) + ` Successfully ${action} ${instanceId}`);
  150. }