Adopt legacy OCI VCNs by CIDR
This commit is contained in:
parent
fbed94b309
commit
254823e064
|
|
@ -3891,7 +3891,7 @@ oci_foundation_reconcile_existing() {
|
|||
printf 'Preserving state-managed OCI VCN and network resources.\n'
|
||||
elif [[ -z "${LAB_OCI_EXISTING_VCN_ID:-}" ]]; then
|
||||
local discovered_vcn_id
|
||||
discovered_vcn_id="$(oci_discover_vcn_id "${LAB_OCI_TENANCY_ID}" "${LAB_OCI_VCN_DISPLAY_NAME:-homelab-vcn}" || true)"
|
||||
discovered_vcn_id="$(oci_discover_vcn_id "${LAB_OCI_TENANCY_ID}" "${LAB_OCI_VCN_DISPLAY_NAME:-homelab-vcn}" "${LAB_OCI_VCN_CIDR:-10.50.0.0/16}" || 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}"
|
||||
|
|
@ -4346,19 +4346,21 @@ PY
|
|||
}
|
||||
|
||||
oci_discover_vcn_id() {
|
||||
local root_compartment="$1" display_name="$2" oci_bin compartments compartment_id payload result
|
||||
local root_compartment="$1" display_name="$2" target_cidr="${3:-}" 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'
|
||||
result="$(OCI_DISCOVERY_JSON="${payload}" python3 - "${display_name}" "${target_cidr}" <<'PY'
|
||||
import json, os, sys
|
||||
name = sys.argv[1]
|
||||
target_cidr = sys.argv[2]
|
||||
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":
|
||||
cidrs = item.get("cidr-blocks") or []
|
||||
if item.get("display-name") == name or tags.get("managed_by") == "jeannie" or target_cidr in cidrs:
|
||||
print(item.get("id", ""))
|
||||
break
|
||||
PY
|
||||
|
|
|
|||
Loading…
Reference in New Issue