Identify owners of VSIs

In IBM Cloud’s virtual server instance (VSI) API, the VSI object does not itself identify the creator of the VSI. However, the creator is known to the cloud resource controller, so you can use the cloud resource API to determine the creator of a VSI.

The path to do this is to:

  1. Call VSI API to identify VSIs
  2. Call resource API to identify owner
  3. Call (or cache) user API to map owner to email

Here is some sample code using the ibmcloud CLI to do this:

#!/bin/zsh

# Identify owners of all VPC VSIs in IBM Cloud account

users=$(ibmcloud account users --output json)

for region in $(ibmcloud regions --output json | jq -r '.[]|.Name')
do
  ibmcloud target -r $region -q > /dev/null
  for instance in $(ibmcloud is instances --output json | jq -r '.[]|.name')
  do
    # VSI names might be reused in multiple regions; filter by region
    user=$(ibmcloud resource service-instance $instance --output json 2> /dev/null | jq -r ".[]|select(.region_id==\"$region\")|.created_by")
    email=$(echo $users | jq -r ".[]|select(.ibmUniqueId==\"$user\")|.email")
    echo "VSI $instance in region $region was deployed by $email"
  done
done

Leave a comment