fix: use List endpoints for status detection — Get endpoints lack state fields

This commit is contained in:
2026-09-09 00:38:23 +00:00
parent 0cc0745c86
commit b5ef495764
+55 -15
View File
@@ -19,7 +19,22 @@ const DETECT_ORDER: ResourceType[] = [
"sync_resource", "sync_resource",
]; ];
/** Map resource type → Komodo Get request name */ /** Map resource type → List request name (returns list items with state) */
const LIST_REQUEST: Record<string, string> = {
stack: "ListStacks",
deployment: "ListDeployments",
build: "ListBuilds",
server: "ListServers",
procedure: "ListProcedures",
repo: "ListRepos",
action: "ListActions",
alerter: "ListAlerters",
builder: "ListBuilders",
swarm: "ListSwarms",
sync_resource: "ListResourceSyncs",
};
/** Map resource type → Get request name (fallback for types without state in List) */
const GET_REQUEST: Record<string, string> = { const GET_REQUEST: Record<string, string> = {
stack: "GetStack", stack: "GetStack",
deployment: "GetDeployment", deployment: "GetDeployment",
@@ -50,8 +65,8 @@ export interface DetectionResult {
} }
/** /**
* Auto-detect a resource by trying Get endpoints across types. * Auto-detect a resource by trying List endpoints with filter across types.
* Returns the first match, or null if nothing found. * Returns the first match with full list-item info (including state), or null.
*/ */
export async function detectResource( export async function detectResource(
client: KomodoClient, client: KomodoClient,
@@ -61,20 +76,45 @@ export async function detectResource(
const typesToTry = hint ? [hint, ...DETECT_ORDER.filter((t) => t !== hint)] : DETECT_ORDER; const typesToTry = hint ? [hint, ...DETECT_ORDER.filter((t) => t !== hint)] : DETECT_ORDER;
for (const type of typesToTry) { for (const type of typesToTry) {
const requestName = GET_REQUEST[type]; // Try List with filter first — returns list items with state info
if (!requestName) continue; const listRequest = LIST_REQUEST[type];
if (listRequest) {
try {
const items = (await client.rpc("read", listRequest, {
filter: nameOrId,
})) as Array<Record<string, unknown>>;
const paramKey = GET_PARAM_KEY[type] || "id"; if (Array.isArray(items) && items.length > 0) {
try { // Find exact name match
const resource = (await client.rpc("read", requestName, { const match = items.find((item) => {
[paramKey]: nameOrId, if (item.name === nameOrId) return true;
})) as Record<string, unknown>; const id = item._id as Record<string, unknown> | undefined;
return id?.$oid === nameOrId;
if (resource && (resource.name || resource._id)) { });
return { type, resource }; if (match) {
return { type, resource: match };
}
}
} catch {
// List failed — try Get fallback
}
}
// Fallback: try Get endpoint
const getRequest = GET_REQUEST[type];
if (getRequest) {
const paramKey = GET_PARAM_KEY[type] || "id";
try {
const resource = (await client.rpc("read", getRequest, {
[paramKey]: nameOrId,
})) as Record<string, unknown>;
if (resource && (resource.name || resource._id)) {
return { type, resource };
}
} catch {
// Not found on this type — try next
} }
} catch {
// Not found on this type — try next
} }
} }