diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 96fcd84..7358df6 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3889,12 +3889,9 @@ oci_foundation_reconcile_existing() { # to data-source mode and planning their destruction. unset LAB_OCI_EXISTING_VCN_ID LAB_OCI_EXISTING_PUBLIC_SUBNET_ID printf 'Preserving state-managed OCI VCN and network resources.\n' - elif [[ -z "${LAB_OCI_EXISTING_VCN_ID:-}" && -n "${LAB_OCI_COMPARTMENT_ID:-}" ]]; then + elif [[ -z "${LAB_OCI_EXISTING_VCN_ID:-}" ]]; then local discovered_vcn_id - discovered_vcn_id="$(${oci_bin} network vcn list \ - --compartment-id "${LAB_OCI_COMPARTMENT_ID}" --all \ - --query 'data[?"display-name"==`homelab-vcn`].id | [0]' \ - --raw-output 2>/dev/null || true)" + discovered_vcn_id="$(oci_discover_vcn_id "${LAB_OCI_TENANCY_ID}" "${LAB_OCI_VCN_DISPLAY_NAME:-homelab-vcn}" || true)" if [[ -n "${discovered_vcn_id}" && "${discovered_vcn_id}" != "null" ]]; then export LAB_OCI_EXISTING_VCN_ID="${discovered_vcn_id}" printf 'Reusing existing OCI VCN (%s).\n' "${discovered_vcn_id}" @@ -4348,6 +4345,29 @@ PY return 1 } +oci_discover_vcn_id() { + local root_compartment="$1" display_name="$2" oci_bin compartments compartment_id payload result + oci_bin="$(oci_cli_resolve)" || return 1 + compartments="${root_compartment}"$'\n'$("${oci_bin}" iam compartment list --compartment-id "${root_compartment}" --compartment-id-in-subtree true --all --query 'data[].id' --raw-output 2>/dev/null || true) + while IFS= read -r compartment_id; do + [[ -n "${compartment_id}" ]] || continue + payload="$("${oci_bin}" network vcn list --compartment-id "${compartment_id}" --all --output json 2>/dev/null || true)" + [[ -n "${payload}" ]] || continue + result="$(OCI_DISCOVERY_JSON="${payload}" python3 - "${display_name}" <<'PY' +import json, os, sys +name = sys.argv[1] +for item in json.loads(os.environ["OCI_DISCOVERY_JSON"]).get("data", []): + tags = item.get("freeform-tags") or {} + if item.get("display-name") == name or tags.get("managed_by") == "jeannie": + print(item.get("id", "")) + break +PY + )" + [[ -n "${result}" ]] && { printf '%s\n' "${result}"; return 0; } + done <<<"${compartments}" + return 1 +} + oci_discover_active_bastion_id() { local root_compartment="$1" bastion_name="$2" oci_bin compartments compartment_id payload result oci_bin="$(oci_cli_resolve)" || return 1