Cache plan IDs from Atlas create responses
Single-host PUT and bulk POST now extract and store the action_plan_id from the Atlas API response in the local cache. Previously only a stub with plan_type/commit_date was now the actual plan ID iscached included so it can be referenced for updates/display without re-fetching from Atlas.
This commit is contained in:
@@ -472,6 +472,45 @@ function createAtlasRouter() {
|
|||||||
if (result.status >= 200 && result.status < 300) {
|
if (result.status >= 200 && result.status < 300) {
|
||||||
let body;
|
let body;
|
||||||
try { body = JSON.parse(result.body); } catch (e) { body = result.body; }
|
try { body = JSON.parse(result.body); } catch (e) { body = result.body; }
|
||||||
|
|
||||||
|
// Update local cache with the created plan
|
||||||
|
try {
|
||||||
|
const { rows: existingRows } = await pool.query(
|
||||||
|
`SELECT plan_count, plans_json FROM atlas_action_plans_cache WHERE host_id = $1`,
|
||||||
|
[hostId]
|
||||||
|
);
|
||||||
|
const existing = existingRows[0];
|
||||||
|
let existingPlans = [];
|
||||||
|
if (existing && existing.plans_json) {
|
||||||
|
try { existingPlans = JSON.parse(existing.plans_json); } catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include the plan ID from Atlas response if available
|
||||||
|
const newPlan = {
|
||||||
|
plan_type,
|
||||||
|
commit_date,
|
||||||
|
source: 'create',
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
...(body?.action_plan_id ? { action_plan_id: body.action_plan_id } : {}),
|
||||||
|
...(body?.id ? { action_plan_id: body.id } : {}),
|
||||||
|
};
|
||||||
|
const updatedPlans = [...existingPlans, newPlan];
|
||||||
|
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO atlas_action_plans_cache (host_id, has_action_plan, plan_count, plans_json, atlas_known, synced_at)
|
||||||
|
VALUES ($1, true, $2, $3, true, NOW())
|
||||||
|
ON CONFLICT(host_id) DO UPDATE SET
|
||||||
|
has_action_plan = true,
|
||||||
|
plan_count = EXCLUDED.plan_count,
|
||||||
|
plans_json = EXCLUDED.plans_json,
|
||||||
|
atlas_known = true,
|
||||||
|
synced_at = EXCLUDED.synced_at`,
|
||||||
|
[hostId, updatedPlans.length, JSON.stringify(updatedPlans)]
|
||||||
|
);
|
||||||
|
} catch (cacheErr) {
|
||||||
|
console.error('[Atlas] Cache update failed after plan create for host', hostId, ':', cacheErr.message);
|
||||||
|
}
|
||||||
|
|
||||||
res.status(result.status).json(body);
|
res.status(result.status).json(body);
|
||||||
} else {
|
} else {
|
||||||
let errorBody;
|
let errorBody;
|
||||||
@@ -603,7 +642,27 @@ function createAtlasRouter() {
|
|||||||
try { existingPlans = JSON.parse(existing.plans_json); } catch (_) {}
|
try { existingPlans = JSON.parse(existing.plans_json); } catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const stubPlan = { plan_type, commit_date, source: 'bulk-create', created_at: new Date().toISOString() };
|
// Extract plan ID from bulk response if available (keyed by host_id)
|
||||||
|
let planId = null;
|
||||||
|
if (body && typeof body === 'object') {
|
||||||
|
// Response may be { results: [{host_id, action_plan_id}] } or { [hostId]: {id} }
|
||||||
|
if (Array.isArray(body.results)) {
|
||||||
|
const match = body.results.find(r => r.host_id === hid || r.host_id === String(hid));
|
||||||
|
if (match) planId = match.action_plan_id || match.id;
|
||||||
|
} else if (body[hid]) {
|
||||||
|
planId = body[hid].action_plan_id || body[hid].id;
|
||||||
|
} else if (body[String(hid)]) {
|
||||||
|
planId = body[String(hid)].action_plan_id || body[String(hid)].id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const stubPlan = {
|
||||||
|
plan_type,
|
||||||
|
commit_date,
|
||||||
|
source: 'bulk-create',
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
...(planId ? { action_plan_id: planId } : {}),
|
||||||
|
};
|
||||||
const updatedPlans = [...existingPlans, stubPlan];
|
const updatedPlans = [...existingPlans, stubPlan];
|
||||||
const newCount = updatedPlans.length;
|
const newCount = updatedPlans.length;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user