|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +VPC_NAME="$1" |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +echo "*** Deleting instances..." |
| 8 | +echo "" |
| 9 | + |
| 10 | +ibmcloud is instances --all-resource-groups --output JSON | \ |
| 11 | + jq -c --arg VPC_NAME $VPC_NAME '.[] | select(.vpc.name == $VPC_NAME)' | \ |
| 12 | + while read instance; |
| 13 | +do |
| 14 | + |
| 15 | + id=$(echo "$instance" | jq -r '.id') |
| 16 | + name=$(echo "$instance" | jq -r '.name') |
| 17 | + |
| 18 | + echo $instance | jq -c '.network_interfaces | .[] | .floating_ips | .[]' | \ |
| 19 | + while read floating_ip; |
| 20 | + do |
| 21 | + |
| 22 | + floating_ip_id=$(echo "$floating_ip" | jq -r '.id') |
| 23 | + floating_ip_name=$(echo "$floating_ip" | jq -r '.name') |
| 24 | + |
| 25 | + echo "Releasing instance floating ip: ${floating_ip_name} (${floating_ip_id})" |
| 26 | + ibmcloud is floating-ip-release -f "${floating_ip_id}" |
| 27 | + done |
| 28 | + |
| 29 | + echo "Stopping instance: ${name} (${id})" |
| 30 | + ibmcloud is instance-stop -f "${id}" |
| 31 | + |
| 32 | + count=0 |
| 33 | + while [[ $(ibmcloud is instance "${id}" --output JSON | jq -r '.status') == "stopping" ]] && [[ ${count} -lt 10 ]]; do |
| 34 | + echo "Waiting for instance to stop: ${name} (${id})" |
| 35 | + sleep 30 |
| 36 | + |
| 37 | + count=$((count + 1)) |
| 38 | + done |
| 39 | + if [[ $count -eq 10 ]]; then |
| 40 | + echo "Timed out waiting for instance to stop: ${name} (${id})" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + echo "Deleting instance: ${name} (${id})" |
| 45 | + ibmcloud is instance-delete -f "${id}" |
| 46 | + |
| 47 | + count=0 |
| 48 | + while ibmcloud is instance "${id}" && [[ ${count} -lt 10 ]]; do |
| 49 | + echo "Waiting for instance to be deleted: ${name} (${id})" |
| 50 | + sleep 30 |
| 51 | + |
| 52 | + count=$((count + 1)) |
| 53 | + done |
| 54 | + if [[ $count -eq 10 ]]; then |
| 55 | + echo "Timed out waiting for instance to be deleted: ${name} (${id})" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | +done |
| 59 | + |
| 60 | +echo "" |
| 61 | +echo "*** Deleting endpoint gateways..." |
| 62 | +echo "" |
| 63 | + |
| 64 | +ibmcloud is endpoint-gateways --all-resource-groups --output JSON | \ |
| 65 | + jq -c --arg VPC_NAME sms-vpn-mgmt-vpc '.[] | select(.vpc.name == $VPC_NAME)' | \ |
| 66 | + while read endpoint; |
| 67 | +do |
| 68 | + |
| 69 | + id=$(echo "$endpoint" | jq -r '.id') |
| 70 | + name=$(echo "$endpoint" | jq -r '.name') |
| 71 | + |
| 72 | + echo "$endpoint" | jq -c '.ips | .[]' | \ |
| 73 | + while read endpoint_ip; |
| 74 | + do |
| 75 | + ip_id=$(echo "$endpoint_ip" | jq -r '.id') |
| 76 | + ip_name=$(echo "$endpoint_ip" | jq -r '.name') |
| 77 | + |
| 78 | + echo "Unbinding endpoint gateway ip: ${ip_name} (${ip_id})" |
| 79 | + ibmcloud is endpoint-gateway-reserved-ip-unbind -f "${id}" --reserved-ip-id "${ip_id}" |
| 80 | + done |
| 81 | + |
| 82 | + echo "Deleting endpoint gateway: ${name} (${id})" |
| 83 | + ibmcloud is endpoint-gateway-delete -f "${id}" |
| 84 | +done |
| 85 | + |
| 86 | +echo "" |
| 87 | +echo "*** Deleting subnets..." |
| 88 | +echo "" |
| 89 | + |
| 90 | +ibmcloud is subnets --all-resource-groups --output JSON | \ |
| 91 | + jq -c --arg VPC_NAME "${VPC_NAME}" '.[] | select(.vpc.name == $VPC_NAME)' | \ |
| 92 | + while read subnet; |
| 93 | +do |
| 94 | + |
| 95 | + id=$(echo "$subnet" | jq -r '.id') |
| 96 | + name=$(echo "$subnet" | jq -r '.name') |
| 97 | + public_gateway_id=$(echo "$subnet" | jq -r '.public_gateway.id // ""') |
| 98 | + public_gateway_name=$(echo "$subnet" | jq -r '.public_gateway.name // ""') |
| 99 | + |
| 100 | + if [[ -n "${public_gateway_id}" ]]; then |
| 101 | + echo "Detach public gateway from subnet: ${public_gateway_name} (${public_gateway_id})" |
| 102 | + ibmcloud is subnet-public-gateway-detach "${id}" |
| 103 | + fi |
| 104 | + |
| 105 | + ibmcloud is subnet-reserved-ips "${id}" | \ |
| 106 | + jq -c '.[]' | \ |
| 107 | + while read reserved_ip; |
| 108 | + do |
| 109 | + ip_id=$(echo "$reserved_ip" | jq -r '.id') |
| 110 | + ip_name=$(echo "$reserved_ip" | jq -r '.name') |
| 111 | + |
| 112 | + echo "Deleting reserved ip: ${ip_name} (${ip_id})" |
| 113 | + ibmcloud is subnet-reserved-ip-delete -f "${id}" "${ip_id}" |
| 114 | + done |
| 115 | + |
| 116 | + echo "Deleting subnet: ${name} (${id})" |
| 117 | + ibmcloud is subnet-delete -f "${id}" |
| 118 | +done |
| 119 | + |
| 120 | +echo "" |
| 121 | +echo "*** Deleting public gateways..." |
| 122 | +echo "" |
| 123 | + |
| 124 | +ibmcloud is public-gateways --all-resource-groups --output JSON | \ |
| 125 | + jq -c --arg VPC_NAME sms-vpn-vpc '.[] | select(.vpc.name == $VPC_NAME)' | \ |
| 126 | + while read gateway; |
| 127 | +do |
| 128 | + |
| 129 | + id=$(echo "$gateway" | jq -r '.id') |
| 130 | + name=$(echo "$gateway" | jq -r '.name') |
| 131 | + floating_ip_id=$(echo "$gateway" | jq -r '.floating_ip.id') |
| 132 | + floating_ip_name=$(echo "$gateway" | jq -r '.floating_ip.name') |
| 133 | + |
| 134 | + echo "Deleting public gateway: ${name} (${id})" |
| 135 | + ibmcloud is public-gateway-delete -f "${id}" |
| 136 | + |
| 137 | +# if [[ -n "${floating_ip_id}" ]]; then |
| 138 | +# echo "Releasing gateway floating ip: ${floating_ip_name} (${floating_ip_id})" |
| 139 | +# ibmcloud is floating-ip-release -f "${floating_ip_id}" |
| 140 | +# fi |
| 141 | +done |
| 142 | + |
| 143 | +echo "" |
| 144 | +echo "*** Deleting VPC..." |
| 145 | +echo "" |
| 146 | + |
| 147 | +ibmcloud is vpcs --all-resource-groups --output JSON | \ |
| 148 | + jq -c --arg VPC_NAME "${VPC_NAME}" '.[] | select(.name == $VPC_NAME)' | \ |
| 149 | + while read vpc; |
| 150 | +do |
| 151 | + |
| 152 | + id=$(echo "$vpc" | jq -r '.id') |
| 153 | + name="${VPC_NAME}" |
| 154 | + |
| 155 | + echo "Deleting VPC: ${name} (${id})" |
| 156 | + ibmcloud is vpc-delete -f "${id}" |
| 157 | +done |
0 commit comments