Advertisement
gregmark

OpenStack or Bust, Part 9: stack_id script

Mar 17th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.95 KB | None | 0 0
  1. #!/bin/bash
  2. #set -x
  3. ######################################################################
  4. ###
  5. ### stack_id
  6. ### --------
  7. ###
  8. ### Given name of tenant, user, role, etc, printout keystone id
  9. ###
  10. ######################################################################
  11.  
  12. source /root/.keystonerc
  13.  
  14. function USAGE {
  15.     cat <<-EOF
  16.         USAGE:
  17.           $(basename $0) <option> <data_name>
  18.  
  19.         OPTIONS:
  20.           -t    tenant
  21.           -u    user
  22.           -r    role
  23.           -s    service
  24.           -i    image
  25.           -n    network
  26.           -b    subnet
  27.           -o    router
  28.  
  29.         ARGS:
  30.           data_name    name of tenant, user, or role whose id you want
  31.  
  32.         EXAMPLE:
  33.           $(basename $0) -u admin
  34. EOF
  35. }
  36.  
  37. query="/usr/bin/keystone"
  38.  
  39. ### Get Options
  40. ### Select data table to query
  41. while getopts ":htursinbo" opt; do   # Leading colon xlates unknown opts to ?
  42.     # Pick the first option seen, ignore rest
  43.     if [[ -n "$table" ]] ; then
  44.         continue
  45.     fi
  46.     case $opt in
  47.         h) USAGE; exit 1;;
  48.         t) table=tenant;;
  49.         u) table=user;;
  50.         r) table=role;;
  51.         s) table=service;;
  52.         i) table=image; query="/usr/bin/glance";;
  53.         n) table=net; query="/usr/bin/quantum";;
  54.         b) table=subnet; query="/usr/bin/quantum";;
  55.         o) table=router; query="/usr/bin/quantum";;
  56.         ?) printf "[ERROR] Invalid option\n\n" >&2
  57.            USAGE
  58.            exit 1;;
  59.     esac
  60. done
  61. shift $((OPTIND - 1))
  62.  
  63. ### Get data name
  64. name=$1
  65.  
  66. ### Early Exit
  67. table_regex="^(tenant|user|role|service|image|net|subnet|router)$"
  68. if [[ -z "$name" || -z "$table" || ! $table =~ $table_regex ]] ; then
  69.     printf "[ERROR] Bad usage\n\n"
  70.     USAGE
  71.     exit 1
  72. fi
  73.  
  74. ### Run query
  75. out=$( ${query} ${table}-list 2>/dev/null |
  76.     awk -v name=$name '$4 == name {print $2}' )
  77.  
  78. ### Did we get an answer?
  79. if [[ -z "$out" ]] ; then
  80.     echo "No such $table $name" >&2
  81.     exit 1
  82. else
  83.     echo $out
  84. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement