#!/bin/bash
set -e

# This script will test various methods for getting the target platform, tenant and password for robot authentication.

PUT=np-aws-lz-dsh
TUT=greenbox-dev
PFUT="$(dirname "$0")/$PUT.$TUT.pwd"
DUT="$(dirname "$0")/.dsh_cli_under_test"

# For this test to run the following is expected:
#
# * Tenant $TUT exists at platform $PUT
# * Password file $PFUT exists in the tests directory and contains the robot password for tenant $TUT at platform $PUT
# * You will be asked to enter your keyring password (use "Always Allow" or you'll have to provide it multiple times)

PUT_NA=prod-azure-dsh
TUT_NE=nope
PFUT_NAE="$(dirname "$0")/$PUT_NA.nope.pwd"
PWUT_NE=nope

export DSH_CLI_OUTPUT_FORMAT=quiet
export DSH_CLI_LOG_LEVEL=info
export DSH_CLI_VERBOSITY=high
export DSH_CLI_AUTHENTICATION=robot

function set_environment {
  rm -rf $DUT
  mkdir $DUT
  mkdir "$DUT/targets"
  export DSH_CLI_HOME=$DUT
  unset DSH_CLI_PLATFORM
  unset DSH_CLI_TENANT
  unset DSH_CLI_PASSWORD
  unset DSH_CLI_PASSWORD_FILE
}

function set_default_platform {
  touch "$DUT/settings.toml"
  echo "default-platform = \"$1\"" >> "$DUT/settings.toml"
}

function set_default_tenant {
  touch "$DUT/settings.toml"
  echo "default-tenant = \"$1\"" >> "$DUT/settings.toml"
}

echo "target platform from argument"
set_environment
set_default_platform $PUT_NA
export DSH_CLI_PLATFORM=$PUT_NA
dsh service list --platform $PUT --tenant $TUT --password-file $PFUT
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --platform $PUT --tenant $TUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "target platform from environment variable"
set_environment
set_default_platform $PUT_NA
export DSH_CLI_PLATFORM=$PUT
dsh service list --tenant $TUT --password-file $PFUT
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --tenant $TUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "target platform from settings"
set_environment
set_default_platform $PUT
CMD=$(echo "dsh service list --authentication robot --tenant $TUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "target tenant from argument"
set_environment
set_default_tenant $TUT_NE
export DSH_CLI_TENANT=$TUT_NE
dsh service list --platform $PUT --tenant $TUT --password-file $PFUT
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --platform $PUT --tenant $TUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "target tenant from environment variable"
set_environment
set_default_tenant $TUT_NE
export DSH_CLI_TENANT=$TUT
dsh service list --platform $PUT --password-file $PFUT
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --platform $PUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "target tenant from settings"
set_environment
set_default_tenant $TUT
CMD=$(echo "dsh service list --authentication robot --platform $PUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "password from password file argument"
set_environment
export DSH_CLI_PASSWORD=$PWUT_NE
export DSH_CLI_PASSWORD_FILE=$PFUT_NAE
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --platform $PUT --tenant $TUT --password-file $PFUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "password from password file environment variable"
set_environment
export DSH_CLI_PASSWORD=$PWUT_NE
export DSH_CLI_PASSWORD_FILE=$PFUT
dsh service list --platform $PUT --tenant $TUT
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --platform $PUT --tenant $TUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "password from password environment variable"
set_environment
PASSWORD="$(cat $PFUT)"
export DSH_CLI_PASSWORD=$PASSWORD
dsh service list --platform $PUT --tenant $TUT
export DSH_CLI_HOME=
CMD=$(echo "dsh service list --authentication robot --platform $PUT --tenant $TUT" | envsubst)
echo "$CMD"
eval "$CMD"

echo "password from keyring"
set_environment
CMD=$(echo "dsh service list --authentication robot --platform $PUT --tenant $TUT" | envsubst)
echo "$CMD"
eval "$CMD"
