123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #! /bin/bash
- cache_dir=
- if [[ -v $XDG_CACHE_HOME ]]; then
- cache_dir="$XDG_CACHE_HOME"/openstack
- else
- cache_dir="$HOME/.cache/openstack"
- fi
- [[ -d "$cache_dir" ]] || mkdir -p "$cache_dir"
- token_response_file=$cache_dir/$(uriparse "$OS_AUTH_URL" | awk '/^hostText/ {print $2}')
- token_response_tmp="${token_response_file}-$(uuidgen)"
- get_password_token() {
- # usually -s and -L shouldn't be combined, but they are needed for devcloud due to keystone redirect
- curl -sL -D- -X POST \
- --json '{"auth": {
- "identity": {
- "methods": [
- "password"
- ],
- "password": {
- "user": {
- "domain": {
- "name": "'"$OS_USER_DOMAIN_NAME"'"
- },
- "name": "'"$OS_USERNAME"'",
- "password": "'"$OS_PASSWORD"'"
- }
- }
- },
- "scope": {
- "project": {
- "domain": {
- "name": "'"$OS_PROJECT_DOMAIN_NAME"'"
- },
- "name": "'"$OS_PROJECT_NAME"'",
- "id": "'"$OS_PROJECT_ID"'"
- }
- }
- }}' "${OS_AUTH_URL}/auth/tokens" \
- -o "$token_response_tmp" \
- | awk -v IGNORECASE=1 '/x-subject-token/ {gsub(/\x0d/,"",$0); print $2}'
- }
- get_v3applicationcredential_token() {
- curl -sL -D- -X POST \
- -H "Content-Type: application/json" \
- -d '{"auth": {
- "identity": {
- "methods": [
- "application_credential"
- ],
- "application_credential": {
- "id": "'"$OS_APPLICATION_CREDENTIAL_ID"'",
- "secret": "'"$OS_APPLICATION_CREDENTIAL_SECRET"'"
- }
- }
- }}' "${OS_AUTH_URL}auth/tokens" \
- -o "$token_response_tmp" \
- | awk -v IGNORECASE=1 '/x-subject-token/ {gsub(/\x0d/,"",$0); print $2}'
- }
- CURL_OS_TOKEN=
- expiry=0
- if [[ -f $token_response_file ]]; then
- expiry="$(jq -r '.token.expires_at' < $token_response_file | xargs date +%s --date)"
- fi
- now=$(date +%s)
- renewal_warning_seconds=600
- if [[ $expiry -gt $(( now + renewal_warning_seconds )) ]]; then
- CURL_OS_TOKEN="$(jq -r '.token.token' < $token_response_file)"
- else
- CURL_OS_TOKEN=$(get_"${OS_AUTH_TYPE}"_token)
- jq -r '.token += {"token": "'"${CURL_OS_TOKEN}"'"}' < "$token_response_tmp" > "$token_response_file"
- rm "$token_response_tmp"
- fi
- export CURL_OS_TOKEN
- get_catalog() {
- region_filter=
- if [[ -n $OS_REGION_NAME ]]; then
- region_filter=' and .region == "'"$OS_REGION_NAME"'"'
- fi
- jq -r '["-----", "-----", "-----", "-----"], (.token.catalog[] | [.id, .type, .name, ([.endpoints[] | select(.interface == "'"$OS_INTERFACE"'"'"$region_filter"') | .url] | join(" "))]) | @tsv' < "$token_response_file" \
- | column -s$'\t' -o' | ' --table-columns id,type,name,endpoints --table-wrap endpoints -t
- }
- get_service_endpoint() {
- iface="$OS_INTERFACE"
- if [[ -z $iface ]]; then
- iface=public
- fi
- if [[ -n $OS_REGION_NAME ]]; then
- region_filter=' and .region == "'"$OS_REGION_NAME"'"'
- fi
- jq -r '.token.catalog[] | select(.type == "'"$1"'") | .endpoints[] | select(.interface == "'"$iface"'"'"$region_filter"') | .url' < "$token_response_file"
- }
- check_project_id() {
- curl -s -o /dev/null -w "%{http_code}\n" \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- "${OS_AUTH_URL}/projects/$1"
- }
- check_user_id() {
- curl -s -o /dev/null -w "%{http_code}\n" \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- "${OS_AUTH_URL}/users/$1"
- }
- get_project_id() {
- # Converts a project name to the project id
- curl -s \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- "${OS_AUTH_URL}/projects?name=$1" \
- | jq -r '.projects[].id'
- }
- get_user_id() {
- # Converts a user name to the user id
- curl -s \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- "${OS_AUTH_URL}/users?name=$1" \
- | jq -r '.users[].id'
- }
- get_role_id() {
- curl -s \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- "${OS_AUTH_URL}/roles?name=$1" \
- | jq -r '.roles[].id'
- }
- get_aggregates() {
- local endpoint=$(get_service_endpoint compute)
- curl -s \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- -H "X-OpenStack-Nova-API-Version: 2.42" \
- "${endpoint}/os-aggregates"
- }
- get_instance() {
- local endpoint=$(get_service_endpoint compute)
- curl -s \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- -H "X-OpenStack-Nova-API-Version: 2.42" \
- "${endpoint}/servers/$1"
- }
- get_server_list_detailed() {
- local endpoint=$(get_service_endpoint compute)
- proj=$(coerce_project_id "$1")
- curl -s \
- -H "X-Auth-Token: $CURL_OS_TOKEN" \
- -H "X-OpenStack-Nova-API-Version: 2.42" \
- "${endpoint}/servers/detail?all_tenants=1&project_id=${proj}"
- }
- coerce_project_id() {
- # Many API queries require project id, but we want to be able to call them using project name.
- # This function takes either an id or a name; ids are passed through, while names are coerced to ids.
- case $(check_project_id $1) in
- 200)
- printf "%s\n" "$1"
- ;;
- *)
- get_project_id "$1"
- ;;
- esac
- }
- coerce_user_id() {
- # Many API queries require user id, but we want to be able to call them using user name.
- # This function takes either an id or a name; ids are passed through, while names are coerced to ids.
- case $(check_user_id $1) in
- 200)
- printf "%s\n" "$1"
- ;;
- *)
- get_user_id "$1"
- ;;
- esac
- }
- filterify_arguments() {
- # https://stackoverflow.com/a/34802471
- (( $# )) || return
- local res
- printf -v res '"%s",' "$@"
- printf "%s\n" "${res%,}"
- }
|