fix: tolerate unsupported OCI cleanup list APIs
This commit is contained in:
parent
06825c81de
commit
fab0596b74
|
|
@ -23,35 +23,35 @@ region="${LAB_OCI_REGION:-${OCI_CLI_REGION:-}}"
|
||||||
exit 2
|
exit 2
|
||||||
}
|
}
|
||||||
|
|
||||||
json_ids() {
|
oci_json_ids() {
|
||||||
local command_json="$1"
|
local command_json="$(cat)"
|
||||||
python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))' <<<"${command_json}"
|
python3 -c 'import json,sys; raw=sys.stdin.read().strip(); data=json.loads(raw) if raw else {}; print("\\n".join(x["id"] for x in data.get("data", [])))' <<<"${command_json}"
|
||||||
}
|
}
|
||||||
|
|
||||||
mapfile -t compartments < <(
|
mapfile -t compartments < <(
|
||||||
printf '%s\n' "${tenancy}"
|
printf '%s\n' "${tenancy}"
|
||||||
"${oci_bin}" iam compartment list --compartment-id "${tenancy}" \
|
"${oci_bin}" iam compartment list --compartment-id "${tenancy}" \
|
||||||
--compartment-id-in-subtree true --all --output json \
|
--compartment-id-in-subtree true --all --output json 2>/dev/null \
|
||||||
| python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))'
|
| python3 -c 'import json,sys; raw=sys.stdin.read().strip(); data=json.loads(raw) if raw else {}; print("\\n".join(x["id"] for x in data.get("data", [])))'
|
||||||
)
|
)
|
||||||
|
|
||||||
declare -a instances=() boot_volumes=() block_volumes=() public_ips=() vcns=()
|
declare -a instances=() boot_volumes=() block_volumes=() public_ips=() vcns=()
|
||||||
declare -a subnets=() gateways=() route_tables=() security_lists=() dhcp_options=() nsgs=()
|
declare -a subnets=() gateways=() route_tables=() security_lists=() dhcp_options=() nsgs=()
|
||||||
for compartment in "${compartments[@]}"; do
|
for compartment in "${compartments[@]}"; do
|
||||||
[[ -n "${compartment}" ]] || continue
|
[[ -n "${compartment}" ]] || continue
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && instances+=("${compartment}|${id}"); done < <("${oci_bin}" compute instance list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && instances+=("${compartment}|${id}"); done < <("${oci_bin}" compute instance list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && boot_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv boot-volume list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && boot_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv boot-volume list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && block_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv volume list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && block_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv volume list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && public_ips+=("${compartment}|${id}"); done < <("${oci_bin}" network public-ip list --compartment-id "${compartment}" --scope REGION --region "${region}" --all --output json 2>/dev/null | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))' || true)
|
while IFS= read -r id; do [[ -n "${id}" ]] && public_ips+=("${compartment}|${id}"); done < <("${oci_bin}" network public-ip list --compartment-id "${compartment}" --scope REGION --region "${region}" --all --output json 2>/dev/null | oci_json_ids || true)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && vcns+=("${compartment}|${id}"); done < <("${oci_bin}" network vcn list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && vcns+=("${compartment}|${id}"); done < <("${oci_bin}" network vcn list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && subnets+=("${compartment}|${id}"); done < <("${oci_bin}" network subnet list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && subnets+=("${compartment}|${id}"); done < <("${oci_bin}" network subnet list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
for resource in internet-gateway nat-gateway service-gateway local-peering-gateway; do
|
for resource in internet-gateway nat-gateway service-gateway local-peering-gateway; do
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && gateways+=("${resource}|${compartment}|${id}"); done < <("${oci_bin}" network "${resource}" list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && gateways+=("${resource}|${compartment}|${id}"); done < <("${oci_bin}" network "${resource}" list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
done
|
done
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && route_tables+=("${compartment}|${id}"); done < <("${oci_bin}" network route-table list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && route_tables+=("${compartment}|${id}"); done < <("${oci_bin}" network route-table list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && security_lists+=("${compartment}|${id}"); done < <("${oci_bin}" network security-list list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && security_lists+=("${compartment}|${id}"); done < <("${oci_bin}" network security-list list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && dhcp_options+=("${compartment}|${id}"); done < <("${oci_bin}" network dhcp-options list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && dhcp_options+=("${compartment}|${id}"); done < <("${oci_bin}" network dhcp-options list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
while IFS= read -r id; do [[ -n "${id}" ]] && nsgs+=("${compartment}|${id}"); done < <("${oci_bin}" network nsg list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
|
while IFS= read -r id; do [[ -n "${id}" ]] && nsgs+=("${compartment}|${id}"); done < <("${oci_bin}" network nsg list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "OCI infrastructure cleanup (${action})"
|
echo "OCI infrastructure cleanup (${action})"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue