Skip to content

Instantly share code, notes, and snippets.

@vijayjt
Last active October 3, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayjt/9934b4591882893c870c6937740e0b26 to your computer and use it in GitHub Desktop.
Save vijayjt/9934b4591882893c870c6937740e0b26 to your computer and use it in GitHub Desktop.
HDInsight Ambari REST API examples - check out my repo AzureHDInsight for the full code
function check_ambari_user_exists()
{
USER_TO_CHECK=$1
# Get list of users in Ambari
USER_LIST=$(curl -u "$USERID:$PASSWD" -sS -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/users" | grep 'user_name' | cut -d":" -f2 | tr -d '"','',' ' )
for User in $( echo "$USER_LIST" | tr '\r' ' '); do
echo "-${User}-"
if [ "$User" == "$USER_TO_CHECK" ];then
echo 0
return
fi
done
# the user does not exist
echo 1
}
#end function check_ambari_user_exists
function check_ambari_group_exists()
{
GROUP_TO_CHECK=$1
# store the whole response with the status at the and
HTTP_RESPONSE=$(curl -u "$USERID:$PASSWD" --silent --write-out "HTTPSTATUS:%{http_code}" -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups")
# extract the status
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
#http_response=$(curl -u "$USERID:$PASSWD" -sS -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups" | grep 'group_name' | cut -d":" -f2 | tr -d '"','',' ' )
if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -le 299 ]]; then
# Get list of groups in Ambari
IFS=$'\n'
GROUP_LIST=$(curl -u "$USERID:$PASSWD" -sS -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups" | grep 'group_name' | cut -d":" -f2 | tr -d '"','',' ' )
unset IFS
for GROUP in "$GROUP_LIST"; do
#echo "$GROUP"
if [ "$GROUP" == "$GROUP_TO_CHECK" ];then
echo 0
return
fi
done
else
echo 1
return
fi
# the group does not exist
echo 1
}
#end function check_ambari_group_exists
function check_user_is_member_of_ambari_group()
{
USER_TO_CHECK=$1
GROUP_TO_CHECK=$2
# store the whole response with the status at the and
HTTP_RESPONSE=$(curl -u "$USERID:$PASSWD" --silent --write-out "HTTPSTATUS:%{http_code}" -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups/${GROUP_TO_CHECK}/members")
# extract the status
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
#http_response=$(curl -u "$USERID:$PASSWD" -sS -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups/${GROUP_TO_CHECK}/members" | grep 'user_name' | cut -d":" -f2 | tr -d '"','',' ' )
if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -le 299 ]]; then
# Get members of the group
IFS=$'\n'
USER_LIST=$(curl -u "$USERID:$PASSWD" -sS -G "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups/${GROUP_TO_CHECK}/members" | grep 'user_name' | cut -d":" -f2 | tr -d '"','',' ' )
unset IFS
for User in "$USER_LIST"; do
#echo "$User"
if [ "$User" == "$USER_TO_CHECK" ];then
echo 0
return
fi
done
else
echo 1
return
fi
# the user is not a member of the specified group
echo 1
}
#end function check_ambari_group_exists
# Creating a user in Ambari
curl -iv --write-out "HTTPSTATUS:%{http_code}" --output /dev/null --silent -u "$USERID:$PASSWD" -H "X-Requested-By: ambari" -X POST -d "{\"Users/user_name\":\"${username}\",\"Users/password\":\"${userpassword}\",\"Users/active\":\"true\",\"Users/admin\":\"false\"}" "http://${ACTIVEAMBARIHOST}:8080/api/v1/users")
# Create a group in Ambari
curl -iv --write-out "HTTPSTATUS:%{http_code}" --output /dev/null --silent -u "$USERID:$PASSWD" -H "X-Requested-By: ambari" -X POST -d "{\"Groups/group_name\":\"${ambarigroup}\"}" "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups
# Add a user to a group in Ambari
curl -iv --write-out "HTTPSTATUS:%{http_code}" --output /dev/null --silent -u "$USERID:$PASSWD" -H "X-Requested-By: ambari" -X POST -d "[{\"MemberInfo/user_name\":\"${username}\", \"MemberInfo/group_name\":\"${ambarigroup}\"}]" "http://${ACTIVEAMBARIHOST}:8080/api/v1/groups/${ambarigroup}/members
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment