fix: preserve state-managed OCI network resources
This commit is contained in:
parent
d84ac8ebc3
commit
c38973374a
|
|
@ -6,7 +6,7 @@ locals {
|
||||||
email_subnet_id = trimspace(var.existing_email_subnet_id) != "" ? var.existing_email_subnet_id : oci_core_subnet.email[0].id
|
email_subnet_id = trimspace(var.existing_email_subnet_id) != "" ? var.existing_email_subnet_id : oci_core_subnet.email[0].id
|
||||||
# The persisted default (10.0.2.0/24) may belong to an older VCN. When
|
# The persisted default (10.0.2.0/24) may belong to an older VCN. When
|
||||||
# attaching to an existing VCN, derive a deterministic /24 inside it.
|
# attaching to an existing VCN, derive a deterministic /24 inside it.
|
||||||
email_subnet_cidr = trimspace(var.existing_vcn_id) != "" ? cidrsubnet(data.oci_core_vcn.existing[0].cidr_blocks[0], 8, 2) : var.email_subnet_cidr
|
email_subnet_cidr = trimspace(var.existing_vcn_id) != "" ? cidrsubnet(data.oci_core_vcn.existing[0].cidr_blocks[0], 8, 2) : cidrsubnet(var.vcn_cidr, 8, 2)
|
||||||
email_route_table_id = trimspace(var.existing_vcn_id) != "" ? data.oci_core_subnet.existing_public[0].route_table_id : local.public_route_table_id
|
email_route_table_id = trimspace(var.existing_vcn_id) != "" ? data.oci_core_subnet.existing_public[0].route_table_id : local.public_route_table_id
|
||||||
bastion_target_subnet_id = trimspace(var.bastion_target_subnet_id) != "" ? var.bastion_target_subnet_id : local.public_subnet_id
|
bastion_target_subnet_id = trimspace(var.bastion_target_subnet_id) != "" ? var.bastion_target_subnet_id : local.public_subnet_id
|
||||||
bastion_client_cidrs = length(var.bastion_client_cidr_block_allow_list) > 0 ? var.bastion_client_cidr_block_allow_list : [var.admin_cidr]
|
bastion_client_cidrs = length(var.bastion_client_cidr_block_allow_list) > 0 ? var.bastion_client_cidr_block_allow_list : [var.admin_cidr]
|
||||||
|
|
|
||||||
|
|
@ -3641,9 +3641,13 @@ oci_foundation_reconcile_existing() {
|
||||||
local oci_bin="" output_file status compartment_id compartment_name
|
local oci_bin="" output_file status compartment_id compartment_name
|
||||||
local homelab_state_file="${LAB_OCI_FOUNDATION_STATE_FILE:-${REPO_ROOT}/bootstrap/oci/terraform.tfstate}"
|
local homelab_state_file="${LAB_OCI_FOUNDATION_STATE_FILE:-${REPO_ROOT}/bootstrap/oci/terraform.tfstate}"
|
||||||
local homelab_compartment_managed=false
|
local homelab_compartment_managed=false
|
||||||
|
local network_state_managed=false
|
||||||
if [[ -f "${homelab_state_file}" ]] && grep -q '"type"[[:space:]]*:[[:space:]]*"oci_identity_compartment"' "${homelab_state_file}"; then
|
if [[ -f "${homelab_state_file}" ]] && grep -q '"type"[[:space:]]*:[[:space:]]*"oci_identity_compartment"' "${homelab_state_file}"; then
|
||||||
homelab_compartment_managed=true
|
homelab_compartment_managed=true
|
||||||
fi
|
fi
|
||||||
|
if [[ -f "${homelab_state_file}" ]] && grep -q '"type"[[:space:]]*:[[:space:]]*"oci_core_vcn"' "${homelab_state_file}"; then
|
||||||
|
network_state_managed=true
|
||||||
|
fi
|
||||||
if command -v oci >/dev/null 2>&1; then
|
if command -v oci >/dev/null 2>&1; then
|
||||||
oci_bin="$(command -v oci)"
|
oci_bin="$(command -v oci)"
|
||||||
elif [[ -x "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci" ]]; then
|
elif [[ -x "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci" ]]; then
|
||||||
|
|
@ -3700,7 +3704,13 @@ oci_foundation_reconcile_existing() {
|
||||||
# Recover an existing VCN when the persisted input was lost. This keeps
|
# Recover an existing VCN when the persisted input was lost. This keeps
|
||||||
# subnet CIDR derivation and all dependent resources anchored to the
|
# subnet CIDR derivation and all dependent resources anchored to the
|
||||||
# surviving network instead of falling back to the default VCN inputs.
|
# surviving network instead of falling back to the default VCN inputs.
|
||||||
if [[ -z "${LAB_OCI_EXISTING_VCN_ID:-}" && -n "${LAB_OCI_COMPARTMENT_ID:-}" ]]; then
|
if [[ "${network_state_managed}" == "true" ]]; then
|
||||||
|
# The VCN and dependent network resources are still managed by this
|
||||||
|
# state file; keep their resource counts stable rather than switching
|
||||||
|
# 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
|
||||||
local discovered_vcn_id
|
local discovered_vcn_id
|
||||||
discovered_vcn_id="$(${oci_bin} network vcn list \
|
discovered_vcn_id="$(${oci_bin} network vcn list \
|
||||||
--compartment-id "${LAB_OCI_COMPARTMENT_ID}" --all \
|
--compartment-id "${LAB_OCI_COMPARTMENT_ID}" --all \
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue