Last active
March 13, 2025 12:21
RT REST2 API bash example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Simple example using RT's REST2 API to add an existing user to a group: | |
# Requires curl and jq to be installed. | |
CURL=/usr/bin/curl | |
RT_URL='https://<your_rt_server>' | |
RT_TOKEN='<your_token>' | |
[[ -z "$1" ]] && echo "usage: $0 <user> <group>" && exit 1 | |
[[ -z "$2" ]] && echo "usage: $0 <user> <group>" && exit 1 | |
RT_USER=$1 | |
RT_GROUP=$2 | |
# JSON for group query here. easier to embed variables etc: | |
generate_group_query() | |
{ | |
cat <<EOF | |
[ | |
{ "field": "Name", | |
"value": "${RT_GROUP}" | |
} | |
] | |
EOF | |
} | |
# Get user: | |
JSON_USER=$($CURL --silent "$RT_URL/REST/2.0/user/$RT_USER" \ | |
--request GET \ | |
--header "Authorization: token $RT_TOKEN" \ | |
--header "Content-Type: application/json" ) | |
ret=$? | |
# Bail if curl returned error: | |
[[ $ret > 0 ]] && echo "ERROR: curl retured error $ret" && exit 1 | |
#echo "$JSON_USER" | jq | |
# Bail if RT returned message. (Authentication failed, invalid query etc.) | |
json_message=$(echo "$JSON_USER" | jq -r '.message | values') | |
[[ -n "$json_message" ]] && echo "ERROR: JSON response: $json_message" && exit 1 | |
# get user id | |
user_id=$(echo $JSON_USER | jq '.id') | |
### could do something if no user_id here... | |
# Get group: | |
JSON_GROUP=$($CURL --silent "$RT_URL/REST/2.0/groups" \ | |
--request POST \ | |
--header "Authorization: token $RT_TOKEN" \ | |
--header "Content-Type: application/json" \ | |
--data-binary "$(generate_group_query)") | |
ret=$? | |
# Bail if curl returned error: | |
[[ $ret > 0 ]] && echo "ERROR: curl retured error $ret" && exit 1 | |
# Bail if RT returned message. (Authentication failed, invalid query etc.) | |
json_message=$(echo "$JSON_GROUP" | jq -r '.message | values') | |
[[ -n "$json_message" ]] && echo "ERROR: JSON response: $json_message" && exit 1 | |
# Get various properties from the JSON and do things: | |
total=$(echo "$JSON_GROUP" | jq -r '.total') # Total groups found | |
# If we got one match, get the group ID: | |
if [[ "$total" -eq 1 ]] ; then | |
group_id=$(echo "$JSON_GROUP" | jq -r '.items[].id') # Group id | |
fi | |
[[ $group_id -eq 0 ]] && echo "No id found for group $RT_GROUP" && exit 1 | |
[[ $total -gt 1 ]] && echo "Multiple ids for group $RT_GROUP" && exit 1 | |
# Is user already in group? | |
IN_GROUP_ID=$(echo $JSON_USER | jq -r --arg RT_GID "$group_id" '.Memberships[] | select(.id == $RT_GID) | .id') | |
[[ "$IN_GROUP_ID" == "$group_id" ]] && echo "User $RT_USER already in group $RT_GROUP ($IN_GROUP_ID)" && exit 1 | |
#echo "total: $total" | |
#echo "user_id: $user_id" | |
#echo "group_id: $group_id" | |
# Add user to group: | |
curl --silent "$RT_URL/REST/2.0/user/$RT_USER/groups" \ | |
--request PUT \ | |
--header "Authorization: token $RT_TOKEN" \ | |
--header "Content-Type: application/json" \ | |
--data-binary '['$group_id']' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment