Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Last active October 14, 2021 15:08
Show Gist options
  • Save greyhoundforty/1cd71970a7d7af376e64719d7b3bd20a to your computer and use it in GitHub Desktop.
Save greyhoundforty/1cd71970a7d7af376e64719d7b3bd20a to your computer and use it in GitHub Desktop.
Tagg IBM Classic IaaS Block / File Storage Volumes

The Classic File and Block volumes do not support tagging via the Cloud Portal. In their place their is a field to store notes about the volume. We can use the Tagging API to add tags. This will allow us to use the Search API to query for volumes by tag.

It is recommended to use IBM Cloud Shell if you do not already have the following tools installed on your local system:

  • IBM Cloud CLI
  • jq
  • curl

Set IBM Cloud API Key

Existing API Key If you have an existing IBM Cloud API Key simply export it to your shell session.

export IBMCLOUD_API_KEY=<Your IBM Cloud API Key>

No API Key If you do not have an API key to use for your Cloud Shell session, run the following commands to generate an API key and export it as IBMCLOUD_API_KEY

ibmcloud iam api-key-create testing -d "API Key for Cloud Shell tag project" --file apikey.json
export IBMCLOUD_API_KEY=$(jq -r .apikey < apikey.json)

Generate IAM Token

With the API key exported we now need to generate an IAM token in order to interact with the Search API. The following command will generate a new IAM token from your API Key and store it as the variable iam_token.

iam_token=`curl -s -k -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" --data-urlencode "apikey=${IBMCLOUD_API_KEY}" "https://iam.cloud.ibm.com/identity/token" | jq -r '(.token_type + " " + .access_token)'`

Find the CRN for the volume you want to tag

In order to tag the volume we need to get the CRN for the volume. The following search will return the volume name and CRN for each block volume on the account. If you are wanting to tag File volumes change (type:block-storage AND service_name:block-storage) to (type:file-storage AND service_name:file-storage) in the API call.

$ curl -s -X POST "https://api.global-search-tagging.cloud.ibm.com/v3/resources/search?limit=50" -H "accept: application/json" -H "Authorization: ${iam_token}" -H "content-type: application/json" -d '{"query":"(type:block-storage AND service_name:block-storage)" , "fields": [ "name", "crn" ]}' | jq -r

Export CRN of volume

export VOLUME_CRN='CRN of Volume'

Add Tags to volume

curl -X POST -H "Authorization: ${iam_token}" -H "Accept: application/json" -H "Content-Type: application/json" \
 -d '{ "resources": [{ "resource_id": "${VOLUME_CRN}" }], "tag_names": ["tag1", "tag2"] }' \
"https://tags.global-search-tagging.cloud.ibm.com/v3/tags/attach"

Add Tags to all volumes on account

This code will return all the block volumes on the account and then loop through and apply the required tags. This version uses both the REST API and the IBM Cloud CLI so Cloud Shell is recommended if you do not already have the IBM Cloud CLI installed.

Notice the limit=500 in the REST API call. This instructs the API to only return 500 volumes at a time. If you need to tag more than that you will need to adjust the limit.

The tags are assigned using --tag-names flag with a comma separated list of tag names. In this example the tags being assigned would be tag1 and tag2.

for i in $(curl -s -X POST "https://api.global-search-tagging.cloud.ibm.com/v3/resources/search?limit=500" -H "accept: application/json" -H "Authorization: ${iam_token}" -H "content-type: application/json" -d '{"query":"(type:file-storage AND service_name:file-storage)"}' | jq -r '.items[].crn'); do ibmcloud resource tag-attach --tag-names tag1,tag2 --resource-id "${i}" ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment