forked from IBM-Cloud/vpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteardown.sh
executable file
·59 lines (52 loc) · 1.75 KB
/
teardown.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# Purge resources created during a test
if [ ! "$TRAVIS" == "true" ];
then
echo "Are you sure to delete all resources and VPCs under $RESOURCE_GROUP?"
read confirmation
if [[ "$confirmation" = "yes" || "$confirmation" = "YES" ]]; then
echo "ok, going ahead..."
else
echo "exiting..."
exit
fi
fi
# ensure we target the test resource group
ibmcloud target -g $RESOURCE_GROUP
# find all resources in the target resource group and delete them
if RESOURCES=$(ibmcloud resource service-instances --output JSON)
then
echo "${RESOURCES}" | jq -r '.[]| "\(.id) \(.guid) \(.sub_type)"' | while read resourceId resourceGuid resourceType
do
echo "Deleting ${resourceId} (${resourceGuid}, ${resourceType})"
# cleanup a KMS service from all its keys
if [ $resourceType == "kms" ];
then
echo "Deleting keys in Key Protect service ${resourceGuid}"
if KP_KEYS=$(ibmcloud kp list -i "${resourceGuid}" --output json)
then
echo "$KP_KEYS" | jq -r .[].id | while read keyId
do
echo "Removing key ${keyId}"
ibmcloud kp delete $keyId -i $resourceGuid
done
fi
fi
# delete the actual service instance
ibmcloud resource service-instance-delete "${resourceId}" -g $RESOURCE_GROUP -f --recursive
done
else
echo "Failed to get resources: ${RESOURCES}"
fi
# find all VPCs in the target resource group and delete them
RESOURCE_GROUP_ID=$(ibmcloud resource group $RESOURCE_GROUP --id)
if VPCS=$(ibmcloud is vpcs --json)
then
echo "$VPCS" | jq -r '.[] | select (.resource_group.id=="'$RESOURCE_GROUP_ID'") | .name' | while read vpcName
do
echo "Deleting VPC ${vpcName}"
./scripts/vpc-cleanup.sh ${vpcName} -f
done
else
echo "Failed to list VPCs: ${VPCS}"
fi