-if [ ${#os_ports[@]} -eq 0 ]; then
- echo "No orphaned ports found."
-else
- for port in "${os_ports[@]}"; do
- echo "Removing orphaned port $port"
- openstack --os-cloud "$os_cloud" port delete "$port"
- done
+ # Cleanup objects where created_at is older than specified cutoff time
+ # created_at_uxts is measured against UNIX epoch; lower values are older
+ if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
+ echo "Removing orphaned port $uuid created $created_at_uxts > $age"
+ openstack --os-cloud "$os_cloud" port delete "$uuid"
+ fi
+ else
+ # Don't stop the job, but warn about unexpected value
+ echo "Unknown/unexpected value for created_at: ${created_at}"
+ fi
+}
+
+_rmtemp()
+{
+ if [ -f "$tmpfile" ]; then
+ # Removes temporary file on script exit
+ rm -f "$tmpfile"
+ fi
+}
+
+trap _rmtemp EXIT
+
+# Output the initial list of port UUIDs to a temporary file
+openstack --os-cloud "$os_cloud" port list -f value -c ID -c status \
+ | { grep -e DOWN || true; } | { awk '{print $1}' || true; } > "$tmpfile"
+
+# Count the number to process
+total=$(wc -l "$tmpfile" | awk '{print $1}')
+
+if [ "$total" -eq 0 ]; then
+ echo "No orphaned ports to process."
+ exit 0