@geek-fun/serverlessinsight 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +1 -1
  2. package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +2 -2
  3. package/.serverlessinsight/state-insight-poc-app-insight-poc-tencent.json.backup +4 -8
  4. package/.serverlessinsight/state-insight-poc-app-log-insight-poc-tencent-log.json.backup +3 -8
  5. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +4 -4
  6. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +1 -1
  7. package/.serverlessinsight/state-insight-poc-timer-app-insight-poc-timer.json.backup +132 -0
  8. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +2 -2
  9. package/.serverlessinsight/state-insight-volc-nested-app-insight-volc-nested.json.backup +3 -3
  10. package/.serverlessinsight/state-nested-drift-app-nested-drift-service.json.backup +20 -5
  11. package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +3 -3
  12. package/AGENTS.md +10 -0
  13. package/CHANGELOG.md +51 -0
  14. package/README.md +103 -13
  15. package/README.zh-CN.md +73 -13
  16. package/codecov +0 -0
  17. package/codecov.SHA256SUM +1 -0
  18. package/codecov.SHA256SUM.sig +16 -0
  19. package/dist/package.json +1 -1
  20. package/dist/src/commands/index.js +92 -24
  21. package/dist/src/commands/local.js +16 -3
  22. package/dist/src/commands/plan.js +31 -4
  23. package/dist/src/commands/schema.js +24 -0
  24. package/dist/src/commands/show.js +64 -10
  25. package/dist/src/commands/validate.js +34 -2
  26. package/dist/src/common/aliyunClient/apigwOperations.js +17 -1
  27. package/dist/src/common/aliyunClient/fc3Operations.js +39 -2
  28. package/dist/src/common/aliyunClient/ossOperations.js +214 -221
  29. package/dist/src/common/bucketPolicy.js +63 -0
  30. package/dist/src/common/constants.js +7 -1
  31. package/dist/src/common/domainUtils.js +4 -1
  32. package/dist/src/common/functionDefinition.js +25 -0
  33. package/dist/src/common/index.js +4 -0
  34. package/dist/src/common/jsonOutput.js +82 -0
  35. package/dist/src/common/logger.js +6 -3
  36. package/dist/src/common/nameBuilder.js +3 -1
  37. package/dist/src/common/noColor.js +9 -0
  38. package/dist/src/common/planCapabilities.js +119 -0
  39. package/dist/src/common/planCompare.js +119 -66
  40. package/dist/src/common/planFormatter.js +34 -20
  41. package/dist/src/common/planRedaction.js +26 -0
  42. package/dist/src/common/refreshPlanner.js +66 -21
  43. package/dist/src/common/siCron.js +415 -0
  44. package/dist/src/common/stateBackend/saasStateBackend.js +5 -4
  45. package/dist/src/common/tencentClient/cosOperations.js +326 -322
  46. package/dist/src/common/tencentClient/scfOperations.js +11 -2
  47. package/dist/src/common/tencentClient/tdsqlcOperations.js +6 -11
  48. package/dist/src/common/triggerMapper.js +16 -1
  49. package/dist/src/common/volcengineClient/apigwOperations.js +82 -0
  50. package/dist/src/common/volcengineClient/iamOperations.js +27 -3
  51. package/dist/src/common/volcengineClient/tosOperations.js +62 -56
  52. package/dist/src/common/volcengineClient/vefaasOperations.js +45 -0
  53. package/dist/src/lang/en.js +70 -3
  54. package/dist/src/lang/index.js +36 -1
  55. package/dist/src/lang/zh-CN.js +70 -3
  56. package/dist/src/parser/eventParser.js +1 -0
  57. package/dist/src/parser/functionParser.js +48 -2
  58. package/dist/src/stack/aliyunStack/apigwExecutor.js +1 -1
  59. package/dist/src/stack/aliyunStack/apigwPlanner.js +103 -23
  60. package/dist/src/stack/aliyunStack/apigwResource.js +4 -3
  61. package/dist/src/stack/aliyunStack/apigwTypes.js +3 -7
  62. package/dist/src/stack/aliyunStack/deployer.js +2 -0
  63. package/dist/src/stack/aliyunStack/esServerlessTypes.js +8 -4
  64. package/dist/src/stack/aliyunStack/fc3Executor.js +7 -3
  65. package/dist/src/stack/aliyunStack/fc3Planner.js +148 -153
  66. package/dist/src/stack/aliyunStack/fc3Resource.js +206 -53
  67. package/dist/src/stack/aliyunStack/fc3Types.js +30 -16
  68. package/dist/src/stack/aliyunStack/ossPlanner.js +15 -1
  69. package/dist/src/stack/aliyunStack/ossResource.js +113 -102
  70. package/dist/src/stack/aliyunStack/ossTypes.js +20 -20
  71. package/dist/src/stack/localStack/aliyunFc.js +1 -6
  72. package/dist/src/stack/localStack/event.js +31 -71
  73. package/dist/src/stack/localStack/function.js +25 -77
  74. package/dist/src/stack/localStack/functionRunner.js +10 -2
  75. package/dist/src/stack/localStack/httpAdapter.js +54 -0
  76. package/dist/src/stack/localStack/httpError.js +5 -0
  77. package/dist/src/stack/localStack/index.js +8 -1
  78. package/dist/src/stack/localStack/legacyLambda.js +26 -0
  79. package/dist/src/stack/localStack/localContext.js +29 -0
  80. package/dist/src/stack/localStack/localRoute.js +12 -0
  81. package/dist/src/stack/localStack/localRuntimes.js +22 -0
  82. package/dist/src/stack/localStack/localServer.js +8 -3
  83. package/dist/src/stack/localStack/requestBody.js +24 -0
  84. package/dist/src/stack/localStack/requestId.js +18 -0
  85. package/dist/src/stack/localStack/tencent.js +147 -0
  86. package/dist/src/stack/localStack/timer.js +183 -0
  87. package/dist/src/stack/localStack/utils.js +19 -1
  88. package/dist/src/stack/localStack/volcengine.js +68 -0
  89. package/dist/src/stack/scfStack/cosPlanner.js +9 -14
  90. package/dist/src/stack/scfStack/cosResource.js +42 -50
  91. package/dist/src/stack/scfStack/cosTypes.js +19 -14
  92. package/dist/src/stack/scfStack/deployer.js +2 -0
  93. package/dist/src/stack/scfStack/scfExecutor.js +7 -3
  94. package/dist/src/stack/scfStack/scfHttpTrigger.js +106 -0
  95. package/dist/src/stack/scfStack/scfPlanner.js +114 -29
  96. package/dist/src/stack/scfStack/scfResource.js +329 -311
  97. package/dist/src/stack/scfStack/scfTypes.js +4 -3
  98. package/dist/src/stack/scfStack/tdsqlcPlanner.js +10 -13
  99. package/dist/src/stack/volcengineStack/apigwDomains.js +64 -0
  100. package/dist/src/stack/volcengineStack/apigwPlanner.js +71 -15
  101. package/dist/src/stack/volcengineStack/apigwResource.js +60 -24
  102. package/dist/src/stack/volcengineStack/apigwTypes.js +5 -1
  103. package/dist/src/stack/volcengineStack/deployer.js +6 -0
  104. package/dist/src/stack/volcengineStack/tosPlanner.js +8 -13
  105. package/dist/src/stack/volcengineStack/tosResource.js +22 -35
  106. package/dist/src/stack/volcengineStack/tosTypes.js +5 -8
  107. package/dist/src/stack/volcengineStack/vefaasPlanner.js +186 -44
  108. package/dist/src/stack/volcengineStack/vefaasResource.js +232 -14
  109. package/dist/src/stack/volcengineStack/vefaasTypes.js +6 -3
  110. package/dist/src/validator/functionSchema.js +25 -2
  111. package/dist/src/validator/iacSchema.js +2 -1
  112. package/dist/src/validator/index.js +4 -1
  113. package/dist/src/validator/rootSchema.js +49 -18
  114. package/dist/src/validator/schemaExport.js +60 -0
  115. package/dist/src/validator/semanticValidation.js +79 -0
  116. package/dist/src/validator/templateRefSchema.js +9 -1
  117. package/dist/tsconfig.tsbuildinfo +1 -1
  118. package/package.json +1 -1
  119. package/release-notes.md +79 -0
  120. package/samples/aliyun-poc-timer.yml +39 -0
  121. package/samples/tencent-local/index.js +13 -0
  122. package/samples/tencent-local/serverlessinsight.yml +23 -0
  123. package/samples/volcengine-local/index.js +13 -0
  124. package/samples/volcengine-local/serverlessinsight.yml +28 -0
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deleteResource = exports.updateResource = exports.readResource = exports.createResource = exports.deriveScfExecutionStatements = void 0;
4
+ const functionDefinition_1 = require("../../common/functionDefinition");
5
+ const planCompare_1 = require("../../common/planCompare");
6
+ const scfHttpTrigger_1 = require("./scfHttpTrigger");
4
7
  const types_1 = require("../../types");
8
+ const siCron_1 = require("../../common/siCron");
5
9
  const tencentClient_1 = require("../../common/tencentClient");
6
10
  const fileUtils_1 = require("../../common/fileUtils");
7
11
  const scfTypes_1 = require("./scfTypes");
@@ -15,26 +19,148 @@ const ownershipTag_1 = require("../ownershipTag");
15
19
  const alreadyExists_1 = require("../alreadyExists");
16
20
  const sharedLogset_1 = require("./sharedLogset");
17
21
  const clsOperations_1 = require("../../common/tencentClient/clsOperations");
22
+ const TIMER_TRIGGER_INSTANCE_TYPE = 'TENCENT_SCF_TIMER_TRIGGER';
23
+ /** Tencent timer dialect: 7-field cron (UTC+8, `*` year), payload → CustomArgument. */
24
+ const buildScfTimerTriggerConfig = (timer) => ({
25
+ cron: (0, siCron_1.translateSiCron)(timer.cron, 'tencent'),
26
+ ...(timer.payload !== undefined ? { payload: timer.payload } : {}),
27
+ enable: timer.enable,
28
+ ...(timer.description !== undefined ? { description: timer.description } : {}),
29
+ });
30
+ const normalizeScfLiveTimerConfig = (trigger) => ({
31
+ cron: trigger.TriggerDesc ?? '',
32
+ ...(trigger.CustomArgument !== undefined && trigger.CustomArgument !== ''
33
+ ? { payload: trigger.CustomArgument }
34
+ : {}),
35
+ enable: trigger.Enable === 1 || trigger.Enable === 'OPEN',
36
+ ...(trigger.Description ? { description: trigger.Description } : {}),
37
+ });
38
+ const scfTimerConfigFromAttributes = (attrs) => {
39
+ const cron = typeof attrs.cron === 'string' ? attrs.cron : undefined;
40
+ if (!cron) {
41
+ return null;
42
+ }
43
+ try {
44
+ return {
45
+ cron: (0, siCron_1.translateSiCron)(cron, 'tencent'),
46
+ ...(attrs.payload !== undefined ? { payload: String(attrs.payload) } : {}),
47
+ enable: attrs.enable !== false,
48
+ ...(attrs.description !== undefined ? { description: String(attrs.description) } : {}),
49
+ };
50
+ }
51
+ catch {
52
+ return null;
53
+ }
54
+ };
55
+ const buildScfTimerTriggerInstance = (context, fn, timer) => ({
56
+ sid: (0, common_1.buildSid)('tencent', 'scf-timer-trigger', context.stage, `${fn.name}.${timer.name}`),
57
+ type: TIMER_TRIGGER_INSTANCE_TYPE,
58
+ id: timer.name,
59
+ cron: timer.cron,
60
+ ...(timer.payload !== undefined ? { payload: timer.payload } : {}),
61
+ enable: timer.enable,
62
+ ...(timer.description !== undefined ? { description: timer.description } : {}),
63
+ });
64
+ const deleteScfTimerTrigger = async (fnName, triggerName, client) => {
65
+ try {
66
+ await client.scf.deleteTrigger({
67
+ FunctionName: fnName,
68
+ TriggerName: triggerName,
69
+ Type: 'timer',
70
+ });
71
+ logger_1.logger.info(lang_1.lang.__('TIMER_TRIGGER_DELETED', { triggerName }));
72
+ return true;
73
+ }
74
+ catch (err) {
75
+ const errorCode = err?.code;
76
+ if (errorCode === 'ResourceNotFound.TriggerName' || errorCode === 'ResourceNotFound') {
77
+ logger_1.logger.warn(lang_1.lang.__('TIMER_TRIGGER_NOT_FOUND', { triggerName }));
78
+ return true;
79
+ }
80
+ throw err;
81
+ }
82
+ };
18
83
  /**
19
- * Build the Function URL trigger description for Tencent CreateTrigger
20
- * (Type: 'http'). Format per official docs (创建函数 URL / Creating a Function
21
- * URL): { AuthType, NetConfig: { EnableIntranet, EnableExtranet } }.
22
- * AuthType is the mapAuthType result ('NONE' = no auth, 'CAM' = CAM auth);
23
- * the API Gateway trigger format ({ api/service/release }) is obsolete —
24
- * Tencent shut down new API Gateway triggers on 2024-07-01.
84
+ * Reconcile the function's timer triggers against the provider (issue #258).
85
+ *
86
+ * The live trigger list from GetFunction is the reconcile baseline, so
87
+ * console-side cron edits, manual deletions and extra triggers converge on
88
+ * every deploy. When the probe fails the state-recorded instances take over.
89
+ * Name is the drift key; si-cron stays stored and is translated only here.
25
90
  */
26
- const buildTencentTriggerDesc = (authType, netConfig) => JSON.stringify({
27
- AuthType: authType,
28
- NetConfig: {
29
- EnableIntranet: netConfig.enableIntranet ?? false,
30
- EnableExtranet: netConfig.enableExtranet ?? true,
31
- },
32
- });
33
- // Tencent names Function URL triggers with a random id (e.g. "5obtzwwxw1"), not
34
- // our ${fn.key}-http-trigger, so name-based matching never finds them. A
35
- // Function URL trigger is identified by Type 'http' + a TriggerDesc carrying
36
- // the NetConfig field (the API Gateway flavor uses api/service/release instead).
37
- const isFunctionUrlTrigger = (t) => t.Type === 'http' && typeof t.TriggerDesc === 'string' && t.TriggerDesc.includes('NetConfig');
91
+ const reconcileScfTimerTriggers = async (context, fn, previousInstances, client) => {
92
+ const desiredTimers = fn.triggers?.timer ?? [];
93
+ const previousTimers = previousInstances.filter((i) => i.type === TIMER_TRIGGER_INSTANCE_TYPE);
94
+ if (desiredTimers.length === 0 && previousTimers.length === 0) {
95
+ return [];
96
+ }
97
+ let liveTimers = [];
98
+ let liveAvailable = true;
99
+ try {
100
+ const probe = await client.scf.getFunction(fn.name);
101
+ liveTimers = (probe?.Triggers ?? [])
102
+ .filter((trigger) => trigger.Type === 'timer' && trigger.TriggerName)
103
+ .map((trigger) => ({
104
+ name: trigger.TriggerName,
105
+ config: normalizeScfLiveTimerConfig(trigger),
106
+ }));
107
+ }
108
+ catch (error) {
109
+ liveAvailable = false;
110
+ logger_1.logger.warn(lang_1.lang.__('PLAN_TIMER_TRIGGER_PROBE_FAILED', { functionName: fn.name, error: String(error) }));
111
+ }
112
+ const baseline = liveAvailable
113
+ ? liveTimers
114
+ : previousTimers.flatMap((instance) => {
115
+ const config = scfTimerConfigFromAttributes(instance);
116
+ return config ? [{ name: String(instance.id), config }] : [];
117
+ });
118
+ const resultingInstances = [];
119
+ for (const timer of desiredTimers) {
120
+ const desiredConfig = buildScfTimerTriggerConfig(timer);
121
+ const live = baseline.find((entry) => entry.name === timer.name);
122
+ if (!live) {
123
+ logger_1.logger.info(lang_1.lang.__('CREATING_TIMER_TRIGGER', { triggerName: timer.name, functionName: fn.name }));
124
+ }
125
+ else if (!(0, common_1.attributesEqual)(live.config, desiredConfig)) {
126
+ logger_1.logger.info(lang_1.lang.__('UPDATING_TIMER_TRIGGER', { triggerName: timer.name, functionName: fn.name }));
127
+ await deleteScfTimerTrigger(fn.name, timer.name, client);
128
+ }
129
+ else {
130
+ resultingInstances.push(buildScfTimerTriggerInstance(context, fn, timer));
131
+ continue;
132
+ }
133
+ try {
134
+ await client.scf.createTrigger({
135
+ FunctionName: fn.name,
136
+ TriggerName: timer.name,
137
+ Type: 'timer',
138
+ TriggerDesc: desiredConfig.cron,
139
+ ...(desiredConfig.payload !== undefined ? { CustomArgument: desiredConfig.payload } : {}),
140
+ Qualifier: '$DEFAULT',
141
+ Enable: desiredConfig.enable ? 'OPEN' : 'CLOSE',
142
+ ...(desiredConfig.description ? { Description: desiredConfig.description } : {}),
143
+ });
144
+ }
145
+ catch (error) {
146
+ // a leftover trigger from an adopted function may already carry the name
147
+ if (!(0, alreadyExists_1.isResourceAlreadyExistsError)(error))
148
+ throw error;
149
+ logger_1.logger.warn(lang_1.lang.__('TIMER_TRIGGER_ALREADY_ATTACHED', {
150
+ triggerName: timer.name,
151
+ functionName: fn.name,
152
+ }));
153
+ }
154
+ logger_1.logger.info(lang_1.lang.__('TIMER_TRIGGER_CREATED', { triggerName: timer.name, functionName: fn.name }));
155
+ resultingInstances.push(buildScfTimerTriggerInstance(context, fn, timer));
156
+ }
157
+ const desiredNames = new Set(desiredTimers.map((timer) => timer.name));
158
+ for (const stale of baseline.filter((entry) => !desiredNames.has(entry.name))) {
159
+ logger_1.logger.info(lang_1.lang.__('DELETING_TIMER_TRIGGER', { triggerName: stale.name, functionName: fn.name }));
160
+ await deleteScfTimerTrigger(fn.name, stale.name, client);
161
+ }
162
+ return resultingInstances;
163
+ };
38
164
  /**
39
165
  * Best-effort extraction of a cloud-SDK error's useful details. Tencent SDK
40
166
  * errors carry `code`/`message`/`requestId` (and may throw with an empty
@@ -100,13 +226,15 @@ const isLogConfigChanged = (desiredDefinition, existingDefinition) => {
100
226
  }
101
227
  return !(0, common_1.attributesEqual)(desiredLogConfig, existingDefinition.logConfig ?? {});
102
228
  };
103
- const buildScfInstanceFromProvider = (info, sid) => {
229
+ const isScfFunctionInstance = (instance) => typeof instance.functionName === 'string' || instance.type == null;
230
+ const buildScfInstanceFromProvider = (info, sid, ownedHttpIds = []) => {
104
231
  const envMap = info.Environment?.Variables?.reduce((acc, v) => ({ ...acc, [v.Key]: v.Value }), {}) ?? {};
105
232
  const triggersArray = info.Triggers?.map((t) => ({
106
233
  modTime: t.ModTime,
107
234
  type: t.Type,
108
235
  triggerDesc: t.TriggerDesc,
109
236
  triggerName: t.TriggerName,
237
+ ...(t.Type === 'http' ? { owned: ownedHttpIds.includes(t.TriggerName) } : {}),
110
238
  addTime: t.AddTime,
111
239
  enable: t.Enable,
112
240
  customArgument: t.CustomArgument ?? null,
@@ -330,7 +458,7 @@ const createDependentResources = async (context, fn, existingInstances = [], sta
330
458
  clsInstances.push(logsetInstance, topicInstance);
331
459
  }
332
460
  else {
333
- const legacyFn = existingInstances.find((i) => i.type === undefined);
461
+ const legacyFn = existingInstances.find((i) => isScfFunctionInstance(i));
334
462
  if (legacyFn?.clsLogsetId && legacyFn?.clsTopicId) {
335
463
  clsConfig = {
336
464
  logsetId: legacyFn.clsLogsetId,
@@ -474,7 +602,7 @@ const createResource = async (context, fn, state) => {
474
602
  const codePath = fn.code.path;
475
603
  const codeBase64 = (0, fileUtils_1.readFileAsBase64)(codePath);
476
604
  const codeHash = await (0, hashUtils_1.computeZipContentHash)(codePath);
477
- const definition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam);
605
+ const definition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam, fn);
478
606
  const dependentInstances = [...dependentResources.clsInstances];
479
607
  if (dependentResources.role) {
480
608
  const iamConfig = fn.iam?.role;
@@ -499,14 +627,10 @@ const createResource = async (context, fn, state) => {
499
627
  const stateWithShared = dependentResources.sharedInstance
500
628
  ? (0, stateManager_1.setSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY, dependentResources.sharedInstance)
501
629
  : state;
502
- const stateAfterDependents = (0, stateManager_1.setResource)(stateWithShared, logicalId, taintedResourceState);
630
+ let stateAfterDependents = (0, stateManager_1.setResource)(stateWithShared, logicalId, taintedResourceState);
503
631
  const client = (0, tencentClient_1.createTencentClient)(context);
504
632
  const isTainted = existingResourceState?.status === 'tainted';
505
633
  const existingFunctionOnRetry = isTainted ? await client.scf.getFunction(fn.name) : null;
506
- // Provider info of a function adopted from a previous partial run (tainted
507
- // retry or reconciled after a create error) — used to skip already-attached
508
- // triggers below.
509
- let adoptedInfo = existingFunctionOnRetry;
510
634
  if (existingFunctionOnRetry) {
511
635
  logger_1.logger.info(lang_1.lang.__('TAINTED_RECOVERY_SKIP_CREATE', { resourceType: 'Function', resourceName: fn.name }));
512
636
  }
@@ -526,7 +650,6 @@ const createResource = async (context, fn, state) => {
526
650
  await delay(RECOVERY_GET_FUNCTION_DELAY_MS);
527
651
  const afterError = await client.scf.getFunction(fn.name);
528
652
  if (afterError) {
529
- adoptedInfo = afterError;
530
653
  logger_1.logger.info(lang_1.lang.__('FC3_FOUND_AFTER_CREATE_RECONCILE', { functionName: fn.name }));
531
654
  }
532
655
  else {
@@ -542,7 +665,6 @@ const createResource = async (context, fn, state) => {
542
665
  // a resource that was never ours).
543
666
  const probe = await client.scf.getFunction(fn.name);
544
667
  if (probe && (0, ownershipTag_1.isOwnedByStack)(context, logicalId, probe.Tags)) {
545
- adoptedInfo = probe;
546
668
  logger_1.logger.info(lang_1.lang.__('RESOURCE_OWNERSHIP_TAG_ADOPT', {
547
669
  resourceType: 'Function',
548
670
  name: fn.name,
@@ -561,55 +683,28 @@ const createResource = async (context, fn, state) => {
561
683
  throw new types_1.PartialResourceError(stateAfterDependents, new Error(toErrorMessage(error)));
562
684
  }
563
685
  }
564
- // Create HTTP trigger if configured
565
- if (fn.triggers?.http) {
566
- const authType = (0, common_1.mapAuthType)(common_1.ProviderEnum.TENCENT, fn.triggers.http.auth_type);
567
- const access = (0, common_1.mapAccess)(fn.triggers.http.access);
568
- const triggerName = `${fn.key}-http-trigger`;
569
- const triggerDesc = buildTencentTriggerDesc(authType, access);
570
- // Probe the provider for an already-attached trigger BEFORE attempting to
571
- // create it. A leftover trigger from a previous partial run (function exists
572
- // in cloud, state blank) would otherwise collide with CreateTrigger and
573
- // surface as a confusing empty error. Applies to both fresh creates (function
574
- // just created — Triggers from the createFunction flow) and tainted retries.
575
- let providerTriggers = adoptedInfo?.Triggers ?? [];
576
- if (providerTriggers.length === 0) {
577
- const probe = await client.scf.getFunction(fn.name);
578
- if (probe) {
579
- providerTriggers = probe.Triggers ?? [];
580
- }
581
- }
582
- const triggerAlreadyAttached = providerTriggers.some(isFunctionUrlTrigger);
583
- if (triggerAlreadyAttached) {
584
- logger_1.logger.info(lang_1.lang.__('SCF_HTTP_TRIGGER_ALREADY_ATTACHED', { triggerName, functionName: fn.name }));
686
+ try {
687
+ const httpInstances = await (0, scfHttpTrigger_1.reconcileScfHttpTrigger)(context, fn, existingResourceState?.instances ?? [], existingResourceState?.definition ?? {}, client, false, existingResourceState?.status === 'tainted');
688
+ dependentInstances.push(...httpInstances);
689
+ if (httpInstances.length)
690
+ stateAfterDependents = (0, stateManager_1.setResource)(stateAfterDependents, logicalId, {
691
+ ...taintedResourceState,
692
+ instances: [...dependentInstances],
693
+ });
694
+ }
695
+ catch (error) {
696
+ throw new types_1.PartialResourceError(stateAfterDependents, new Error(toErrorMessage(error)));
697
+ }
698
+ // Create timer triggers if configured — a failure mid-way persists the
699
+ // tainted state so a re-run reconciles instead of duplicating triggers.
700
+ if (fn.triggers?.timer) {
701
+ try {
702
+ dependentInstances.push(...(await reconcileScfTimerTriggers(context, fn, [], client)));
585
703
  }
586
- else {
587
- logger_1.logger.info(lang_1.lang.__('CREATING_HTTP_TRIGGER', { triggerName, functionName: fn.name }));
588
- try {
589
- await client.scf.createTrigger({
590
- FunctionName: fn.name,
591
- TriggerName: triggerName,
592
- Type: 'http',
593
- TriggerDesc: triggerDesc,
594
- Qualifier: '$DEFAULT',
595
- Enable: 'OPEN',
596
- });
597
- logger_1.logger.info(lang_1.lang.__('HTTP_TRIGGER_CREATED', { triggerName, functionName: fn.name }));
598
- }
599
- catch (error) {
600
- if ((0, alreadyExists_1.isResourceAlreadyExistsError)(error)) {
601
- logger_1.logger.warn(lang_1.lang.__('SCF_HTTP_TRIGGER_ALREADY_EXISTS', {
602
- triggerName,
603
- functionName: fn.name,
604
- error: String(error),
605
- }));
606
- }
607
- else {
608
- // The function exists in the cloud but the trigger failed to attach —
609
- // persist the tainted state so a re-run reconciles instead of losing it.
610
- throw new types_1.PartialResourceError(stateAfterDependents, new Error(toErrorMessage(error)));
611
- }
612
- }
704
+ catch (error) {
705
+ if (error instanceof types_1.PartialResourceError)
706
+ throw error;
707
+ throw new types_1.PartialResourceError(stateAfterDependents, new Error(toErrorMessage(error)));
613
708
  }
614
709
  }
615
710
  // Create custom domain if configured
@@ -661,7 +756,9 @@ const createResource = async (context, fn, state) => {
661
756
  region: context.region,
662
757
  definition,
663
758
  instances: [
664
- buildScfInstanceFromProvider(functionInfo, sid),
759
+ buildScfInstanceFromProvider(functionInfo, sid, dependentInstances
760
+ .filter((instance) => instance.type === scfHttpTrigger_1.SCF_HTTP_TRIGGER_TYPE)
761
+ .map((instance) => instance.id)),
665
762
  ...dependentInstances,
666
763
  ],
667
764
  lastUpdated: new Date().toISOString(),
@@ -675,11 +772,12 @@ const readResource = async (context, functionName) => {
675
772
  return await client.scf.getFunction(functionName);
676
773
  };
677
774
  exports.readResource = readResource;
678
- const updateResource = async (context, fn, state, options) => {
679
- const logicalId = `functions.${fn.key}`;
775
+ const updateResource = async (context, providedFn, state, options) => {
776
+ const logicalId = `functions.${providedFn.key}`;
680
777
  const existingState = (0, stateManager_1.getResource)(state, logicalId);
778
+ const fn = (0, functionDefinition_1.withStoredFunctionIam)(providedFn, existingState?.definition);
681
779
  const existingInstances = (existingState?.instances ?? []);
682
- const existingFnInstance = existingInstances.find((i) => i.type === undefined);
780
+ const existingFnInstance = existingInstances.find((i) => isScfFunctionInstance(i));
683
781
  const existingClsTopicInstance = existingInstances.find((i) => i.type === types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC);
684
782
  const existingDefinition = (existingState?.definition ?? {});
685
783
  const legacyLogsetId = existingFnInstance?.clsLogsetId;
@@ -772,7 +870,7 @@ const updateResource = async (context, fn, state, options) => {
772
870
  let clsConfig;
773
871
  let clsTopicInstance;
774
872
  let clsSharedInstance;
775
- const disableLog = !fn.log && wasLogEnabled;
873
+ const disableLog = fn.log === false && wasLogEnabled;
776
874
  let topicToDelete;
777
875
  let topicNameToDelete;
778
876
  let sharedLogsetReleased = false;
@@ -839,7 +937,7 @@ const updateResource = async (context, fn, state, options) => {
839
937
  // Tencent's UpdateFunctionConfiguration rejects Handler/Runtime (immutable at
840
938
  // creation) and we don't want to re-send unchanged values. Handler/Runtime
841
939
  // changes are a hard error: the platform cannot apply them on update.
842
- const desiredDefinition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam);
940
+ const desiredDefinition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam, fn);
843
941
  const CONFIG_DIFF_KEYS = ['runtime', 'handler', 'memorySize', 'timeout', 'environment', 'role'];
844
942
  const mutableKeys = CONFIG_DIFF_KEYS.filter((k) => k !== 'runtime' && k !== 'handler');
845
943
  if (desiredDefinition.handler &&
@@ -858,7 +956,7 @@ const updateResource = async (context, fn, state, options) => {
858
956
  newRuntime: String(desiredDefinition.runtime),
859
957
  }));
860
958
  }
861
- const configChanged = mutableKeys.some((k) => desiredDefinition[k] !== existingDefinition[k]) ||
959
+ const configChanged = mutableKeys.some((k) => (0, planCompare_1.definitionsDiffer)({ [k]: existingDefinition[k] }, { [k]: desiredDefinition[k] })) ||
862
960
  isLogConfigChanged(desiredDefinition, existingDefinition) ||
863
961
  // Disabling log always re-pushes config so the empty Cls ids reach the
864
962
  // UpdateFunctionConfiguration unbind contract.
@@ -888,146 +986,26 @@ const updateResource = async (context, fn, state, options) => {
888
986
  }
889
987
  // Update code
890
988
  await client.scf.updateFunctionCode(fn.name, codeBase64);
891
- const existingHttpTrigger = existingFnInstance?.triggers?.find((t) => t.type === 'http');
892
- const desiredHttpConfig = fn.triggers?.http;
893
- if (desiredHttpConfig) {
894
- if (!desiredHttpConfig.auth_type) {
895
- throw new Error(lang_1.lang.__('HTTP_TRIGGER_AUTH_TYPE_REQUIRED', { functionName: fn.name }));
896
- }
897
- const authType = (0, common_1.mapAuthType)(common_1.ProviderEnum.TENCENT, desiredHttpConfig.auth_type);
898
- const access = (0, common_1.mapAccess)(desiredHttpConfig.access);
899
- const triggerName = `${fn.key}-http-trigger`;
900
- const desiredTriggerDesc = buildTencentTriggerDesc(authType, access);
901
- if (existingHttpTrigger) {
902
- if (existingHttpTrigger.triggerDesc !== desiredTriggerDesc) {
903
- logger_1.logger.info(lang_1.lang.__('UPDATING_HTTP_TRIGGER', { functionName: fn.name }));
904
- try {
905
- await client.scf.deleteTrigger({
906
- FunctionName: fn.name,
907
- TriggerName: existingHttpTrigger.triggerName,
908
- Type: 'http',
909
- });
910
- }
911
- catch (err) {
912
- const errorCode = err?.code;
913
- if (errorCode !== 'ResourceNotFound.TriggerName' && errorCode !== 'ResourceNotFound') {
914
- throw err;
915
- }
916
- }
917
- await client.scf.createTrigger({
918
- FunctionName: fn.name,
919
- TriggerName: triggerName,
920
- Type: 'http',
921
- TriggerDesc: desiredTriggerDesc,
922
- Qualifier: '$DEFAULT',
923
- Enable: 'OPEN',
924
- });
925
- }
926
- }
927
- else {
928
- // No trigger recorded in state — but a leftover trigger may still exist
929
- // in the provider (e.g. adopted function whose state lacked trigger
930
- // records). Probe before creating to stay idempotent.
931
- const probe = await client.scf.getFunction(fn.name);
932
- const providerTriggerAttached = (probe?.Triggers ?? []).some(isFunctionUrlTrigger);
933
- if (providerTriggerAttached) {
934
- logger_1.logger.info(lang_1.lang.__('SCF_HTTP_TRIGGER_ALREADY_ATTACHED', { triggerName, functionName: fn.name }));
935
- }
936
- else {
937
- logger_1.logger.info(lang_1.lang.__('CREATING_HTTP_TRIGGER', { triggerName, functionName: fn.name }));
938
- try {
939
- await client.scf.createTrigger({
940
- FunctionName: fn.name,
941
- TriggerName: triggerName,
942
- Type: 'http',
943
- TriggerDesc: desiredTriggerDesc,
944
- Qualifier: '$DEFAULT',
945
- Enable: 'OPEN',
946
- });
947
- logger_1.logger.info(lang_1.lang.__('HTTP_TRIGGER_CREATED', { triggerName, functionName: fn.name }));
948
- }
949
- catch (error) {
950
- if ((0, alreadyExists_1.isResourceAlreadyExistsError)(error)) {
951
- logger_1.logger.warn(lang_1.lang.__('SCF_HTTP_TRIGGER_ALREADY_EXISTS', {
952
- triggerName,
953
- functionName: fn.name,
954
- error: String(error),
955
- }));
956
- }
957
- else {
958
- throw error;
959
- }
960
- }
961
- }
962
- }
963
- }
964
- else if (existingHttpTrigger) {
965
- const tName = existingHttpTrigger.triggerName;
966
- logger_1.logger.info(lang_1.lang.__('DELETING_HTTP_TRIGGER', { triggerName: tName, functionName: fn.name }));
967
- try {
968
- await client.scf.deleteTrigger({
969
- FunctionName: fn.name,
970
- TriggerName: tName,
971
- Type: 'http',
972
- });
973
- logger_1.logger.info(lang_1.lang.__('HTTP_TRIGGER_DELETED', { triggerName: tName }));
974
- }
975
- catch (err) {
976
- const errorCode = err?.code;
977
- if (errorCode === 'ResourceNotFound.TriggerName' || errorCode === 'ResourceNotFound') {
978
- logger_1.logger.warn(lang_1.lang.__('HTTP_TRIGGER_NOT_FOUND', { triggerName: tName }));
979
- }
980
- else {
981
- throw err;
982
- }
983
- }
984
- }
985
- // Reconcile custom domain
986
- const existingCustomDomain = existingInstances.find((i) => i.type === 'TENCENT_SCF_CUSTOM_DOMAIN');
987
- if (fn.domain && !existingCustomDomain) {
988
- logger_1.logger.info(lang_1.lang.__('CREATING_CUSTOM_DOMAIN', { domainName: fn.domain.domain_name }));
989
- await client.scf.createCustomDomain({
990
- Domain: fn.domain.domain_name,
991
- Protocol: fn.domain.protocol,
992
- EndpointsConfig: [
993
- {
994
- Namespace: 'default',
995
- FunctionName: fn.name,
996
- Qualifier: '$DEFAULT',
997
- PathMatch: '/*',
998
- },
999
- ],
1000
- ...(fn.domain.certificate_id
1001
- ? { CertConfig: { CertificateId: fn.domain.certificate_id } }
1002
- : {}),
1003
- });
1004
- logger_1.logger.info(lang_1.lang.__('CUSTOM_DOMAIN_CREATED', { domainName: fn.domain.domain_name }));
1005
- }
1006
- else if (!fn.domain && existingCustomDomain) {
1007
- logger_1.logger.info(lang_1.lang.__('DELETING_CUSTOM_DOMAIN', { domainName: existingCustomDomain.id }));
1008
- try {
1009
- await client.scf.deleteCustomDomain(existingCustomDomain.id);
1010
- logger_1.logger.info(lang_1.lang.__('CUSTOM_DOMAIN_DELETED', { domainName: existingCustomDomain.id }));
1011
- }
1012
- catch (err) {
1013
- const errorCode = err?.code;
1014
- if (errorCode === 'ResourceNotFound') {
1015
- logger_1.logger.warn(lang_1.lang.__('RESOURCE_NOT_FOUND_PROVIDER', {
1016
- resourceType: 'Custom Domain',
1017
- name: existingCustomDomain.id,
1018
- }));
1019
- }
1020
- else {
1021
- throw err;
1022
- }
1023
- }
1024
- }
1025
- else if (fn.domain && existingCustomDomain) {
1026
- const domainChanged = fn.domain.domain_name !== existingCustomDomain.id ||
1027
- fn.domain.protocol !== existingCustomDomain.protocol;
1028
- if (domainChanged) {
1029
- logger_1.logger.info(lang_1.lang.__('DELETING_CUSTOM_DOMAIN', { domainName: existingCustomDomain.id }));
1030
- await client.scf.deleteCustomDomain(existingCustomDomain.id);
989
+ const httpInstances = await (0, scfHttpTrigger_1.reconcileScfHttpTrigger)(context, fn, existingInstances, existingState?.definition ?? {}, client, options?.repairHttp);
990
+ const stateAfterHttp = (0, stateManager_1.setResource)(state, logicalId, {
991
+ ...(existingState ?? {
992
+ mode: 'managed',
993
+ region: context.region,
994
+ definition: existingDefinition,
995
+ lastUpdated: new Date().toISOString(),
996
+ }),
997
+ instances: [
998
+ ...(existingState?.instances ?? []).filter((instance) => instance.type !== scfHttpTrigger_1.SCF_HTTP_TRIGGER_TYPE),
999
+ ...httpInstances,
1000
+ ],
1001
+ });
1002
+ try {
1003
+ // Reconcile timer triggers against the provider (issue #258) — the live
1004
+ // GetFunction trigger list is the baseline so console edits converge.
1005
+ const timerInstances = await reconcileScfTimerTriggers(context, fn, existingInstances, client);
1006
+ // Reconcile custom domain
1007
+ const existingCustomDomain = existingInstances.find((i) => i.type === 'TENCENT_SCF_CUSTOM_DOMAIN');
1008
+ if (fn.domain && !existingCustomDomain) {
1031
1009
  logger_1.logger.info(lang_1.lang.__('CREATING_CUSTOM_DOMAIN', { domainName: fn.domain.domain_name }));
1032
1010
  await client.scf.createCustomDomain({
1033
1011
  Domain: fn.domain.domain_name,
@@ -1046,100 +1024,137 @@ const updateResource = async (context, fn, state, options) => {
1046
1024
  });
1047
1025
  logger_1.logger.info(lang_1.lang.__('CUSTOM_DOMAIN_CREATED', { domainName: fn.domain.domain_name }));
1048
1026
  }
1049
- }
1050
- // Refresh state from provider to get all attributes (including triggers)
1051
- const functionInfo = await client.scf.getFunction(fn.name);
1052
- if (!functionInfo) {
1053
- throw new Error(lang_1.lang.__('REFRESH_STATE_FUNCTION', { name: fn.name }));
1054
- }
1055
- const definition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam);
1056
- const sid = (0, common_1.buildSid)('tencent', 'scf', context.stage, fn.name);
1057
- const existingDependentInstances = existingInstances
1058
- .filter((i) => i.type !== undefined &&
1059
- i.type !== 'TENCENT_SCF_CUSTOM_DOMAIN' &&
1060
- !((clsTopicInstance || disableLog) &&
1061
- (i.type === types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC ||
1062
- i.type === types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET)))
1063
- .map((i) => {
1064
- const { type, id, sid, roleArn, external } = i;
1065
- return {
1066
- sid: sid ?? (0, common_1.buildSid)('tencent', 'scf-role', context.stage, id),
1067
- type,
1068
- id,
1069
- ...(roleArn ? { roleArn } : {}),
1070
- ...(external ? { external } : {}),
1071
- };
1072
- });
1073
- const newDependentInstances = [];
1074
- if (role && !hasCamRole && typeof newIamRole !== 'string') {
1075
- const newRoleSid = (0, common_1.buildSid)('tencent', 'scf-role', context.stage, role.roleName ?? '');
1076
- newDependentInstances.push({
1077
- sid: newRoleSid,
1078
- type: types_1.ResourceTypeEnum.TENCENT_SCF_ROLE,
1079
- id: role.roleName ?? '',
1080
- ...(role.arn ? { roleArn: role.arn } : {}),
1081
- });
1082
- }
1083
- if (fn.domain) {
1084
- const domainSid = (0, common_1.buildSid)('tencent', 'scf-custom-domain', context.stage, fn.domain.domain_name);
1085
- newDependentInstances.push({
1086
- sid: domainSid,
1087
- type: 'TENCENT_SCF_CUSTOM_DOMAIN',
1088
- id: fn.domain.domain_name,
1089
- protocol: fn.domain.protocol,
1027
+ else if (!fn.domain && existingCustomDomain) {
1028
+ logger_1.logger.info(lang_1.lang.__('DELETING_CUSTOM_DOMAIN', { domainName: existingCustomDomain.id }));
1029
+ try {
1030
+ await client.scf.deleteCustomDomain(existingCustomDomain.id);
1031
+ logger_1.logger.info(lang_1.lang.__('CUSTOM_DOMAIN_DELETED', { domainName: existingCustomDomain.id }));
1032
+ }
1033
+ catch (err) {
1034
+ const errorCode = err?.code;
1035
+ if (errorCode !== 'ResourceNotFound')
1036
+ throw err;
1037
+ logger_1.logger.warn(lang_1.lang.__('RESOURCE_NOT_FOUND_PROVIDER', {
1038
+ resourceType: 'Custom Domain',
1039
+ name: existingCustomDomain.id,
1040
+ }));
1041
+ }
1042
+ }
1043
+ else if (fn.domain && existingCustomDomain) {
1044
+ const domainChanged = options?.repairDomain ||
1045
+ !(0, common_1.attributesEqual)(existingState?.definition?.domain ?? {}, fn.domain) ||
1046
+ fn.domain.domain_name !== existingCustomDomain.id ||
1047
+ fn.domain.protocol !== existingCustomDomain.protocol;
1048
+ if (domainChanged) {
1049
+ logger_1.logger.info(lang_1.lang.__('DELETING_CUSTOM_DOMAIN', { domainName: existingCustomDomain.id }));
1050
+ await client.scf.deleteCustomDomain(existingCustomDomain.id);
1051
+ logger_1.logger.info(lang_1.lang.__('CREATING_CUSTOM_DOMAIN', { domainName: fn.domain.domain_name }));
1052
+ await client.scf.createCustomDomain({
1053
+ Domain: fn.domain.domain_name,
1054
+ Protocol: fn.domain.protocol,
1055
+ EndpointsConfig: [
1056
+ {
1057
+ Namespace: 'default',
1058
+ FunctionName: fn.name,
1059
+ Qualifier: '$DEFAULT',
1060
+ PathMatch: '/*',
1061
+ },
1062
+ ],
1063
+ ...(fn.domain.certificate_id
1064
+ ? { CertConfig: { CertificateId: fn.domain.certificate_id } }
1065
+ : {}),
1066
+ });
1067
+ logger_1.logger.info(lang_1.lang.__('CUSTOM_DOMAIN_CREATED', { domainName: fn.domain.domain_name }));
1068
+ }
1069
+ }
1070
+ // Refresh state from provider to get all attributes (including triggers)
1071
+ const functionInfo = await client.scf.getFunction(fn.name);
1072
+ if (!functionInfo) {
1073
+ throw new Error(lang_1.lang.__('REFRESH_STATE_FUNCTION', { name: fn.name }));
1074
+ }
1075
+ const definition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam, fn);
1076
+ const sid = (0, common_1.buildSid)('tencent', 'scf', context.stage, fn.name);
1077
+ const existingDependentInstances = existingInstances
1078
+ .filter((i) => !isScfFunctionInstance(i) &&
1079
+ i.type !== 'TENCENT_SCF_CUSTOM_DOMAIN' &&
1080
+ // timers are fully rebuilt from the reconcile result below
1081
+ i.type !== TIMER_TRIGGER_INSTANCE_TYPE &&
1082
+ i.type !== scfHttpTrigger_1.SCF_HTTP_TRIGGER_TYPE &&
1083
+ !((clsTopicInstance || disableLog) &&
1084
+ (i.type === types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC ||
1085
+ i.type === types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET)))
1086
+ .map((i) => {
1087
+ const { type, id, sid, roleArn, external } = i;
1088
+ return {
1089
+ sid: sid ?? (0, common_1.buildSid)('tencent', 'scf-role', context.stage, id),
1090
+ type,
1091
+ id,
1092
+ ...(roleArn ? { roleArn } : {}),
1093
+ ...(external ? { external } : {}),
1094
+ };
1090
1095
  });
1096
+ const newDependentInstances = [];
1097
+ if (role && !hasCamRole && typeof newIamRole !== 'string') {
1098
+ const newRoleSid = (0, common_1.buildSid)('tencent', 'scf-role', context.stage, role.roleName ?? '');
1099
+ newDependentInstances.push({
1100
+ sid: newRoleSid,
1101
+ type: types_1.ResourceTypeEnum.TENCENT_SCF_ROLE,
1102
+ id: role.roleName ?? '',
1103
+ ...(role.arn ? { roleArn: role.arn } : {}),
1104
+ });
1105
+ }
1106
+ if (fn.domain) {
1107
+ const domainSid = (0, common_1.buildSid)('tencent', 'scf-custom-domain', context.stage, fn.domain.domain_name);
1108
+ newDependentInstances.push({
1109
+ sid: domainSid,
1110
+ type: 'TENCENT_SCF_CUSTOM_DOMAIN',
1111
+ id: fn.domain.domain_name,
1112
+ protocol: fn.domain.protocol,
1113
+ });
1114
+ }
1115
+ if (clsTopicInstance) {
1116
+ newDependentInstances.push(clsTopicInstance);
1117
+ }
1118
+ const resourceState = {
1119
+ mode: 'managed',
1120
+ region: context.region,
1121
+ definition: (0, functionDefinition_1.withLifecycleIntent)((0, planCompare_1.mergeManagedIntent)(existingState?.definition ?? {}, definition), definition, ['http', 'domain', 'timers']),
1122
+ instances: [
1123
+ buildScfInstanceFromProvider(functionInfo, sid, httpInstances.map((instance) => instance.id)),
1124
+ ...existingDependentInstances,
1125
+ ...newDependentInstances,
1126
+ ...timerInstances,
1127
+ ...httpInstances,
1128
+ ],
1129
+ lastUpdated: new Date().toISOString(),
1130
+ };
1131
+ const stateWithShared = clsSharedInstance
1132
+ ? (0, stateManager_1.setSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY, clsSharedInstance)
1133
+ : sharedLogsetReleased
1134
+ ? (0, stateManager_1.removeSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY)
1135
+ : state;
1136
+ return (0, stateManager_1.setResource)(stateWithShared, logicalId, resourceState);
1091
1137
  }
1092
- if (clsTopicInstance) {
1093
- newDependentInstances.push(clsTopicInstance);
1138
+ catch (error) {
1139
+ if (error instanceof types_1.PartialResourceError)
1140
+ throw error;
1141
+ throw new types_1.PartialResourceError(stateAfterHttp, new Error(toErrorMessage(error)));
1094
1142
  }
1095
- const resourceState = {
1096
- mode: 'managed',
1097
- region: context.region,
1098
- definition,
1099
- instances: [
1100
- buildScfInstanceFromProvider(functionInfo, sid),
1101
- ...existingDependentInstances,
1102
- ...newDependentInstances,
1103
- ],
1104
- lastUpdated: new Date().toISOString(),
1105
- };
1106
- const stateWithShared = clsSharedInstance
1107
- ? (0, stateManager_1.setSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY, clsSharedInstance)
1108
- : sharedLogsetReleased
1109
- ? (0, stateManager_1.removeSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY)
1110
- : state;
1111
- return (0, stateManager_1.setResource)(stateWithShared, logicalId, resourceState);
1112
1143
  };
1113
1144
  exports.updateResource = updateResource;
1114
1145
  const deleteResource = async (context, functionName, logicalId, state) => {
1115
1146
  const existingState = (0, stateManager_1.getResource)(state, logicalId);
1116
1147
  const existingInstances = (existingState?.instances ?? []);
1117
1148
  const client = (0, tencentClient_1.createTencentClient)(context);
1118
- // Delete HTTP trigger before deleting function
1119
- const fnInstance = existingInstances.find((i) => i.type === undefined);
1120
- const httpTrigger = fnInstance?.triggers?.find((t) => t.type === 'http');
1121
- if (httpTrigger) {
1122
- try {
1123
- const triggerName = httpTrigger.triggerName;
1124
- logger_1.logger.info(lang_1.lang.__('DELETING_HTTP_TRIGGER', { triggerName, functionName }));
1125
- await client.scf.deleteTrigger({
1126
- FunctionName: functionName,
1127
- TriggerName: triggerName,
1128
- Type: 'http',
1129
- });
1130
- logger_1.logger.info(lang_1.lang.__('HTTP_TRIGGER_DELETED', { triggerName }));
1131
- }
1132
- catch (err) {
1133
- const errorCode = err?.code;
1134
- if (errorCode === 'ResourceNotFound.TriggerName' || errorCode === 'ResourceNotFound') {
1135
- logger_1.logger.warn(lang_1.lang.__('HTTP_TRIGGER_NOT_FOUND', {
1136
- triggerName: httpTrigger.triggerName,
1137
- }));
1138
- }
1139
- else {
1140
- throw err;
1141
- }
1142
- }
1149
+ const ownedHttp = (0, scfHttpTrigger_1.ownedScfHttpTrigger)(existingInstances, existingState?.definition ?? {});
1150
+ if (ownedHttp) {
1151
+ await (0, scfHttpTrigger_1.reconcileScfHttpTrigger)(context, { name: functionName }, existingInstances, existingState?.definition ?? {}, client);
1152
+ }
1153
+ // Delete timer triggers before deleting function
1154
+ const timerTriggerInstances = existingInstances.filter((i) => i.type === TIMER_TRIGGER_INSTANCE_TYPE);
1155
+ for (const timerInstance of timerTriggerInstances) {
1156
+ logger_1.logger.info(lang_1.lang.__('DELETING_TIMER_TRIGGER', { triggerName: timerInstance.id, functionName }));
1157
+ await deleteScfTimerTrigger(functionName, timerInstance.id, client);
1143
1158
  }
1144
1159
  // Delete custom domain before deleting function
1145
1160
  const existingCustomDomain = existingInstances.find((i) => i.type === 'TENCENT_SCF_CUSTOM_DOMAIN');
@@ -1162,7 +1177,7 @@ const deleteResource = async (context, functionName, logicalId, state) => {
1162
1177
  }
1163
1178
  }
1164
1179
  }
1165
- const hasFunction = existingInstances.some((i) => i.type === undefined);
1180
+ const hasFunction = existingInstances.some((i) => isScfFunctionInstance(i));
1166
1181
  if (hasFunction) {
1167
1182
  try {
1168
1183
  await client.scf.deleteFunction(functionName);
@@ -1177,8 +1192,11 @@ const deleteResource = async (context, functionName, logicalId, state) => {
1177
1192
  }
1178
1193
  }
1179
1194
  }
1180
- const dependentInstances = existingInstances.filter((i) => i.type !== undefined &&
1181
- i.type !== 'TENCENT_SCF_CUSTOM_DOMAIN');
1195
+ const dependentInstances = existingInstances.filter((i) => !isScfFunctionInstance(i) &&
1196
+ i.type !== 'TENCENT_SCF_CUSTOM_DOMAIN' &&
1197
+ // timers were deleted explicitly above
1198
+ i.type !== TIMER_TRIGGER_INSTANCE_TYPE &&
1199
+ i.type !== scfHttpTrigger_1.SCF_HTTP_TRIGGER_TYPE);
1182
1200
  if (dependentInstances.length > 0) {
1183
1201
  await deleteDependentResources(context, dependentInstances);
1184
1202
  }