Skip to content

Instantly share code, notes, and snippets.

@tykeal
Created May 22, 2020 19:27
Show Gist options
  • Save tykeal/fc26be40c60fe9777bbcea0759b930c6 to your computer and use it in GitHub Desktop.
Save tykeal/fc26be40c60fe9777bbcea0759b930c6 to your computer and use it in GitHub Desktop.
OpenStack cloud "switcher"
# requires python yq (pip install --user yq)
# requires openstack installed into a virtual env, adjust path accordingling
# requires that you use ~/.config/openstack/secure.yaml for all the clouds that you
# have passwords for. Adjust appropriately
# openstack
cloud() {
# export our cloud
export OS_CLOUD="$1"
# activate openstack client
alias openstack=~/venv/current-openstack/bin/openstack
# shellcheck disable=SC1090
source <(openstack complete)
# Set the NETID
# shellcheck disable=SC2155
export NETID=$(openstack network list | awk "/${OS_CLOUD}/ {print \$2}")
}
_cloud_completions() {
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$(yq -y '.clouds | keys' ~/.config/openstack/secure.yaml | cut -c 3- | tr '\n' ' ' | sed 's/ $//')" -- "${COMP_WORDS[1]}"))
}
complete -F _cloud_completions cloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment