You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.5 KiB

#!/usr/bin/env bash
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--token)
TOKEN="$2"
shift # past argument
shift # past value
;;
--device)
DEVICE="$2"
shift # past argument
shift # past value
;;
--volume-name)
VOLUME_NAME="$2"
shift # past argument
shift # past value
;;
--volume-region)
VOLUME_REGION="$2"
shift # past argument
shift # past value
;;
--buffer)
BUFFER="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
echo "DEBUG"
echo "====="
echo "TOKEN = ${TOKEN}"
echo "DEVICE = ${DEVICE}"
echo "VOLUME_NAME = ${VOLUME_NAME}"
echo "VOLUME_REGION = ${VOLUME_REGION}"
echo "BUFFER = ${BUFFER}"
echo
digital_ocean_api () {
api="https://api.digitalocean.com/v2"
endpoint=$1
curl --silent -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" "${api}/${endpoint}"
}
main () {
# Get volume data
echo "Getting data for volume \"${VOLUME_NAME}\" in region \"${VOLUME_REGION}\"..."
volume_json=$(digital_ocean_api "volumes?region=${VOLUME_REGION}" | jq -r --arg VOLUME_NAME "${VOLUME_NAME}" '.volumes | .[] | select(.name==$VOLUME_NAME)')
volume_id=$(echo $volume_json | jq -r .id)
volume_size=$(echo $volume_json | jq -r .size_gigabytes)
echo "Volume ID is \"${volume_id}\" and is currently ${volume_size}GB"
}
main