@geek-fun/serverlessinsight 0.8.6 → 0.8.8

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 (76) 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 +94 -0
  4. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +47 -27
  5. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +1 -1
  6. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +35 -73
  7. package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +148 -0
  8. package/AGENTS.md +29 -0
  9. package/dist/package.json +1 -1
  10. package/dist/src/commands/deploy.js +1 -0
  11. package/dist/src/commands/index.js +6 -2
  12. package/dist/src/commands/plan.js +1 -0
  13. package/dist/src/common/aliyunClient/ecsOperations.js +70 -1
  14. package/dist/src/common/aliyunClient/nasOperations.js +21 -0
  15. package/dist/src/common/aliyunClient/ramOperations.js +116 -15
  16. package/dist/src/common/aliyunClient/slsOperations.js +16 -3
  17. package/dist/src/common/concurrency.js +30 -0
  18. package/dist/src/common/context.js +3 -0
  19. package/dist/src/common/dependencyGraph/graph.js +5 -0
  20. package/dist/src/common/iacHelper.js +3 -1
  21. package/dist/src/common/iamStatements.js +21 -1
  22. package/dist/src/common/index.js +5 -0
  23. package/dist/src/common/nameBuilder.js +12 -1
  24. package/dist/src/common/planCompare.js +62 -0
  25. package/dist/src/common/planFormatter.js +15 -3
  26. package/dist/src/common/refreshCache.js +32 -0
  27. package/dist/src/common/refreshPlanner.js +105 -0
  28. package/dist/src/common/tencentClient/camOperations.js +24 -12
  29. package/dist/src/common/tencentClient/clsOperations.js +27 -3
  30. package/dist/src/common/tencentClient/tdsqlcOperations.js +22 -0
  31. package/dist/src/common/throttleRetry.js +56 -0
  32. package/dist/src/common/volcengineClient/iamOperations.js +15 -11
  33. package/dist/src/common/volcengineClient/tlsOperations.js +12 -4
  34. package/dist/src/lang/en.js +23 -0
  35. package/dist/src/lang/zh-CN.js +22 -0
  36. package/dist/src/stack/aliyunStack/apigwExecutor.js +5 -5
  37. package/dist/src/stack/aliyunStack/apigwPlanner.js +75 -7
  38. package/dist/src/stack/aliyunStack/apigwResource.js +10 -8
  39. package/dist/src/stack/aliyunStack/apigwTypes.js +86 -5
  40. package/dist/src/stack/aliyunStack/databasePlanner.js +26 -62
  41. package/dist/src/stack/aliyunStack/deployer.js +33 -3
  42. package/dist/src/stack/aliyunStack/esServerlessTypes.js +50 -1
  43. package/dist/src/stack/aliyunStack/fc3Executor.js +5 -3
  44. package/dist/src/stack/aliyunStack/fc3Planner.js +299 -12
  45. package/dist/src/stack/aliyunStack/fc3Resource.js +358 -34
  46. package/dist/src/stack/aliyunStack/fc3Types.js +61 -1
  47. package/dist/src/stack/aliyunStack/ossPlanner.js +21 -63
  48. package/dist/src/stack/aliyunStack/ossTypes.js +25 -1
  49. package/dist/src/stack/aliyunStack/rdsTypes.js +38 -1
  50. package/dist/src/stack/aliyunStack/sharedLogProject.js +22 -5
  51. package/dist/src/stack/aliyunStack/tablestorePlanner.js +20 -74
  52. package/dist/src/stack/aliyunStack/tablestoreTypes.js +33 -1
  53. package/dist/src/stack/scfStack/cosPlanner.js +30 -50
  54. package/dist/src/stack/scfStack/cosTypes.js +29 -1
  55. package/dist/src/stack/scfStack/esServerlessPlanner.js +21 -54
  56. package/dist/src/stack/scfStack/esServerlessTypes.js +13 -1
  57. package/dist/src/stack/scfStack/scfExecutor.js +5 -3
  58. package/dist/src/stack/scfStack/scfPlanner.js +72 -51
  59. package/dist/src/stack/scfStack/scfResource.js +109 -12
  60. package/dist/src/stack/scfStack/scfTypes.js +35 -1
  61. package/dist/src/stack/scfStack/sharedLogset.js +12 -3
  62. package/dist/src/stack/scfStack/tdsqlcPlanner.js +37 -55
  63. package/dist/src/stack/scfStack/tdsqlcTypes.js +21 -1
  64. package/dist/src/stack/volcengineStack/apigwPlanner.js +59 -8
  65. package/dist/src/stack/volcengineStack/apigwResource.js +52 -30
  66. package/dist/src/stack/volcengineStack/apigwTypes.js +44 -8
  67. package/dist/src/stack/volcengineStack/sharedLogProject.js +9 -3
  68. package/dist/src/stack/volcengineStack/tosPlanner.js +28 -55
  69. package/dist/src/stack/volcengineStack/tosTypes.js +18 -1
  70. package/dist/src/stack/volcengineStack/vefaasExecutor.js +5 -3
  71. package/dist/src/stack/volcengineStack/vefaasPlanner.js +119 -10
  72. package/dist/src/stack/volcengineStack/vefaasResource.js +112 -13
  73. package/dist/src/stack/volcengineStack/vefaasTypes.js +61 -7
  74. package/dist/src/validator/semanticValidation.js +41 -0
  75. package/dist/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +1 -1
@@ -2,9 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateApigwPlan = void 0;
4
4
  const volcengineClient_1 = require("../../common/volcengineClient");
5
+ const refreshCache_1 = require("../../common/refreshCache");
6
+ const concurrency_1 = require("../../common/concurrency");
5
7
  const apigwTypes_1 = require("./apigwTypes");
6
8
  const stateManager_1 = require("../../common/stateManager");
7
9
  const hashUtils_1 = require("../../common/hashUtils");
10
+ const common_1 = require("../../common");
11
+ const lang_1 = require("../../lang");
8
12
  const ownershipTag_1 = require("../ownershipTag");
9
13
  const planEventDeletion = (logicalId, definition) => ({
10
14
  logicalId,
@@ -21,18 +25,22 @@ const generateApigwPlan = async (context, state, events, serviceName) => {
21
25
  return { items };
22
26
  }
23
27
  const desiredLogicalIds = new Set(events.map((e) => `events.${e.key}`));
24
- const eventItems = await Promise.all(events.map(async (event) => {
28
+ const eventItems = await (0, concurrency_1.mapWithConcurrency)(events, concurrency_1.PLAN_READ_CONCURRENCY, async (event) => {
25
29
  const logicalId = `events.${event.key}`;
26
30
  const currentState = (0, stateManager_1.getResource)(state, logicalId);
27
31
  const client = (0, volcengineClient_1.createVolcengineClient)(context);
28
- const desiredDefinition = (0, apigwTypes_1.buildEventResourceDefinition)(event);
32
+ // Stored-first: an existing event keeps its recorded topic name so the
33
+ // desired definition never phantom-drifts against legacy state.
34
+ const storedTlsTopic = currentState?.instances?.find((i) => i.type === 'VOLCENGINE_TLS_TOPIC');
35
+ const storedTopicName = storedTlsTopic?.id?.split('/')[1];
36
+ const desiredDefinition = (0, apigwTypes_1.buildEventResourceDefinition)(event, storedTopicName);
29
37
  if (!currentState || currentState.status === 'tainted') {
30
38
  // No usable local state: probe the provider before planning create.
31
39
  // If a same-named gateway already exists WITHOUT our ownership tag it
32
40
  // may belong to another project — fail fast in the plan instead of
33
41
  // letting the executor discover it mid-deploy. The serverless gateway
34
42
  // is account-scoped, so only refuse when the NAMED one is foreign.
35
- const remoteGateway = await client.apigw.findGatewayByName((0, apigwTypes_1.buildGatewayName)(serviceName, context.stage));
43
+ const remoteGateway = await (0, refreshCache_1.cachedRefreshRead)(context, `apigw.findGatewayByName:${serviceName}:${context.stage}`, () => client.apigw.findGatewayByName((0, apigwTypes_1.buildGatewayName)(serviceName, context.stage)));
36
44
  if (remoteGateway?.gatewayId && !(0, ownershipTag_1.isOwnedByStack)(context, logicalId, remoteGateway.tags)) {
37
45
  throw new Error(`API Gateway ${(0, apigwTypes_1.buildGatewayName)(serviceName, context.stage)} already exists in provider but is not owned by this stack (missing ${ownershipTag_1.OWNERSHIP_TAG_KEY} tag). Refusing to create — resolve manually.`);
38
46
  }
@@ -44,9 +52,14 @@ const generateApigwPlan = async (context, state, events, serviceName) => {
44
52
  };
45
53
  }
46
54
  const serviceInstance = currentState.instances.find((i) => i.type === 'VOLCENGINE_APIGW_SERVICE');
47
- if (serviceInstance) {
48
- const remoteService = await client.apigw.getService(serviceInstance.id).catch(() => null);
49
- if (!remoteService) {
55
+ // --no-refresh: no live reads, no drift claims — intent-diff only.
56
+ const refreshEnabled = context.refresh !== false;
57
+ let cloudService = null;
58
+ if (refreshEnabled && serviceInstance) {
59
+ // Keep the not-found swallow OUTSIDE the cached read: a cached rejection
60
+ // evicts its key so the next plan pass retries against the provider.
61
+ cloudService = await (0, refreshCache_1.cachedRefreshRead)(context, `apigw.getService:${serviceInstance.id}`, () => client.apigw.getService(serviceInstance.id)).catch(() => null);
62
+ if (!cloudService) {
50
63
  return {
51
64
  logicalId,
52
65
  action: 'create',
@@ -58,16 +71,54 @@ const generateApigwPlan = async (context, state, events, serviceName) => {
58
71
  }
59
72
  const currentDefinition = currentState.definition || {};
60
73
  const definitionChanged = !(0, hashUtils_1.attributesEqual)(currentDefinition, desiredDefinition);
61
- if (definitionChanged) {
74
+ // Issue #234 phase 2: live trigger drift. The service record alone can be
75
+ // untouched while console edits reroute a trigger (method/path/upstream).
76
+ // Routes and upstreams are matched by the derived names the executor
77
+ // writes; a probe failure stays noop (best-effort detection) instead of
78
+ // fabricating drift from a transient read error.
79
+ let triggersDiffer = false;
80
+ if (refreshEnabled && !definitionChanged && serviceInstance && event.triggers.length > 0) {
81
+ try {
82
+ const desiredTriggers = (0, apigwTypes_1.buildDesiredTriggerMap)(event, context.stage);
83
+ const cloudRoutes = await (0, refreshCache_1.cachedRefreshRead)(context, `apigw.listRoutesByService:${serviceInstance.id}`, () => client.apigw.listRoutesByService(serviceInstance.id));
84
+ const matched = (cloudRoutes ?? []).filter((route) => route.routeName && desiredTriggers.has(route.routeName));
85
+ const upstreamNameById = new Map();
86
+ for (const route of matched) {
87
+ const upstreamId = route.upstreamIds?.[0];
88
+ if (upstreamId && !upstreamNameById.has(upstreamId)) {
89
+ const upstream = await (0, refreshCache_1.cachedRefreshRead)(context, `apigw.getUpstream:${upstreamId}`, () => client.apigw.getUpstream(upstreamId));
90
+ upstreamNameById.set(upstreamId, upstream?.upstreamName);
91
+ }
92
+ }
93
+ triggersDiffer = (0, apigwTypes_1.cloudTriggerDiffers)(matched, upstreamNameById, desiredTriggers);
94
+ }
95
+ catch (error) {
96
+ common_1.logger.warn(lang_1.lang.__('PLAN_EVENT_TRIGGER_PROBE_FAILED', {
97
+ eventName: event.name,
98
+ error: String(error),
99
+ }));
100
+ }
101
+ }
102
+ // Issue #234 phase 3: declared custom-domain drift — the service read
103
+ // already carries customDomains, so this costs no extra API call.
104
+ const desiredDomainName = desiredDefinition.domain
105
+ ?.domainName;
106
+ let domainDiffers = false;
107
+ if (refreshEnabled && desiredDomainName) {
108
+ const cloudDomains = cloudService?.customDomains ?? [];
109
+ domainDiffers = !cloudDomains.some((d) => d.domain === desiredDomainName);
110
+ }
111
+ if (definitionChanged || triggersDiffer || domainDiffers) {
62
112
  return {
63
113
  logicalId,
64
114
  action: 'update',
65
115
  resourceType: 'VOLCENGINE_APIGW',
66
116
  changes: { before: currentDefinition, after: desiredDefinition },
117
+ drifted: true,
67
118
  };
68
119
  }
69
120
  return { logicalId, action: 'noop', resourceType: 'VOLCENGINE_APIGW' };
70
- }));
121
+ });
71
122
  const allStates = (0, stateManager_1.getAllResources)(state);
72
123
  const deletionItems = Object.entries(allStates)
73
124
  .filter(([logicalId]) => {
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deleteApigwResource = exports.updateApigwResource = exports.createApigwResource = void 0;
4
4
  const types_1 = require("../../types");
5
5
  const volcengineClient_1 = require("../../common/volcengineClient");
6
+ const common_1 = require("../../common");
6
7
  const apigwTypes_1 = require("./apigwTypes");
7
8
  const stateManager_1 = require("../../common/stateManager");
8
- const common_1 = require("../../common");
9
+ const common_2 = require("../../common");
9
10
  const logger_1 = require("../../common/logger");
10
11
  const lang_1 = require("../../lang");
11
12
  const ownershipTag_1 = require("../ownershipTag");
@@ -13,7 +14,7 @@ const alreadyExists_1 = require("../alreadyExists");
13
14
  const sharedLogProject_1 = require("./sharedLogProject");
14
15
  const buildGatewayInstance = (info, stage) => ({
15
16
  type: 'VOLCENGINE_APIGW_GATEWAY',
16
- sid: (0, common_1.buildSid)('volcengine', 'apigw', stage, info.gatewayId ?? ''),
17
+ sid: (0, common_2.buildSid)('volcengine', 'apigw', stage, info.gatewayId ?? ''),
17
18
  id: info.gatewayId ?? '',
18
19
  gatewayId: info.gatewayId ?? null,
19
20
  gatewayName: info.gatewayName ?? null,
@@ -32,7 +33,11 @@ const buildGatewayInstance = (info, stage) => ({
32
33
  logSpec: info.logSpec ?? null,
33
34
  resourceSpec: info.resourceSpec ?? null,
34
35
  });
35
- const ensureApigwLogResources = async (context, existingInstances, state, logicalId) => {
36
+ const resolveTlsTopicNameFromInstances = (instances) => {
37
+ const tlsTopic = instances.find((i) => i.type === 'VOLCENGINE_TLS_TOPIC');
38
+ return tlsTopic?.id?.split('/')[1];
39
+ };
40
+ const ensureApigwLogResources = async (context, existingInstances, state, logicalId, eventKey) => {
36
41
  const client = (0, volcengineClient_1.createVolcengineClient)(context);
37
42
  const tlsProjectInstance = existingInstances.find((i) => i.type === 'VOLCENGINE_TLS_PROJECT');
38
43
  const tlsTopicInstance = existingInstances.find((i) => i.type === 'VOLCENGINE_TLS_TOPIC');
@@ -47,7 +52,7 @@ const ensureApigwLogResources = async (context, existingInstances, state, logica
47
52
  // Shared app-scoped TLS project (#214) with a per-event topic nested under it.
48
53
  const shared = await (0, sharedLogProject_1.ensureSharedLogProject)(context, client, state);
49
54
  const sharedInstance = (0, sharedLogProject_1.buildSharedProjectResourceState)(context, shared);
50
- const topicName = `${context.service}-${context.stage}-apigw-logs`;
55
+ const topicName = (0, sharedLogProject_1.buildApigwLogTopicName)(context.service, context.stage, eventKey);
51
56
  const topic = await (0, sharedLogProject_1.ensureOwnedTopic)(context, client, {
52
57
  projectName: shared.projectName,
53
58
  topicName,
@@ -59,14 +64,14 @@ const ensureApigwLogResources = async (context, existingInstances, state, logica
59
64
  instances: [
60
65
  {
61
66
  type: 'VOLCENGINE_TLS_TOPIC',
62
- sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, `${shared.projectName}/${topic.topicName}`),
67
+ sid: (0, common_2.buildSid)('volcengine', 'tls', context.stage, `${shared.projectName}/${topic.topicName}`),
63
68
  id: `${shared.projectName}/${topic.topicName}`,
64
69
  projectId: shared.projectId,
65
70
  topicId: topic.topicId,
66
71
  },
67
72
  {
68
73
  type: 'VOLCENGINE_TLS_INDEX',
69
- sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, `${shared.projectName}/${topic.topicName}/index`),
74
+ sid: (0, common_2.buildSid)('volcengine', 'tls', context.stage, `${shared.projectName}/${topic.topicName}/index`),
70
75
  id: `${shared.projectName}/${topic.topicName}/index`,
71
76
  },
72
77
  ],
@@ -75,7 +80,7 @@ const ensureApigwLogResources = async (context, existingInstances, state, logica
75
80
  };
76
81
  const buildServiceInstance = (serviceId, serviceName, gatewayId, stage, info) => ({
77
82
  type: 'VOLCENGINE_APIGW_SERVICE',
78
- sid: (0, common_1.buildSid)('volcengine', 'apigw', stage, serviceId),
83
+ sid: (0, common_2.buildSid)('volcengine', 'apigw', stage, serviceId),
79
84
  id: serviceId,
80
85
  serviceId,
81
86
  serviceName,
@@ -92,7 +97,7 @@ const buildServiceInstance = (serviceId, serviceName, gatewayId, stage, info) =>
92
97
  });
93
98
  const buildUpstreamInstance = (upstreamId, upstreamName, gatewayId, functionId, stage, info) => ({
94
99
  type: 'VOLCENGINE_APIGW_UPSTREAM',
95
- sid: (0, common_1.buildSid)('volcengine', 'apigw', stage, upstreamId),
100
+ sid: (0, common_2.buildSid)('volcengine', 'apigw', stage, upstreamId),
96
101
  id: upstreamId,
97
102
  upstreamId,
98
103
  upstreamName,
@@ -109,7 +114,7 @@ const buildUpstreamInstance = (upstreamId, upstreamName, gatewayId, functionId,
109
114
  });
110
115
  const buildRouteInstance = (routeId, routeName, serviceId, stage, info) => ({
111
116
  type: 'VOLCENGINE_APIGW_ROUTE',
112
- sid: (0, common_1.buildSid)('volcengine', 'apigw', stage, routeId),
117
+ sid: (0, common_2.buildSid)('volcengine', 'apigw', stage, routeId),
113
118
  id: routeId,
114
119
  routeId,
115
120
  routeName,
@@ -124,18 +129,36 @@ const buildRouteInstance = (routeId, routeName, serviceId, stage, info) => ({
124
129
  ...(info?.upstreamList && { upstreamList: info.upstreamList }),
125
130
  });
126
131
  /**
127
- * Resolve the veFaaS function Id for a ${functions.xxx} backend ref from state.
128
- * The function resource must already be deployed (functions deploy before events).
132
+ * Resolve the veFaaS function Id for a backend ref. Template refs carry the
133
+ * key directly; bare values are the deployed function name — mapped to the
134
+ * template key first, then resolved from state. A bare name matching no
135
+ * template function is an EXTERNAL deployed function: its Id is resolved
136
+ * from the provider by name (same account/region, function must exist).
129
137
  */
130
- const resolveFunctionIdFromState = (state, backendRef) => {
131
- const fnKey = (0, apigwTypes_1.resolveFunctionKey)(backendRef);
132
- const fnState = (0, stateManager_1.getResource)(state, `functions.${fnKey}`);
133
- const instance = fnState?.instances?.find((i) => i.type === 'VOLCENGINE_VEFAAS_FUNCTION');
134
- const functionId = instance?.functionId;
135
- if (!functionId) {
136
- throw new Error(`Cannot resolve veFaaS function Id for backend ${backendRef} (functions.${fnKey} has no functionId in state). Deploy the function first.`);
138
+ const resolveFunctionIdFromState = async (state, backendRef, client) => {
139
+ const context = (0, common_1.getContext)();
140
+ const refMatch = /^\$\{functions\.([\w.]+)\}$/.exec(String(backendRef ?? ''));
141
+ const templateKey = refMatch?.[1];
142
+ const fnKey = templateKey ?? context.iac?.functions?.find((f) => f.name === String(backendRef))?.key;
143
+ if (fnKey) {
144
+ const fnState = (0, stateManager_1.getResource)(state, `functions.${fnKey}`);
145
+ const instance = fnState?.instances?.find((i) => i.type === 'VOLCENGINE_VEFAAS_FUNCTION');
146
+ const functionId = instance?.functionId;
147
+ if (functionId) {
148
+ return functionId;
149
+ }
150
+ if (templateKey) {
151
+ // Template function with no state Id: deploy failed or state is stale —
152
+ // do not mask it by adopting a same-named foreign function.
153
+ throw new Error(`Cannot resolve veFaaS function Id for backend ${backendRef} (functions.${fnKey} has no functionId in state). Deploy the function first.`);
154
+ }
155
+ }
156
+ const external = await client.vefaas.getFunction(String(backendRef ?? ''));
157
+ const externalId = external?.functionId;
158
+ if (!externalId) {
159
+ throw new Error(lang_1.lang.__('EXTERNAL_FUNCTION_NOT_FOUND_VOLCENGINE', { backendRef: String(backendRef ?? '') }));
137
160
  }
138
- return functionId;
161
+ return externalId;
139
162
  };
140
163
  const createApigwResource = async (context, event, serviceName, state) => {
141
164
  const logicalId = `events.${event.key}`;
@@ -186,7 +209,7 @@ const createApigwResource = async (context, event, serviceName, state) => {
186
209
  if (event.log) {
187
210
  const existingEventState = (0, stateManager_1.getResource)(state, logicalId);
188
211
  const existingInstances = existingEventState?.instances ?? [];
189
- const logResources = await ensureApigwLogResources(context, existingInstances, state, logicalId);
212
+ const logResources = await ensureApigwLogResources(context, existingInstances, state, logicalId, event.key);
190
213
  if (logResources.sharedInstance) {
191
214
  state = (0, stateManager_1.setSharedResource)(state, context.stage, 'logs.project', logResources.sharedInstance);
192
215
  }
@@ -214,7 +237,7 @@ const createApigwResource = async (context, event, serviceName, state) => {
214
237
  const partialResourceState = {
215
238
  mode: 'managed',
216
239
  region: context.region,
217
- definition: (0, apigwTypes_1.buildEventResourceDefinition)(event),
240
+ definition: (0, apigwTypes_1.buildEventResourceDefinition)(event, resolveTlsTopicNameFromInstances(instances)),
218
241
  instances,
219
242
  status: 'tainted',
220
243
  lastUpdated: new Date().toISOString(),
@@ -228,8 +251,8 @@ const createApigwResource = async (context, event, serviceName, state) => {
228
251
  const fnKey = (0, apigwTypes_1.resolveFunctionKey)(backendRef);
229
252
  let upstreamId = upstreamByFunction.get(fnKey);
230
253
  if (!upstreamId) {
231
- const functionId = resolveFunctionIdFromState(state, backendRef);
232
- const upstreamName = `${event.name}-${context.stage}-upstream-${fnKey.replace(/_/g, '-')}`;
254
+ const functionId = await resolveFunctionIdFromState(state, backendRef, client);
255
+ const upstreamName = (0, apigwTypes_1.buildUpstreamName)(event, backendRef, context.stage);
233
256
  const existingUpstream = await client.apigw.findUpstreamByName(gatewayId, upstreamName);
234
257
  if (existingUpstream?.upstreamId) {
235
258
  upstreamId = existingUpstream.upstreamId;
@@ -268,7 +291,7 @@ const createApigwResource = async (context, event, serviceName, state) => {
268
291
  });
269
292
  instances.push({
270
293
  type: 'VOLCENGINE_APIGW_DOMAIN',
271
- sid: (0, common_1.buildSid)('volcengine', 'apigw', context.stage, domainId || domainName),
294
+ sid: (0, common_2.buildSid)('volcengine', 'apigw', context.stage, domainId || domainName),
272
295
  id: domainId || domainName,
273
296
  domainName,
274
297
  serviceId,
@@ -284,7 +307,7 @@ const createApigwResource = async (context, event, serviceName, state) => {
284
307
  const finalResourceState = {
285
308
  mode: 'managed',
286
309
  region: context.region,
287
- definition: (0, apigwTypes_1.buildEventResourceDefinition)(event),
310
+ definition: (0, apigwTypes_1.buildEventResourceDefinition)(event, resolveTlsTopicNameFromInstances(instances)),
288
311
  instances,
289
312
  status: 'ready',
290
313
  lastUpdated: new Date().toISOString(),
@@ -311,7 +334,7 @@ const updateApigwResource = async (context, event, serviceName, state) => {
311
334
  i.type === 'VOLCENGINE_APIGW_UPSTREAM');
312
335
  try {
313
336
  if (event.log) {
314
- const logResources = await ensureApigwLogResources(context, existingState.instances, state, `events.${event.key}`);
337
+ const logResources = await ensureApigwLogResources(context, existingState.instances, state, `events.${event.key}`, event.key);
315
338
  if (logResources.sharedInstance) {
316
339
  state = (0, stateManager_1.setSharedResource)(state, context.stage, 'logs.project', logResources.sharedInstance);
317
340
  }
@@ -350,11 +373,10 @@ const updateApigwResource = async (context, event, serviceName, state) => {
350
373
  }
351
374
  for (const trigger of event.triggers) {
352
375
  const backendRef = String(trigger.backend);
353
- const fnKey = (0, apigwTypes_1.resolveFunctionKey)(backendRef);
354
- const functionId = resolveFunctionIdFromState(state, backendRef);
376
+ const functionId = await resolveFunctionIdFromState(state, backendRef, client);
355
377
  let upstreamId = upstreamByFunction.get(functionId);
356
378
  if (!upstreamId) {
357
- const upstreamName = `${event.name}-${context.stage}-upstream-${fnKey.replace(/_/g, '-')}`;
379
+ const upstreamName = (0, apigwTypes_1.buildUpstreamName)(event, backendRef, context.stage);
358
380
  const existing = await client.apigw.findUpstreamByName(serviceInstance.gatewayId, upstreamName);
359
381
  if (existing?.upstreamId) {
360
382
  upstreamId = existing.upstreamId;
@@ -396,7 +418,7 @@ const updateApigwResource = async (context, event, serviceName, state) => {
396
418
  const resourceState = {
397
419
  mode: 'managed',
398
420
  region: context.region,
399
- definition: (0, apigwTypes_1.buildEventResourceDefinition)(event),
421
+ definition: (0, apigwTypes_1.buildEventResourceDefinition)(event, resolveTlsTopicNameFromInstances(instances)),
400
422
  instances,
401
423
  lastUpdated: new Date().toISOString(),
402
424
  };
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveFunctionReference = exports.buildEventResourceDefinition = exports.extractEventDomainDefinition = exports.triggerToApigwRouteConfig = exports.triggerToApigwUpstreamConfig = exports.eventToApigwServiceConfig = exports.eventToApigwGatewayConfig = exports.resolveFunctionKey = exports.buildRouteName = exports.buildUpstreamName = exports.buildServiceName = exports.buildGatewayName = void 0;
3
+ exports.resolveFunctionReference = exports.cloudTriggerDiffers = exports.buildDesiredTriggerMap = exports.buildEventResourceDefinition = exports.extractEventDomainDefinition = exports.triggerToApigwRouteConfig = exports.triggerToApigwUpstreamConfig = exports.eventToApigwServiceConfig = exports.eventToApigwGatewayConfig = exports.resolveFunctionKey = exports.buildRouteName = exports.buildUpstreamName = exports.buildServiceName = exports.buildGatewayName = void 0;
4
4
  const common_1 = require("../../common");
5
5
  const lang_1 = require("../../lang");
6
+ const ownershipTag_1 = require("../ownershipTag");
7
+ const sharedLogProject_1 = require("./sharedLogProject");
6
8
  /**
7
9
  * Gateway instance name — reused for find-by-name adoption across deploys.
8
10
  */
@@ -20,6 +22,7 @@ exports.buildRouteName = buildRouteName;
20
22
  /**
21
23
  * Resolves a function reference like ${functions.xxx} to the actual function name
22
24
  */
25
+ const FUNCTION_REF_PATTERN = /^\$\{functions\.[\w.]+\}$/;
23
26
  const resolveFunctionReference = (backendRef) => {
24
27
  const context = (0, common_1.getContext)();
25
28
  if (!context.iac) {
@@ -28,7 +31,14 @@ const resolveFunctionReference = (backendRef) => {
28
31
  }
29
32
  const functionDef = (0, common_1.getIacDefinition)(context.iac, backendRef);
30
33
  if (!functionDef || !(0, common_1.isFunctionDomain)(functionDef)) {
31
- common_1.logger.warn(lang_1.lang.__('FUNCTION_REF_NOT_RESOLVED', { backendRef }));
34
+ // Dangling template refs fail semantic validation; a bare value that matches
35
+ // no template function is an external deployed function name (issue #227).
36
+ if (FUNCTION_REF_PATTERN.test(backendRef)) {
37
+ common_1.logger.warn(lang_1.lang.__('FUNCTION_REF_NOT_RESOLVED', { backendRef }));
38
+ }
39
+ else {
40
+ common_1.logger.info(lang_1.lang.__('EXTERNAL_FUNCTION_BACKEND', { backendRef }));
41
+ }
32
42
  return backendRef;
33
43
  }
34
44
  return functionDef.name;
@@ -50,7 +60,7 @@ const eventToApigwGatewayConfig = (event, serviceName, stage, ownershipValue) =>
50
60
  network: { vpcId: event.network.vpc_id, subnetIds: event.network.subnet_ids },
51
61
  }),
52
62
  logConfig: event.log ? { enable: true, projectId: '', topicId: '' } : undefined,
53
- Tags: [{ Key: 'si-owned-by', Value: ownershipValue }],
63
+ Tags: [{ Key: ownershipTag_1.OWNERSHIP_TAG_KEY, Value: ownershipValue }],
54
64
  };
55
65
  };
56
66
  exports.eventToApigwGatewayConfig = eventToApigwGatewayConfig;
@@ -83,7 +93,7 @@ const triggerToApigwRouteConfig = (event, trigger, serviceId, upstreamId) => {
83
93
  };
84
94
  };
85
95
  exports.triggerToApigwRouteConfig = triggerToApigwRouteConfig;
86
- const extractEventDomainDefinition = (event) => {
96
+ const extractEventDomainDefinition = (event, logTopicOverride) => {
87
97
  const context = (0, common_1.getContext)();
88
98
  return {
89
99
  gatewayName: event.name,
@@ -94,8 +104,9 @@ const extractEventDomainDefinition = (event) => {
94
104
  ...(event.log && context
95
105
  ? {
96
106
  logConfig: {
97
- project: `${context.app}-${context.stage}-tls`,
98
- topic: `${context.service}-${context.stage}-apigw-logs`,
107
+ project: (0, sharedLogProject_1.buildSharedProjectName)(context.app, context.stage),
108
+ topic: logTopicOverride ??
109
+ (0, sharedLogProject_1.buildApigwLogTopicName)(context.service, context.stage, String(event.key)),
99
110
  },
100
111
  }
101
112
  : {}),
@@ -113,7 +124,32 @@ const extractEventDomainDefinition = (event) => {
113
124
  };
114
125
  };
115
126
  exports.extractEventDomainDefinition = extractEventDomainDefinition;
116
- const buildEventResourceDefinition = (event) => {
117
- return (0, exports.extractEventDomainDefinition)(event);
127
+ const buildEventResourceDefinition = (event, logTopicOverride) => {
128
+ return (0, exports.extractEventDomainDefinition)(event, logTopicOverride);
118
129
  };
119
130
  exports.buildEventResourceDefinition = buildEventResourceDefinition;
131
+ const buildDesiredTriggerMap = (event, stage) => new Map(event.triggers.map((trigger) => {
132
+ const method = String(trigger.method);
133
+ const path = String(trigger.path);
134
+ return [
135
+ (0, exports.buildRouteName)(event, method, path),
136
+ { method, path, upstreamName: (0, exports.buildUpstreamName)(event, String(trigger.backend), stage) },
137
+ ];
138
+ }));
139
+ exports.buildDesiredTriggerMap = buildDesiredTriggerMap;
140
+ const cloudTriggerDiffers = (cloudRoutes, upstreamNameById, desiredTriggers) => [...desiredTriggers.keys()].some((routeName) => {
141
+ const desired = desiredTriggers.get(routeName);
142
+ const route = cloudRoutes.find((r) => r.routeName === routeName);
143
+ if (!desired || !route) {
144
+ return true;
145
+ }
146
+ if (desired.method !== route.method || desired.path !== route.path) {
147
+ return true;
148
+ }
149
+ const upstreamId = route.upstreamIds?.[0];
150
+ if (!upstreamId) {
151
+ return true;
152
+ }
153
+ return desired.upstreamName !== upstreamNameById.get(upstreamId);
154
+ });
155
+ exports.cloudTriggerDiffers = cloudTriggerDiffers;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deleteTlsLogResources = exports.ensureOwnedTopic = exports.releaseSharedLogProjectIfUnused = exports.buildSharedProjectResourceState = exports.ensureSharedLogProject = exports.buildSharedProjectName = exports.SHARED_LOG_PROJECT_KEY = void 0;
3
+ exports.deleteTlsLogResources = exports.ensureOwnedTopic = exports.releaseSharedLogProjectIfUnused = exports.buildSharedProjectResourceState = exports.ensureSharedLogProject = exports.buildApigwLogTopicName = exports.buildFunctionLogTopicName = exports.buildSharedProjectName = exports.SHARED_LOG_PROJECT_KEY = void 0;
4
4
  const common_1 = require("../../common");
5
5
  const logger_1 = require("../../common/logger");
6
6
  const lang_1 = require("../../lang");
@@ -15,6 +15,12 @@ const ownershipTag_1 = require("../ownershipTag");
15
15
  exports.SHARED_LOG_PROJECT_KEY = 'logs.project';
16
16
  const buildSharedProjectName = (app, stage) => `${app}-${stage}-tls`;
17
17
  exports.buildSharedProjectName = buildSharedProjectName;
18
+ /** Per-function fn-logs topic (#214 per-owner teardown): the function key is part of the name. */
19
+ const buildFunctionLogTopicName = (service, stage, fnKey) => `${service}-${stage}-${fnKey}-fn-logs`;
20
+ exports.buildFunctionLogTopicName = buildFunctionLogTopicName;
21
+ /** Per-event apigw-logs topic (#214 per-owner teardown): the event key is part of the name. */
22
+ const buildApigwLogTopicName = (service, stage, eventKey) => `${service}-${stage}-${eventKey}-apigw-logs`;
23
+ exports.buildApigwLogTopicName = buildApigwLogTopicName;
18
24
  const resolveSharedProjectName = (shared) => {
19
25
  const instanceId = shared.instances?.[0]?.id;
20
26
  return instanceId ?? shared.definition?.projectName;
@@ -130,8 +136,8 @@ const ensureOwnedTopic = async (context, client, params) => {
130
136
  const { projectName, topicName, logicalId } = params;
131
137
  const existing = await client.tls.getTopic(projectName, topicName);
132
138
  if (existing) {
133
- // Service-scoped topics are shared across a service's functions/events, so
134
- // ownership is verified at the owning-stack level, not per logical id.
139
+ // fn-logs topics are per-function (the key is in the name); the apigw topic
140
+ // stays service-scoped. Adoption verifies the owning stack either way.
135
141
  const tag = (existing.tags ?? []).find((t) => t.Key === ownershipTag_1.OWNERSHIP_TAG_KEY);
136
142
  const parsed = (0, ownershipTag_1.parseOwnershipTagValue)(tag?.Value);
137
143
  if (parsed?.stack === `${context.app}-${context.service}`) {
@@ -6,6 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.generateBucketPlan = void 0;
7
7
  const node_path_1 = __importDefault(require("node:path"));
8
8
  const volcengineClient_1 = require("../../common/volcengineClient");
9
+ const refreshCache_1 = require("../../common/refreshCache");
10
+ const concurrency_1 = require("../../common/concurrency");
11
+ const refreshPlanner_1 = require("../../common/refreshPlanner");
12
+ const planCompare_1 = require("../../common/planCompare");
9
13
  const tosTypes_1 = require("./tosTypes");
10
14
  const stateManager_1 = require("../../common/stateManager");
11
15
  const common_1 = require("../../common");
@@ -25,7 +29,7 @@ const generateBucketPlan = async (context, state, buckets) => {
25
29
  return { items };
26
30
  }
27
31
  const desiredLogicalIds = new Set(buckets.map((bucket) => `buckets.${bucket.key}`));
28
- const bucketItems = await Promise.all(buckets.map(async (bucket) => {
32
+ const bucketItems = await (0, concurrency_1.mapWithConcurrency)(buckets, concurrency_1.PLAN_READ_CONCURRENCY, async (bucket) => {
29
33
  const logicalId = `buckets.${bucket.key}`;
30
34
  const currentState = (0, stateManager_1.getResource)(state, logicalId);
31
35
  const config = (0, tosTypes_1.bucketToTosConfig)(bucket);
@@ -40,63 +44,32 @@ const generateBucketPlan = async (context, state, buckets) => {
40
44
  }
41
45
  })();
42
46
  const desiredDefinition = (0, tosTypes_1.extractTosBucketDefinition)(config, websiteCodeHash);
43
- if (!currentState || currentState.status === 'tainted') {
44
- // No usable local state: probe the provider before planning create.
45
- // If a same-named bucket already exists WITHOUT our ownership tag it
46
- // may belong to another project — fail fast in the plan instead of
47
- // letting the executor discover it mid-deploy.
48
- const client = (0, volcengineClient_1.createVolcengineClient)(context);
49
- const remoteBucket = await client.tos.getBucket(bucket.name);
50
- if (remoteBucket && !(0, ownershipTag_1.isOwnedByStack)(context, logicalId, remoteBucket.Tags)) {
51
- throw new Error(`Bucket ${bucket.name} already exists in provider but is not owned by this stack (missing ${ownershipTag_1.OWNERSHIP_TAG_KEY} tag). Refusing to create — resolve manually.`);
52
- }
53
- return {
54
- logicalId,
55
- action: 'create',
56
- resourceType: 'VOLCENGINE_TOS_BUCKET',
57
- changes: { after: desiredDefinition },
58
- };
59
- }
60
- try {
61
- const client = (0, volcengineClient_1.createVolcengineClient)(context);
62
- const remoteBucket = await client.tos.getBucket(bucket.name);
63
- if (!remoteBucket) {
64
- return {
65
- logicalId,
66
- action: 'create',
67
- resourceType: 'VOLCENGINE_TOS_BUCKET',
68
- changes: {
69
- before: currentState.definition,
70
- after: desiredDefinition,
71
- },
72
- drifted: true,
73
- };
74
- }
75
- const currentDefinition = currentState.definition || {};
76
- const definitionChanged = !(0, common_1.attributesEqual)(currentDefinition, desiredDefinition);
77
- if (definitionChanged) {
47
+ const client = (0, volcengineClient_1.createVolcengineClient)(context);
48
+ return (0, refreshPlanner_1.planRefreshedResource)({
49
+ logicalId,
50
+ resourceType: 'VOLCENGINE_TOS_BUCKET',
51
+ currentState,
52
+ desiredDefinition,
53
+ read: () => (0, refreshCache_1.cachedRefreshRead)(context, `tos.getBucket:${bucket.name}`, () => client.tos.getBucket(bucket.name)),
54
+ isOwned: (remote) => (0, ownershipTag_1.isOwnedByStack)(context, logicalId, remote.Tags),
55
+ foreignError: () => new Error(`Bucket ${bucket.name} already exists in provider but is not owned by this stack (missing ${ownershipTag_1.OWNERSHIP_TAG_KEY} tag). Refusing to create — resolve manually.`),
56
+ cloudToDefinition: tosTypes_1.cloudTosToDefinition,
57
+ extraUpdate: async () => {
58
+ // Policy needs a canonical compare: the cloud returns a parsed
59
+ // object while the config serializes it — see jsonDocumentDiffers.
60
+ const desiredPolicy = desiredDefinition.policy ?? null;
61
+ if (desiredPolicy === null) {
62
+ return { update: false, drifted: false };
63
+ }
64
+ const cloudPolicy = await (0, refreshCache_1.cachedRefreshRead)(context, `tos.getBucketPolicy:${bucket.name}`, () => client.tos.getBucketPolicy(bucket.name));
78
65
  return {
79
- logicalId,
80
- action: 'update',
81
- resourceType: 'VOLCENGINE_TOS_BUCKET',
82
- changes: { before: currentDefinition, after: desiredDefinition },
66
+ update: (0, planCompare_1.jsonDocumentDiffers)(desiredPolicy, cloudPolicy),
83
67
  drifted: true,
84
68
  };
85
- }
86
- return { logicalId, action: 'noop', resourceType: 'VOLCENGINE_TOS_BUCKET' };
87
- }
88
- catch {
89
- return {
90
- logicalId,
91
- action: 'create',
92
- resourceType: 'VOLCENGINE_TOS_BUCKET',
93
- changes: {
94
- before: currentState.definition,
95
- after: desiredDefinition,
96
- },
97
- };
98
- }
99
- }));
69
+ },
70
+ refresh: context.refresh,
71
+ });
72
+ });
100
73
  const allStates = (0, stateManager_1.getAllResources)(state);
101
74
  const deletionItems = Object.entries(allStates)
102
75
  .filter(([logicalId]) => logicalId.startsWith('buckets.') && !desiredLogicalIds.has(logicalId))
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildTosInstanceFromProvider = exports.extractTosBucketDefinition = exports.bucketToTosConfig = void 0;
3
+ exports.buildTosInstanceFromProvider = exports.cloudTosToDefinition = exports.extractTosBucketDefinition = exports.bucketToTosConfig = void 0;
4
4
  const aclMap = {
5
5
  PRIVATE: 'private',
6
6
  PUBLIC_READ: 'public-read',
@@ -49,6 +49,23 @@ const extractTosBucketDefinition = (config, websiteCodeHash) => {
49
49
  };
50
50
  };
51
51
  exports.extractTosBucketDefinition = extractTosBucketDefinition;
52
+ // issue #234 phase 2: cloud-side counterpart of extractTosBucketDefinition.
53
+ // Mirrors the executor write shape key-for-key: acl/storageClass/websiteConfig
54
+ // are compared live; policy (GetBucket returns no bucket-policy document) and
55
+ // websiteCodeHash (local-only) are not refreshable and stay omitted so they can
56
+ // never trigger phantom drift.
57
+ const cloudTosToDefinition = (info) => ({
58
+ bucketName: info.name,
59
+ acl: info.acl ?? null,
60
+ storageClass: info.storageClass ?? null,
61
+ websiteConfiguration: info.websiteConfig
62
+ ? {
63
+ indexDocument: info.websiteConfig.indexDocument ?? null,
64
+ errorDocument: info.websiteConfig.errorDocument ?? null,
65
+ }
66
+ : null,
67
+ });
68
+ exports.cloudTosToDefinition = cloudTosToDefinition;
52
69
  const buildTosInstanceFromProvider = (info, sid) => {
53
70
  return {
54
71
  type: 'VOLCENGINE_TOS_BUCKET',
@@ -13,9 +13,11 @@ const executeCreateAction = async (context, fn, currentState) => {
13
13
  common_1.logger.info(lang_1.lang.__('RESOURCE_CREATED', { resourceType: 'function', name: fn.name }));
14
14
  return newState;
15
15
  };
16
- const executeUpdateAction = async (context, fn, currentState) => {
16
+ const executeUpdateAction = async (context, fn, currentState, drifted) => {
17
17
  common_1.logger.info(lang_1.lang.__('UPDATING_RESOURCE', { resourceType: 'function', name: fn.name }));
18
- const newState = await (0, vefaasResource_1.updateResource)(context, fn, currentState);
18
+ // A drifted plan item means the cloud left the config: force the config
19
+ // re-push even when state-vs-desired sees no change.
20
+ const newState = await (0, vefaasResource_1.updateResource)(context, fn, currentState, { force: drifted === true });
19
21
  common_1.logger.info(lang_1.lang.__('RESOURCE_UPDATED', { resourceType: 'function', name: fn.name }));
20
22
  return newState;
21
23
  };
@@ -42,7 +44,7 @@ const executeSingleItem = async (context, item, functionsMap, currentState) => {
42
44
  if (!fn) {
43
45
  throw new Error(`Function not found for logical ID: ${item.logicalId}`);
44
46
  }
45
- return executeUpdateAction(context, fn, currentState);
47
+ return executeUpdateAction(context, fn, currentState, item.drifted);
46
48
  }
47
49
  case 'delete': {
48
50
  const state = (0, stateManager_1.getResource)(currentState, item.logicalId);