Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
create_rabbitmq_user.sh
#!/bin/sh
API_USER="<USER>:<PASSWORD>"
API_URL="https://<SOME_SERVICE>.cloudamqp.com/api"
VHOST="<VHOST>"
__colored_echo()
{
printf "\033[$1m$2\033[0m\n"
}
__success_echo()
{
__colored_echo "0;32" "$@"
}
__error_echo()
{
__colored_echo "0;31" "$@"
}
__warning_echo()
{
__colored_echo "1;33" "$@"
}
__info_echo()
{
__colored_echo "0;37" "$@"
}
__input_echo()
{
__colored_echo "0;94" "$@"
}
initiate_new_user() {
__success_echo "======================="
__info_echo "==> Create new user <=="
__success_echo "======================="
read -p "Enter user name > " user
read -p "Enter password > " password
__success_echo "Creating user: $user with password: $password"
read -p "Looks good? (y/n)> " yn
case $yn in
[yY]* )
(create_user $user $password && create_permission $user) && on_success || on_failure
;;
* )
__error_echo "==> Damn! retrying"
initiate_new_user
;;
esac
}
create_user() {
__warning_echo "--> Creating user $1"
curl -v -u $API_USER -X PUT -d '{"password":"$2","tags":"administrator"}' $API_URL/users/$1
}
create_permission() {
__warning_echo "--> Creating permission for user $1"
curl -v -u $API_USER -X PUT -d '{"configure":".*","write":".*","read":".*"}' $API_URL/permissions/$VHOST/$1
}
on_success() {
__success_echo '==> Successfully created!'
initiate_new_user
}
on_failure() {
__error_echo '==> !!!Failed to create user'
initiate_new_user
}
initiate_new_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.