Skip to content

Instantly share code, notes, and snippets.

@stvnjacobs
Last active October 4, 2018 16:08
Show Gist options
  • Save stvnjacobs/cc64aed1c87a36277da8f05523804bea to your computer and use it in GitHub Desktop.
Save stvnjacobs/cc64aed1c87a36277da8f05523804bea to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Depends on curl and jq
# jq: https://stedolan.github.io/jq/
#
# LINODE_API_KEY must be set to v3 API key
# LINODE_API_TOKEN must be set to v4 API token
# !!!! WARNING !!!!
# This will overwrite any tags you already have made
# It prompts you, but still...
# !!!! WARNING !!!!
update_tags() {
curl -s https://api.linode.com/?api_key=${LINODE_API_KEY}\&api_action=linode.list | jq -r '.DATA[] | [.LINODEID, .LPM_DISPLAYGROUP] | @csv' \
| while IFS=, read -r linode_id linode_group; do
curl -s -X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${LINODE_API_TOKEN}" \
-X PUT -d "{\"tags\": [${linode_group}]}" \
https://api.linode.com/v4/linode/instances/${linode_id} \
| jq -r '"\(.id) \(.label) \(.tags)"'
done
}
echo "This will overwrite any existing tags."
read -p "Do you want to continue (y/n)? " choice
case "${choice}" in
y|Y ) update_tags;;
n|N ) echo "Bailing out";;
* ) echo "That isn't what I asked for, but I'm bailing anyway. Please try again.";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment