Skip to content

Instantly share code, notes, and snippets.

@orimanabu
Last active November 28, 2018 04:55
Show Gist options
  • Save orimanabu/52cdc416b7ea058009c04e2b8ddfd9b6 to your computer and use it in GitHub Desktop.
Save orimanabu/52cdc416b7ea058009c04e2b8ddfd9b6 to your computer and use it in GitHub Desktop.
OpenStack Rest API call with cURL
#!/bin/bash
# This script is an example for calling OpenStack Rest API with cURL.
# First, gets project-scoped token, then gets instance information with the token.
# Prereq: jq
# Note that before running this script, ensure to set some environment variables.
# e.g.
# source ~/keystonerc_admin
# or
# source ~/overcloudrc
JSON_UNSCOPED_TOKEN=$(cat <<END
{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "${OS_USERNAME}",
"domain": {"id": "default"},
"password": "${OS_PASSWORD}"
}
}
}
}
}
END
)
JSON_PROJECT_SCOPED_TOKEN=$(cat <<END
{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "${OS_USERNAME}",
"domain": {"name": "${OS_USER_DOMAIN_NAME}"},
"password": "${OS_PASSWORD}"
}
}
},
"scope": {
"project": {
"name": "${OS_PROJECT_NAME}",
"domain": {"name": "${OS_PROJECT_DOMAIN_NAME}"}
}
}
}
}
END
)
token_curl_output=$(curl -s -i -H "Content-Type: application/json" -d "${JSON_PROJECT_SCOPED_TOKEN}" ${OS_AUTH_URL}/auth/tokens)
#echo "${token_curl_output}"
token=$(echo "${token_curl_output}" | awk '/^X-Subject-Token:/ {print $2}')
#echo "token: ${token}"
$/d')$(echo "${token_curl_output}" | sed -e '1,/^
#echo "json:"
endpoint=$(echo "${json}" | jq -r '.token.catalog[] | select(.type == "compute") | .endpoints[] | select(.interface == "public") | .url')
#echo "public endpoint: ${endpoint}"
curl -s -H "Content-Type: application/json" -H "X-Auth-Token: ${token}" ${endpoint}/servers/detail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment