apiVersion: v1
kind: ConfigMap
metadata:
  name: add-mysql-user-configmap
  namespace: mysql
data:
  add-mysql-user:  |
    #!/bin/bash

    userName="${1}"
    userPassword="${2}"
    userDatabase="${3}"

    # Create User
    echo "Creating user ${userName}"
    mysql -u root -p"$(cat /tmp/mysql-root-password-file/password)" -e "CREATE USER IF NOT EXISTS '${userName}'@'%' IDENTIFIED BY '${userPassword}'"

    # Create User's DB
    echo "Creating DB ${userDatabase}"
    mysql -u root -p"$(cat /tmp/mysql-root-password-file/password)" -e "CREATE DATABASE IF NOT EXISTS ${userDatabase}"

    # Grant DB permissions
    echo "Granting permissions to user ${userName} to database: ${userDatabase}"
    mysql -u root -p"$(cat /tmp/mysql-root-password-file/password)" -e "GRANT ALL PRIVILEGES ON ${userDatabase}.* TO '${userName}'@'%'"
