Discover stale OCI compartments before planning
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-29 12:34:15 -06:00
parent 5a1100700a
commit 61b4a222de
1 changed files with 8 additions and 0 deletions

View File

@ -3827,6 +3827,13 @@ oci_foundation_reconcile_existing() {
return 0
fi
# Persisted OCIDs can refer to deleted resources after a tenancy reset.
# Validate the compartment before using it as the discovery root.
if [[ -n "${LAB_OCI_COMPARTMENT_ID:-}" && "${LAB_OCI_COMPARTMENT_ID}" != "null" ]] && ! "${oci_bin}" iam compartment get --compartment-id "${LAB_OCI_COMPARTMENT_ID}" >/dev/null 2>&1; then
printf 'Persisted OCI compartment %s is absent; rediscovering by name.\n' "${LAB_OCI_COMPARTMENT_ID}" >&2
unset LAB_OCI_COMPARTMENT_ID
fi
# Reuse named child compartments when a previous state file was lost or a
# prior apply completed only partway. OCI rejects a second compartment with
# the same name, so discover the active OCID before Terraform plans.
@ -3849,6 +3856,7 @@ oci_foundation_reconcile_existing() {
[[ -n "${compartment_id}" && "${compartment_id}" != "null" ]] || {
compartment_id="$("${oci_bin}" iam compartment list \
--compartment-id "${LAB_OCI_TENANCY_ID}" \
--compartment-id-in-subtree true \
--name "${compartment_name}" --all \
--query 'data[0].id' --raw-output 2>/dev/null || true)"
if [[ -n "${compartment_id}" && "${compartment_id}" != "null" ]]; then