Implement functionality to delete a cluster in the OpenShift Management (OCM) service.
Functionality Requirements:
The application should read a cluster ID from a specified file.
The application should interface with the OCM API to delete the cluster associated with the given cluster ID.
After initiating the deletion, the application should continuously check the status of the cluster until it confirms the cluster has been deleted.
Inputs:
A file named cluster-id located in the directory defined by the environment variable SHARED_DIR. This file contains the ID of the cluster to be deleted.
Outputs:
The application should log information about the cluster deletion process, including:
A message indicating which cluster ID is being deleted.
A message confirming the initiation of the deletion process.
Status messages while waiting for the cluster to be deleted, which could include polling information.
File Location and Naming:
The implementation should be placed in the /app/ directory.
Your main script file should be named delete_cluster.sh.
Ensure the script has the appropriate execution permissions.
Additional Notes:
The application should handle errors gracefully and should not crash if an invalid cluster ID is provided or if there are issues communicating with the OCM API.
Use a loop with a sleep interval to wait for the cluster deletion to complete, similar to the provided verifier script.
Tests
The following test script will be used to verify your solution:
bash
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
trap 'CHILDREN=$(jobs -p); if test -n "${CHILDREN}"; then kill ${CHILDREN} && wait; fi' TERM
# ocm login created files here that we now use to delete the cluster
export HOME=${SHARED_DIR}
CLUSTER_ID=$(cat "${HOME}/cluster-id")
echo "Deleting cluster-id: ${CLUSTER_ID}"
ocm delete "/api/clusters_mgmt/v1/clusters/${CLUSTER_ID}"
echo "Waiting for cluster deletion..."
while ocm get "/api/clusters_mgmt/v1/clusters/${CLUSTER_ID}" ; do
sleep 60
done
exit 0