#!/usr/bin/env bash


function gitops_db2u_database_help() {
  [[ -n "$1" ]] && echo_warning "$1"
  reset_colors
  cat << EOM
Usage:
  mas gitops_db2u_database [options]
Where ${COLOR_YELLOW}specified${TEXT_RESET} each option may also be defined by setting the appropriate environment variable.
When no options are specified on the command line, interactive-mode will be enabled by default.

GitOps Configuration:
  -d, --dir ${COLOR_YELLOW}GITOPS_WORKING_DIR${TEXT_RESET}           Directory for GitOps repository
  -a, --account-id ${COLOR_YELLOW}ACCOUNT_ID${TEXT_RESET}            Account name that the cluster belongs to
  -r, --region-id ${COLOR_YELLOW}REGION_ID${TEXT_RESET}              Region ID
  -c, --cluster-id ${COLOR_YELLOW}CLUSTER_ID${TEXT_RESET}            Cluster ID

IBM Maximo Application Suite:
      -m, --mas-instance-id ${COLOR_YELLOW}MAS_INSTANCE_ID${TEXT_RESET}   IBM Suite Maximo Application Suite Instance ID      
      --mas-app-id ${COLOR_YELLOW}MAS_APP_ID${TEXT_RESET}                 IBM Suite Maximo Application Suite App that will use this db2u instance      

IBM DB2U:
      --db2-dbname ${COLOR_YELLOW}DB2_DBNAME${TEXT_RESET}                                                     DB2 DB Name
      --db2-instance-name ${COLOR_YELLOW}DB2_INSTANCE_NAME${TEXT_RESET}                                       DB2 Instance Name
      --db2-version ${COLOR_YELLOW}DB2_VERSION${TEXT_RESET}                                                   DB2 Version
      --db2-tls-version ${COLOR_YELLOW}DB2_TLS_VERSION${TEXT_RESET}                                           TLS version to use in connection url
      --db2-table-org ${COLOR_YELLOW}DB2_TABLE_ORG${TEXT_RESET}                                               DB2 table org
      --db2-4k-device-support ${COLOR_YELLOW}DB2_4K_DEVICE_SUPPORT${TEXT_RESET}                               DB2 4k device support
      --db2-workload ${COLOR_YELLOW}DB2_WORKLOAD${TEXT_RESET}                                                 DB2 workload
      --db2-mln-count ${COLOR_YELLOW}DB2_MLN_COUNT${TEXT_RESET}                                               DB2 logical nodes count 
      --db2-num-pods ${COLOR_YELLOW}DB2_NUM_PODS${TEXT_RESET}                                                 DB2 number of pods
      --db2-meta-storage-class ${COLOR_YELLOW}DB2_META_STORAGE_CLASS${TEXT_RESET}                             DB2 meta storage class
      --db2-meta-storage-size ${COLOR_YELLOW}DB2_META_STORAGE_SIZE${TEXT_RESET}                               DB2 meta storage size
      --db2-meta-storage-accessmode ${COLOR_YELLOW}DB2_META_STORAGE_ACCESSMODE${TEXT_RESET}                   DB2 meta storage accessmode
      --db2-data-storage-class ${COLOR_YELLOW}DB2_DATA_STORAGE_CLASS${TEXT_RESET}                             DB2 data storage class
      --db2-data-storage-size ${COLOR_YELLOW}DB2_DATA_STORAGE_SIZE${TEXT_RESET}                               DB2 data storage size
      --db2-data-storage-accessmode ${COLOR_YELLOW}DB2_DATA_STORAGE_ACCESSMODE${TEXT_RESET}                   DB2 data storage accessmode
      --db2-backup-storage-class ${COLOR_YELLOW}DB2_BACKUP_STORAGE_CLASS${TEXT_RESET}                         DB2 backup storage class
      --db2-backup-storage-size ${COLOR_YELLOW}DB2_BACKUP_STORAGE_SIZE${TEXT_RESET}                           DB2 backup storage size
      --db2-backup-storage-accessmode ${COLOR_YELLOW}DB2_BACKUP_STORAGE_ACCESSMODE${TEXT_RESET}               DB2 backup storage accessmode
      --db2-logs-storage-class ${COLOR_YELLOW}DB2_LOGS_STORAGE_CLASS${TEXT_RESET}                             DB2 logs storage class
      --db2-logs-storage-size ${COLOR_YELLOW}DB2_LOGS_STORAGE_SIZE${TEXT_RESET}                               DB2 logs storage size
      --db2-logs-storage-accessmode ${COLOR_YELLOW}DB2_LOGS_STORAGE_ACCESSMODE${TEXT_RESET}                   DB2 logs storage accessmode
      --db2-audit-logs-storage-class ${COLOR_YELLOW}DB2_AUDIT_LOGS_STORAGE_CLASS${TEXT_RESET}                 DB2 audit logs storage class
      --db2-audit-logs-storage-size ${COLOR_YELLOW}DB2_AUDIT_LOGS_STORAGE_SIZE${TEXT_RESET}                   DB2 audit logs storage size
      --db2-audit-logs-storage-accessmode ${COLOR_YELLOW}DB2_AUDIT_LOGS_STORAGE_ACCESSMODE${TEXT_RESET}       DB2 audit logs storage accessmode
      --db2-temp-storage-class ${COLOR_YELLOW}DB2_TEMP_STORAGE_CLASS${TEXT_RESET}                             DB2 temp storage class
      --db2-temp-storage-size ${COLOR_YELLOW}DB2_TEMP_STORAGE_SIZE${TEXT_RESET}                               DB2 temp storage size
      --db2-temp-storage-accessmode ${COLOR_YELLOW}DB2_TEMP_STORAGE_ACCESSMODE${TEXT_RESET}                   DB2 temp storage accessmode
      --db2-archivelogs-storage-class ${COLOR_YELLOW}DB2_ARCHIVELOGS_STORAGE_CLASS${TEXT_RESET}               DB2 archivelogs storage class
      --db2-archivelogs-storage-size ${COLOR_YELLOW}DB2_ARCHIVELOGS_STORAGE_SIZE${TEXT_RESET}                 DB2 archivelogs storage size (default: 100Gi)
      --db2-archivelogs-storage-accessmode ${COLOR_YELLOW}DB2_ARCHIVELOGS_STORAGE_ACCESSMODE${TEXT_RESET}     DB2 archivelogs storage accessmode (default: ReadWriteMany)
      --db2-cpu-requests ${COLOR_YELLOW}DB2_CPU_REQUESTS${TEXT_RESET}                                         Define the Kubernetes CPU request for the Db2 pod.
      --db2-cpu-limits ${COLOR_YELLOW}DB2_CPU_LIMITS${TEXT_RESET}                                             Define the Kubernetes CPU limit for the Db2 pod.
      --db2-memory-requests ${COLOR_YELLOW}DB2_MEMORY_REQUESTS${TEXT_RESET}                                   Define the Kubernetes memory request for the Db2 pod.
      --db2-memory-limits ${COLOR_YELLOW}DB2_MEMORY_LIMITS${TEXT_RESET}                                       Define the Kubernetes memory limit for the Db2 pod.
      --db2-instance-registry-yaml ${COLOR_YELLOW}DB2_INSTANCE_REGISTRY_YAML${TEXT_RESET}                     Yaml file containing the db2ucluster instance registry section.
      --db2-instance-dbm-config-yaml ${COLOR_YELLOW}DB2_INSTANCE_DBM_CONFIG_YAML${TEXT_RESET}                 Yaml file containing the db2ucluster instance dbm config section.
      --db2-database-db-config-yaml ${COLOR_YELLOW}DB2_DATABASE_DB_CONFIG_YAML${TEXT_RESET}                   Yaml file containing the db2ucluster database db config section.
      --db2-addons-audit-config-yaml ${COLOR_YELLOW}DB2_ADDONS_AUDIT_CONFIG_YAML${TEXT_RESET}                 Yaml file containing the db2ucluster addons audit config section.
      --jdbc-route ${COLOR_YELLOW}JDBC_ROUTE${TEXT_RESET}                                                     By default routes are not exposed to public. To expose route, set this to public.
      --jdbc-connection-url-additional-params ${COLOR_YELLOW}JDBC_CONNECTION_URL_ADDITIONAL_PARAMS${TEXT_RESET}  Additional parameters for JDBC connection URL
      --db2-timezone ${COLOR_YELLOW}DB2_TIMEZONE${TEXT_RESET}                                                 DB2 DB Timezone (optional, DB2 default of "UTC" will be used if not specified)
      --db2-backup-notify-slack-url ${COLOR_YELLOW}DB2_BACKUP_NOTIFY_SLACK_URL${TEXT_RESET}                   Slack URL to notify DB2 backup failures
      --replica-db  ${COLOR_YELLOW}REPLICA_DB${TEXT_RESET}                                                    Flag to know that this is ReplicaDB instance
      --is-replica-task ${COLOR_YELLOW}IS_REPLICA_TASK${TEXT_RESET}                                           To check if this is invoked by replica task
      --skip-backup-notification ${COLOR_YELLOW}SKIP_BACKUP_NOTIFICATION${TEXT_RESET}                         Flag to track if slack and icd backup notifications are needed.
      --mas-annotations ${COLOR_YELLOW}MAS_ANNOTATIONS${TEXT_RESET}                                           MAS Annotations
      --auto-backup ${COLOR_YELLOW}AUTO_BACKUP${TEXT_RESET}                                                   Flag whether to install the Auto DB2 backup feature or not
      --manually-set-enhanced-hadr ${COLOR_YELLOW}MANUALLY_SET_ENHANCED_HADR${TEXT_RESET}                     Flag to indicate whether enhanced HADR was configured manually
      --allow-list ${COLOR_YELLOW}ALLOW_LIST${TEXT_RESET}                                                     List of IPs or CIDR range that will be whitelisted
      --cluster-domain ${COLOR_YELLOW}CLUSTER_DOMAIN${TEXT_RESET}                                             Cluster domain if any tls route needs to be created. Only needed if redhat cert-manager is not installed.

Secrets Manager:
      --secrets-path ${COLOR_YELLOW}SECRETS_PATH${TEXT_RESET}                     Secrets Manager path
      --secrets-key-seperator ${COLOR_YELLOW}SECRETS_KEY_SEPERATOR${TEXT_RESET}   Secrets Manager key seperator string      

Automatic GitHub Push:
  -P, --github-push ${COLOR_YELLOW}GITHUB_PUSH${TEXT_RESET}        Enable automatic push to GitHub
  -H, --github-host ${COLOR_YELLOW}GITHUB_HOST${TEXT_RESET}        GitHub Hostname for your GitOps repository
  -O, --github-org  ${COLOR_YELLOW}GITHUB_ORG${TEXT_RESET}         Github org for your GitOps repository
  -R, --github-repo ${COLOR_YELLOW}GITHUB_REPO${TEXT_RESET}        Github repo for your GitOps repository
  -B, --git-branch ${COLOR_YELLOW}GIT_BRANCH${TEXT_RESET}          Git branch to commit to of your GitOps repository
  -M, --git-commit-msg ${COLOR_YELLOW}GIT_COMMIT_MSG${TEXT_RESET}  Git commit message to use when committing to of your GitOps repository
  -S , --github-ssh  ${COLOR_YELLOW}GIT_SSH${TEXT_RESET}           Git ssh key path

Other Commands:
  -h, --help                                      Show this help message
EOM
  [[ -n "$1" ]] && exit 1 || exit 0
}

function gitops_db2u_database_noninteractive() {
  GITOPS_WORKING_DIR=$PWD/working-dir
  SECRETS_KEY_SEPERATOR="/"
  GIT_COMMIT_MSG="gitops-db2u-database commit"

  # TODO: will need to add explicit args to pipeline when we start using this code to deploy to MCSP
  export REGION=${REGION:-${SM_AWS_REGION}}
  export REGION_ID=${REGION_ID:-${REGION}}
  export CLUSTER_URL=${CLUSTER_URL:-"https://kubernetes.default.svc"} # use an in-cluster argocd worker (which fvtsaas does)

  while [[ $# -gt 0 ]]
  do
    key="$1"
    shift
    case $key in
      # GitOps Configuration
      -d|--dir)
        export GITOPS_WORKING_DIR=$1 && shift
        ;;
      -g|--gitops-version)
        echo "${COLOR_YELLOW}WARNING: the -g|--gitops-version parameter is deprecated; it has no effect and will be removed in a future release${COLOR_RESET}"
        shift
        ;;
      -a|--account-id)
        export ACCOUNT_ID=$1 && shift
        ;;
      -c|--cluster-id)
        export CLUSTER_ID=$1 && shift
        ;;
      -r|--region)
        export REGION_ID=$1 && shift
        ;;
      -u|--cluster-url)
        export CLUSTER_URL=$1 && shift
        ;;

      # MAS
      -m|--mas-instance-id)
        export MAS_INSTANCE_ID=$1 && shift
        ;;
      --mas-app-id)
        export MAS_APP_ID=$1 && shift
        ;;
      --mas-annotations)
        export MAS_ANNOTATIONS=$1 && shift
        ;;

      # Secrets Manager
      --secrets-path)
        export SECRETS_PATH=$1 && shift
        ;;
      --secrets-key-seperator)
        export SECRETS_KEY_SEPERATOR=$1 && shift
        ;;

      # DB2
      --db2-channel)
        export DB2_CHANNEL=$1 && shift
        ;;
      --db2-instance-name)
        export DB2_INSTANCE_NAME=$1 && shift
        ;;
      --db2-dbname)
        export DB2_DBNAME=$1 && shift
        ;;
      --db2-version)
        export DB2_VERSION=$1 && shift
        ;;
      --db2-tls-version)
        export DB2_TLS_VERSION=$1 && shift
        ;;
      --db2-table-org)
        export DB2_TABLE_ORG=$1 && shift
        ;;
      --db2-4k-device-support)
        export DB2_4K_DEVICE_SUPPORT=$1 && shift
        ;;
      --db2-workload)
        export DB2_WORKLOAD=$1 && shift
        ;;
      --db2-mln-count)
        export DB2_MLN_COUNT=$1 && shift
        ;;
      --db2-num-pods)
        export DB2_NUM_PODS=$1 && shift
        ;;
      --db2-meta-storage-class)
        export DB2_META_STORAGE_CLASS=$1 && shift
        ;;
      --db2-meta-storage-size)
        export DB2_META_STORAGE_SIZE=$1 && shift
        ;;
      --db2-meta-storage-accessmode) 
        export DB2_META_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-data-storage-class) 
        export DB2_DATA_STORAGE_CLASS=$1 && shift
        ;;
      --db2-data-storage-size)
        export DB2_DATA_STORAGE_SIZE=$1 && shift
        ;;
      --db2-data-storage-accessmode)
        export DB2_DATA_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-backup-storage-class)
        export DB2_BACKUP_STORAGE_CLASS=$1 && shift
        ;;
      --db2-backup-storage-size)
        export DB2_BACKUP_STORAGE_SIZE=$1 && shift
        ;;
      --db2-backup-storage-accessmode)
        export DB2_BACKUP_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-logs-storage-class)
        export DB2_LOGS_STORAGE_CLASS=$1 && shift
        ;;
      --db2-logs-storage-size)
        export DB2_LOGS_STORAGE_SIZE=$1 && shift
        ;;
      --db2-logs-storage-accessmode)
        export DB2_LOGS_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-audit-logs-storage-class)
        export DB2_AUDIT_LOGS_STORAGE_CLASS=$1 && shift
        ;;
      --db2-audit-logs-storage-size)
        export DB2_AUDIT_LOGS_STORAGE_SIZE=$1 && shift
        ;;
      --db2-audit-logs-storage-accessmode)
        export DB2_AUDIT_LOGS_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-temp-storage-class)
        export DB2_TEMP_STORAGE_CLASS=$1 && shift
        ;;
      --db2-temp-storage-size)
        export DB2_TEMP_STORAGE_SIZE=$1 && shift
        ;;
      --db2-temp-storage-accessmode)
        export DB2_TEMP_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-archivelogs-storage-class)
        export DB2_ARCHIVELOGS_STORAGE_CLASS=$1 && shift
        ;;
      --db2-archivelogs-storage-size)
        export DB2_ARCHIVELOGS_STORAGE_SIZE=$1 && shift
        ;;
      --db2-archivelogs-storage-accessmode)
        export DB2_ARCHIVELOGS_STORAGE_ACCESSMODE=$1 && shift
        ;;
      --db2-cpu-requests)
        export DB2_CPU_REQUESTS=$1 && shift
        ;;
      --db2-cpu-limits)
        export DB2_CPU_LIMITS=$1 && shift
        ;;
      --db2-memory-requests)
        export DB2_MEMORY_REQUESTS=$1 && shift
        ;;
      --db2-memory-limits)
        export DB2_MEMORY_LIMITS=$1 && shift
        ;;
      --db2-instance-registry-yaml)
        export DB2_INSTANCE_REGISTRY_YAML=$1 && shift
        ;;
      --db2-instance-dbm-config-yaml)
        export DB2_INSTANCE_DBM_CONFIG_YAML=$1 && shift
        ;;
      --db2-addons-audit-config-yaml)
        export DB2_ADDONS_AUDIT_CONFIG_YAML=$1 && shift
        ;;
      --db2-database-db-config-yaml)
        export DB2_DATABASE_DB_CONFIG_YAML=$1 && shift
        ;;
      --db2-affinity-key)
        export DB2_AFFINITY_KEY=$1 && shift
        ;;
      --db2-affinity-value)
        export DB2_AFFINITY_VALUE=$1 && shift
        ;;
      --db2-tolerate-key)
        export DB2_TOLERATE_KEY=$1 && shift
        ;;
      --db2-tolerate-value)
        export DB2_TOLERATE_VALUE=$1 && shift
        ;;
      --db2-tolerate-effect)
        export DB2_TOLERATE_EFFECT=$1 && shift
        ;;
      --jdbc-connection-url-additional-params)
        export JDBC_CONNECTION_URL_ADDITIONAL_PARAMS=$1 && shift
        ;;
      --jdbc-route)
        export JDBC_ROUTE=$1 && shift
        ;;
      --db2-timezone)
        export DB2_TIMEZONE=$1 && shift
        ;;
      # Optional DB2 Backup details
      --db2-backup-notify-slack-url)
        export DB2_BACKUP_NOTIFY_SLACK_URL=$1 && shift
        ;;
      --replica-db)
        export REPLICA_DB=$1 && shift
        ;;
      --is-replica-task)
        export IS_REPLICA_TASK=$1 && shift
        ;;
      --auto-backup)
        export AUTO_BACKUP=$1 && shift
        ;;
      --skip-backup-notification)
        export SKIP_BACKUP_NOTIFICATION=$1 && shift
        ;;
      --manually-set-enhanced-hadr)
        export MANUALLY_SET_ENHANCED_HADR=$1 && shift
        ;;
      --allow-list)
        export ALLOW_LIST=$1 && shift
        ;;
      --cluster-domain)
      export CLUSTER_DOMAIN=$1 && shift
        ;;

      # Automatic GitHub Push
      -P|--github-push)
        export GITHUB_PUSH=true
        ;;
      -H|--github-host)
        export GITHUB_HOST=$1 && shift
        ;;
      -O|--github-org)
        export GITHUB_ORG=$1 && shift
        ;;
      -R|--github-repo)
        export GITHUB_REPO=$1 && shift
        ;;
      -B|--git-branch)
        export GIT_BRANCH=$1 && shift
        ;;
      -M|--git-commit-msg)
        export GIT_COMMIT_MSG=$1 && shift
        ;;
      -S|--github-ssh)
        export GIT_SSH=$1 && shift
        ;;

      # Other Commands
      -h|--help)
        gitops_db2u_database_help
        ;;
      *)
        # unknown option
        echo -e "${COLOR_RED}Usage Error: Unsupported option \"${key}\"${COLOR_RESET}\n"
        gitops_db2u_database_help  "Usage Error: Unsupported option \"${key}\" "
        exit 1
        ;;
      esac
  done

  [[ -z "$SM_AWS_ACCESS_KEY_ID" ]] &&  gitops_db2u_database_help "SM_AWS_ACCESS_KEY_ID is not set"
  [[ -z "$SM_AWS_SECRET_ACCESS_KEY" ]] && gitops_db2u_database_help "SM_AWS_SECRET_ACCESS_KEY is not set"
  [[ -z "$SM_AWS_REGION" ]] && gitops_db2u_database_help "SM_AWS_REGION is not set"
  [[ -z "$SECRETS_PATH" ]] && gitops_db2u_database_help "SECRETS_PATH is not set"

  [[ -z "$GITOPS_WORKING_DIR" ]] && gitops_db2u_database_help "GITOPS_WORKING_DIR is not set"
  [[ -z "$ACCOUNT_ID" ]] && gitops_db2u_database_help "ACCOUNT_ID is not set"
  [[ -z "$REGION_ID" ]] && gitops_db2u_database_help "REGION_ID is not set"
  [[ -z "$CLUSTER_ID" ]] && gitops_db2u_database_help "CLUSTER_ID is not set"
  
  #db2u
  [[ -z "$DB2_VERSION" ]] && gitops_db2u_database_help "DB2_VERSION is not set"
  [[ -z "$DB2_META_STORAGE_CLASS" ]] && gitops_db2u_database_help "DB2_META_STORAGE_CLASS is not set"
  [[ -z "$DB2_DATA_STORAGE_CLASS" ]] && gitops_db2u_database_help "DB2_DATA_STORAGE_CLASS is not set"
  [[ -z "$DB2_BACKUP_STORAGE_CLASS" ]] && gitops_db2u_database_help "DB2_BACKUP_STORAGE_CLASS is not set"
  [[ -z "$DB2_LOGS_STORAGE_CLASS" ]] && gitops_db2u_database_help "DB2_LOGS_STORAGE_CLASS is not set"
  [[ -z "$MAS_INSTANCE_ID" ]] && gitops_db2u_database_help "MAS_INSTANCE_ID is not set"
  [[ -z "$MAS_APP_ID" ]] && gitops_db2u_database_help "MAS_APP_ID is not set"
  
  #MAS
  [[ -z "$MAS_INSTANCE_ID" ]] && gitops_db2u_database_help "MAS_INSTANCE_ID is not set"
  [[ -z "$MAS_APP_ID" ]] && gitops_db2u_database_help "MAS_APP_ID is not set"
  
  if [[ "$GITHUB_PUSH" == "true" ]]; then
    [[ -z "$GITHUB_HOST" ]] && gitops_db2u_database_help "GITHUB_HOST is not set"
    [[ -z "$GITHUB_ORG" ]] && gitops_db2u_database_help "GITHUB_ORG is not set"
    [[ -z "$GITHUB_REPO" ]] && gitops_db2u_database_help "GITHUB_REPO is not set"
    [[ -z "$GIT_BRANCH" ]] && gitops_db2u_database_help "GIT_BRANCH is not set"
  fi

}

function gitops_db2u_database() {
  # Take the first parameter off (it will be create-gitops)
  shift
  if [[ $# -gt 0 ]]; then
    gitops_db2u_database_noninteractive "$@"
  else
    echo "Not supported yet"
    exit 1
    gitops_db2u_database_interactive
  fi

  # catch errors
  set -o pipefail
  trap 'echo "[ERROR] Error occurred at $BASH_SOURCE, line $LINENO, exited with $?"; exit 1' ERR

  mkdir -p ${GITOPS_WORKING_DIR}
  GITOPS_INSTANCE_DIR=${GITOPS_WORKING_DIR}/${GITHUB_REPO}/${ACCOUNT_ID}/${CLUSTER_ID}/${MAS_INSTANCE_ID}
  CONFIGS_FILE="${GITOPS_INSTANCE_DIR}/ibm-db2u-databases.yaml"

  # NOTE: must align with lock branch name used by gitops_deprovision_db2u_database script
  # as both of these scrtipts modify the same file
  GIT_LOCK_BRANCH=$(git_lock_branch_name "gitops-db2u-database" "${ACCOUNT_ID}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}")

  export SECRET_NAME_DB2_BACKUP=${ACCOUNT_ID}${SECRETS_KEY_SEPERATOR}${CLUSTER_ID}${SECRETS_KEY_SEPERATOR}${MAS_INSTANCE_ID}${SECRETS_KEY_SEPERATOR}db2_backup
  export SECRET_NAME_ICD_AUTH_KEY=${ACCOUNT_ID}${SECRETS_KEY_SEPERATOR}${CLUSTER_ID}${SECRETS_KEY_SEPERATOR}icd

  #Defaults
  if [[ -z $DB2_TLS_VERSION ]]; then
    export DB2_TLS_VERSION=TLSv1.2
  fi
  if [[ -z $DB2_DBNAME ]]; then
    export DB2_DBNAME=BLUDB
  fi
  if [[ -z $DB2_TABLE_ORG ]]; then
    export DB2_TABLE_ORG=ROW
  fi 
  if [[ -z $DB2_4K_DEVICE_SUPPORT ]]; then
    export DB2_4K_DEVICE_SUPPORT=ON
  fi 
  if [[ -z $DB2_WORKLOAD ]]; then
    export DB2_WORKLOAD=ANALYTICS
  fi 
  if [[ -z $DB2_MLN_COUNT ]]; then
    export DB2_MLN_COUNT=1
  fi 
  if [[ -z $DB2_NUM_PODS ]]; then
    export DB2_NUM_PODS=1
  fi 
  if [[ -z $DB2_META_STORAGE_SIZE ]]; then
    export DB2_META_STORAGE_SIZE=20Gi
  fi 
  if [[ -z $DB2_META_STORAGE_ACCESSMODE ]]; then
    export DB2_META_STORAGE_ACCESSMODE=ReadWriteMany
  fi 
  if [[ -z $DB2_DATA_STORAGE_SIZE ]]; then
    export DB2_DATA_STORAGE_SIZE=100Gi
  fi 
  if [[ -z $DB2_DATA_STORAGE_ACCESSMODE ]]; then
    export DB2_DATA_STORAGE_ACCESSMODE=ReadWriteOnce
  fi 
  if [[ -z $DB2_BACKUP_STORAGE_SIZE ]]; then
    export DB2_BACKUP_STORAGE_SIZE=100Gi
  fi 
  if [[ -z $DB2_BACKUP_STORAGE_ACCESSMODE ]]; then
    export DB2_BACKUP_STORAGE_ACCESSMODE=ReadWriteMany
  fi 
  if [[ -z $DB2_LOGS_STORAGE_SIZE ]]; then
    export DB2_LOGS_STORAGE_SIZE=100Gi
  fi 
  if [[ -z $DB2_LOGS_STORAGE_ACCESSMODE ]]; then
    export DB2_LOGS_STORAGE_ACCESSMODE=ReadWriteOnce
  fi  
  if [[ -z $DB2_AUDIT_LOGS_STORAGE_SIZE ]]; then
    export DB2_AUDIT_LOGS_STORAGE_SIZE=100Gi
  fi 
  if [[ -z $DB2_AUDIT_LOGS_STORAGE_ACCESSMODE ]]; then
    export DB2_AUDIT_LOGS_STORAGE_ACCESSMODE=ReadWriteMany
  fi  
  if [[ -z $DB2_TEMP_STORAGE_SIZE ]]; then
    export DB2_TEMP_STORAGE_SIZE=100Gi
  fi 
  if [[ -z $DB2_TEMP_STORAGE_ACCESSMODE ]]; then
    export DB2_TEMP_STORAGE_ACCESSMODE=ReadWriteOnce
  fi
  if [[ -z $DB2_ARCHIVELOGS_STORAGE_SIZE ]]; then
    export DB2_ARCHIVELOGS_STORAGE_SIZE=100Gi
  fi 
  if [[ -z $DB2_ARCHIVELOGS_STORAGE_ACCESSMODE ]]; then
    export DB2_ARCHIVELOGS_STORAGE_ACCESSMODE=ReadWriteMany
  fi  
  if [[ -z $DB2_CPU_REQUESTS ]]; then
    export DB2_CPU_REQUESTS=4
  fi 
  if [[ -z $DB2_CPU_LIMITS ]]; then
    export DB2_CPU_LIMITS=6
  fi  
  if [[ -z $DB2_MEMORY_REQUESTS ]]; then
    export DB2_MEMORY_REQUESTS=8Gi
  fi 
  if [[ -z $DB2_MEMORY_LIMITS ]]; then
    export DB2_MEMORY_LIMITS=16Gi
  fi
  if [[ -z $DB2_DATABASE_DB_CONFIG_YAML ]]; then
    export DB2_DATABASE_DB_CONFIG="APPLHEAPSZ: '8192 AUTOMATIC'"
  fi
  if [[ -z $DB2_INSTANCE_REGISTRY_YAML ]]; then
    export DB2_INSTANCE_REGISTRY="DB2AUTH: 'OSAUTHDB,ALLOW_LOCAL_FALLBACK,PLUGIN_AUTO_RELOAD'
DB2_4K_DEVICE_SUPPORT: '${DB2_4K_DEVICE_SUPPORT}'
DB2_FMP_RUN_AS_CONNECTED_USER: 'NO'
DB2_WORKLOAD: '${DB2_WORKLOAD}'"
  fi
  if [[ -z $REPLICA_DB ]]; then
    export REPLICA_DB="false"
  fi
  if [[ -z $IS_REPLICA_TASK ]]; then
    export IS_REPLICA_TASK="false"
  fi
  if [[ -z $DB2_INSTANCE_NAME ]]; then
    if [[ $IS_REPLICA_TASK == 'true' ]]; then
      export DB2_INSTANCE_NAME=db2wh-${MAS_INSTANCE_ID}-${MAS_APP_ID}-sdb
    else
      export DB2_INSTANCE_NAME=db2wh-${MAS_INSTANCE_ID}-${MAS_APP_ID}
    fi
  fi
  if [[ -z $DB2_BACKUP_BUCKET_NAME ]]; then
    export DB2_BACKUP_BUCKET_NAME=${SECRET_NAME_DB2_BACKUP}#bucketName
  fi
  if [[ -z $SECRET_KEY_DB2_BACKUP_BUCKET_ACCESS_KEY ]]; then
    export SECRET_KEY_DB2_BACKUP_BUCKET_ACCESS_KEY=${SECRET_NAME_DB2_BACKUP}#accessKey
  fi
  if [[ -z $SECRET_KEY_DB2_BACKUP_BUCKET_SECRET_KEY ]]; then
    export SECRET_KEY_DB2_BACKUP_BUCKET_SECRET_KEY=${SECRET_NAME_DB2_BACKUP}#access_secret_key
  fi
  if [[ -z $DB2_BACKUP_BUCKET_ENDPOINT ]]; then
    export DB2_BACKUP_BUCKET_ENDPOINT=${SECRET_NAME_DB2_BACKUP}#endpointURL
  fi
  if [[ -z $SECRET_KEY_DB2_BACKUP_ICD_AUTH_KEY ]]; then
    export SECRET_KEY_DB2_BACKUP_ICD_AUTH_KEY=${SECRET_NAME_ICD_AUTH_KEY}#icd_auth_key
  fi
  if [[ -z $AUTO_BACKUP ]]; then
    export AUTO_BACKUP=false
  fi
  # Option to track icd and slack notifications, and if they need to be set in secret manager. The default behavior for commercial SaaS is false.
  if [[ -z $SKIP_BACKUP_NOTIFICATION ]]; then
    export SKIP_BACKUP_NOTIFICATION=false
  fi
  if [[ -z $MANUALLY_SET_ENHANCED_HADR ]]; then
    export MANUALLY_SET_ENHANCED_HADR=false
  fi
  export DB2_NAMESPACE="db2u-${MAS_INSTANCE_ID}"
  export DB2_DBNAME=${DB2_DBNAME:-"BLUDB"}
  export JDBC_ROUTE=${JDBC_ROUTE:-"default"}

  echo
  reset_colors
  echo_h2 "Review Settings "

  echo "${TEXT_DIM}"
  echo_h2 "Target" "    "
  echo_reset_dim "Account ID ............................ ${COLOR_MAGENTA}${ACCOUNT_ID}"
  echo_reset_dim "Region ID ............................. ${COLOR_MAGENTA}${REGION_ID}"
  echo_reset_dim "Cluster ID ............................ ${COLOR_MAGENTA}${CLUSTER_ID}"
  echo_reset_dim "Instance Config Directory ............. ${COLOR_MAGENTA}${GITOPS_INSTANCE_DIR}"
  reset_colors

  echo "${TEXT_DIM}"
  if [[ "$GITHUB_PUSH" == "true" ]]; then
    echo_h2 "GitOps Target" "    "
    echo_reset_dim "Automatic Push ........................ ${COLOR_GREEN}Enabled"
    echo_reset_dim "Working Directory ..................... ${COLOR_MAGENTA}${GITOPS_WORKING_DIR}"
    echo_reset_dim "Host .................................. ${COLOR_MAGENTA}${GITHUB_HOST}"
    echo_reset_dim "Organization .......................... ${COLOR_MAGENTA}${GITHUB_ORG}"
    echo_reset_dim "Repository ............................ ${COLOR_MAGENTA}${GITHUB_REPO}"
    echo_reset_dim "Branch ................................ ${COLOR_MAGENTA}${GIT_BRANCH}"
    echo_reset_dim "Lock Branch ........................... ${COLOR_MAGENTA}${GIT_LOCK_BRANCH}"
  else
    echo_h2 "GitOps Target" "    "
    echo_reset_dim "Automatic Push ........................ ${COLOR_RED}Disabled"
    echo_reset_dim "Working Directory ..................... ${COLOR_MAGENTA}${GITOPS_WORKING_DIR}"
  fi
  reset_colors

  echo "${TEXT_DIM}"
  echo_h2 "Secrets Manager" "    "
  echo_reset_dim "Secrets Path ............................ ${COLOR_MAGENTA}${SECRETS_PATH}"
  reset_colors

  echo "${TEXT_DIM}"
  echo_h2 "IBM Maximo Application Suite" "    "
  echo_reset_dim "MAS Instance ID ......................... ${COLOR_MAGENTA}${MAS_INSTANCE_ID}"
  echo_reset_dim "MAS Application ID ...................... ${COLOR_MAGENTA}${MAS_APP_ID}"
  reset_colors

  echo "${TEXT_DIM}"
  echo_h2 "DB2 Cluster Configuration" "    "
  echo_reset_dim "DB2 Namespace ................................. ${COLOR_MAGENTA}${DB2_NAMESPACE}"
  echo_reset_dim "db2-instance-name ............................. ${COLOR_MAGENTA}${DB2_INSTANCE_NAME}"
  echo_reset_dim "db2-dbname  ................................... ${COLOR_MAGENTA}${DB2_DBNAME}"
  echo_reset_dim "db2-version  .................................. ${COLOR_MAGENTA}${DB2_VERSION}"
  echo_reset_dim "db2-tls-version  .............................. ${COLOR_MAGENTA}${DB2_TLS_VERSION}"
  echo_reset_dim "db2-table-org  ................................ ${COLOR_MAGENTA}${DB2_TABLE_ORG}"
  echo_reset_dim "db2-4k-device-support  ........................ ${COLOR_MAGENTA}${DB2_4K_DEVICE_SUPPORT}"
  echo_reset_dim "db2-workload  ................................. ${COLOR_MAGENTA}${DB2_WORKLOAD}"
  echo_reset_dim "db2-mln-count  ................................ ${COLOR_MAGENTA}${DB2_MLN_COUNT}"
  echo_reset_dim "db2-num-pods  ................................. ${COLOR_MAGENTA}${DB2_NUM_PODS}"
  echo_reset_dim "db2-meta-storage-class  ....................... ${COLOR_MAGENTA}${DB2_META_STORAGE_CLASS}"
  echo_reset_dim "db2-meta-storage-size  ........................ ${COLOR_MAGENTA}${DB2_META_STORAGE_SIZE}"
  echo_reset_dim "db2-meta-storage-accessmode  .................. ${COLOR_MAGENTA}${DB2_META_STORAGE_ACCESSMODE}"
  echo_reset_dim "db2-data-storage-class  ....................... ${COLOR_MAGENTA}${DB2_DATA_STORAGE_CLASS}"
  echo_reset_dim "db2-data-storage-size  ........................ ${COLOR_MAGENTA}${DB2_DATA_STORAGE_SIZE}"
  echo_reset_dim "db2-data-storage-accessmode  .................. ${COLOR_MAGENTA}${DB2_DATA_STORAGE_ACCESSMODE}"
  echo_reset_dim "db2-backup-storage-class  ..................... ${COLOR_MAGENTA}${DB2_BACKUP_STORAGE_CLASS}"
  echo_reset_dim "db2-backup-storage-size  ...................... ${COLOR_MAGENTA}${DB2_BACKUP_STORAGE_SIZE}"
  echo_reset_dim "db2-backup-storage-accessmode  ................ ${COLOR_MAGENTA}${DB2_BACKUP_STORAGE_ACCESSMODE}"
  echo_reset_dim "db2-logs-storage-class  ....................... ${COLOR_MAGENTA}${DB2_LOGS_STORAGE_CLASS}"
  echo_reset_dim "db2-logs-storage-size  ........................ ${COLOR_MAGENTA}${DB2_LOGS_STORAGE_SIZE}"
  echo_reset_dim "db2-logs-storage-accessmode  .................. ${COLOR_MAGENTA}${DB2_LOGS_STORAGE_ACCESSMODE}"
  echo_reset_dim "db2-audit-logs-storage-class  ................. ${COLOR_MAGENTA}${DB2_AUDIT_LOGS_STORAGE_CLASS}"
  echo_reset_dim "db2-audit-logs-storage-size  .................. ${COLOR_MAGENTA}${DB2_AUDIT_LOGS_STORAGE_SIZE}"
  echo_reset_dim "db2-audit-logs-storage-accessmode  ............ ${COLOR_MAGENTA}${DB2_AUDIT_LOGS_STORAGE_ACCESSMODE}"
  echo_reset_dim "db2-temp-storage-class  ....................... ${COLOR_MAGENTA}${DB2_TEMP_STORAGE_CLASS}"
  echo_reset_dim "db2-temp-storage-size  ........................ ${COLOR_MAGENTA}${DB2_TEMP_STORAGE_SIZE}"
  echo_reset_dim "db2-temp-storage-accessmode  .................. ${COLOR_MAGENTA}${DB2_TEMP_STORAGE_ACCESSMODE}"
  echo_reset_dim "db2-archivelogs-storage-class  ................ ${COLOR_MAGENTA}${DB2_ARCHIVELOGS_STORAGE_CLASS}"
  echo_reset_dim "db2-archivelogs-storage-size  ................. ${COLOR_MAGENTA}${DB2_ARCHIVELOGS_STORAGE_SIZE}"
  echo_reset_dim "db2-archivelogs-storage-accessmode  ........... ${COLOR_MAGENTA}${DB2_ARCHIVELOGS_STORAGE_ACCESSMODE}"
  echo_reset_dim "mas-app-id  ................................... ${COLOR_MAGENTA}${MAS_APP_ID}"
  echo_reset_dim "db2-cpu-requests  ............................. ${COLOR_MAGENTA}${DB2_CPU_REQUESTS}"
  echo_reset_dim "db2-cpu-limits  ............................... ${COLOR_MAGENTA}${DB2_CPU_LIMITS}"
  echo_reset_dim "db2-memory-requests  .......................... ${COLOR_MAGENTA}${DB2_MEMORY_REQUESTS}"
  echo_reset_dim "db2-memory-limits  ............................ ${COLOR_MAGENTA}${DB2_MEMORY_LIMITS}"
  echo_reset_dim "db2-instance-registry-yaml .................... ${COLOR_MAGENTA}${DB2_INSTANCE_REGISTRY_YAML}"
  echo_reset_dim "db2-instance-dbm-config-yaml .................. ${COLOR_MAGENTA}${DB2_INSTANCE_DBM_CONFIG_YAML}"
  echo_reset_dim "db2-database-db-config-yaml ................... ${COLOR_MAGENTA}${DB2_DATABASE_DB_CONFIG_YAML}"
  echo_reset_dim "db2-addons-audit-config-yaml .................. ${COLOR_MAGENTA}${DB2_ADDONS_AUDIT_CONFIG_YAML}"
  echo_reset_dim "db2-affinity-key .............................. ${COLOR_MAGENTA}${DB2_AFFINITY_KEY}"
  echo_reset_dim "db2-affinity-value ............................ ${COLOR_MAGENTA}${DB2_AFFINITY_VALUE}"
  echo_reset_dim "db2-tolerate-key .............................. ${COLOR_MAGENTA}${DB2_TOLERATE_KEY}"
  echo_reset_dim "db2-tolerate-value ............................ ${COLOR_MAGENTA}${DB2_TOLERATE_VALUE}"
  echo_reset_dim "db2-tolerate-effect ........................... ${COLOR_MAGENTA}${DB2_TOLERATE_EFFECT}"
  echo_reset_dim "JDBC_ROUTE .................................... ${COLOR_MAGENTA}${JDBC_ROUTE}"
  echo_reset_dim "db2-timezone .................................. ${COLOR_MAGENTA}${DB2_TIMEZONE}"
  echo_reset_dim "replica-db .................................... ${COLOR_MAGENTA}${REPLICA_DB}"
  echo_reset_dim "is-replica-task ............................... ${COLOR_MAGENTA}${IS_REPLICA_TASK}"
  echo_reset_dim "mas-annotations ............................... ${COLOR_MAGENTA}${MAS_ANNOTATIONS}"
  echo_reset_dim "auto-backup ................................... ${COLOR_MAGENTA}${AUTO_BACKUP}"
  echo_reset_dim "skip-backup-notification ...................... ${COLOR_MAGENTA}${SKIP_BACKUP_NOTIFICATION}"
  echo_reset_dim "manually-set-enhanced-hadr .................... ${COLOR_MAGENTA}${MANUALLY_SET_ENHANCED_HADR}"
  echo_reset_dim "allow-list .................................... ${COLOR_MAGENTA}${ALLOW_LIST}"
  echo_reset_dim "Cluster Domain ................................ ${COLOR_MAGENTA}${CLUSTER_DOMAIN}"
  reset_colors


  AVP_TYPE=aws  # Support for IBM will be added later
  sm_login

  if [[ -z "${CLUSTER_DOMAIN}" ]]; then
    echo "CLUSTER_DOMAIN not specified, fetching from secrets manager"
    export SECRET_KEY_CLUSTER_DOMAIN=${ACCOUNT_ID}${SECRETS_KEY_SEPERATOR}${CLUSTER_ID}${SECRETS_KEY_SEPERATOR}cluster_domain#cluster_domain
  fi

  if [[ $AUTO_BACKUP == 'true' && $IS_REPLICA_TASK == 'false' && $SKIP_BACKUP_NOTIFICATION == 'false' ]]; then
    if [[ -n "$DB2_BACKUP_NOTIFY_SLACK_URL" && -n "$ICD_AUTH_KEY" ]]; then
      TAGS="[{\"Key\": \"source\", \"Value\": \"gitops_db2u_database\"}, {\"Key\": \"account\", \"Value\": \"${ACCOUNT_ID}\"}, {\"Key\": \"cluster\", \"Value\": \"${CLUSTER_ID}\"}]"
      sm_update_secret ${SECRET_NAME_ICD_AUTH_KEY} "{\"icd_auth_key\": \"${ICD_AUTH_KEY}\"}" "${TAGS}"
    else
      if [[ -z "$DB2_BACKUP_NOTIFY_SLACK_URL" ]]; then
        echo -e "DB2 Auto Backup feature is enabled but Slack Url is not configured"
      fi
      if [[ -z "$ICD_AUTH_KEY" ]]; then
        echo -e "DB2 Auto Backup feature is enabled but ICD_AUTH_KEY is not available as environment properties in the apps pipeline"
      fi
      exit 1
    fi
  else
    echo
    echo_h2 "Optional feature - DB2 automated backup is not enabled"
  fi

  TEMP_DIR=$GITOPS_WORKING_DIR/tmp-db2u-database
  mkdir -p $TEMP_DIR

  # Getting provided db2cluster configs
  # ---------------------------------------------------------------------------
  if [[ -n "$DB2_DATABASE_DB_CONFIG_YAML" ]]; then
    echo
    echo_h2 "Using db2 database db config provided for $MAS_APP_ID at $DB2_DATABASE_DB_CONFIG_YAML"
    export DB2_DATABASE_DB_CONFIG=$(cat ${DB2_DATABASE_DB_CONFIG_YAML})
  fi
  if [[ -n "$DB2_INSTANCE_DBM_CONFIG_YAML" ]]; then
    echo
    echo_h2 "Using db2 instance dm config provided for $MAS_APP_ID at $DB2_INSTANCE_DBM_CONFIG_YAML"
    export DB2_INSTANCE_DBM_CONFIG=$(cat ${DB2_INSTANCE_DBM_CONFIG_YAML})
  fi
  if [[ -n "$DB2_ADDONS_AUDIT_CONFIG_YAML" ]]; then
    echo
    echo_h2 "Using db2 addons audit config provided for $MAS_APP_ID at $DB2_ADDONS_AUDIT_CONFIG_YAML"
    export DB2_ADDONS_AUDIT_CONFIG=$(cat ${DB2_ADDONS_AUDIT_CONFIG_YAML})
  fi
  if [[ -n "$DB2_INSTANCE_REGISTRY_YAML" ]]; then
    echo
    echo_h2 "Using db2 instance registry config provided for $MAS_APP_ID at $DB2_INSTANCE_REGISTRY_YAML"
    export DB2_INSTANCE_REGISTRY=$(cat ${DB2_INSTANCE_REGISTRY_YAML})
  fi

  # Optional DB backup configs
  # ---------------------------------------------------------------------------
  if [[ $AUTO_BACKUP == 'true' && -n "$DB2_BACKUP_NOTIFY_SLACK_URL" && -n "$ICD_AUTH_KEY" && $IS_REPLICA_TASK == 'false' && $SKIP_BACKUP_NOTIFICATION == 'false' ]]; then
    echo
    echo_h2 "Optional DB backup configurations are provided"
    sm_verify_secret_exists $SECRET_NAME_ICD_AUTH_KEY "icd_auth_key"
    sm_verify_secret_exists $SECRET_NAME_DB2_BACKUP "bucketName,accessKey,access_secret_key,endpointURL"
    echo_h2 "Auto DB backup feature is enabled"
    export BACKUP=true
  fi

  # DB backup configs when backup notifications are skipped
  # ---------------------------------------------------------------------------
  if [[ $AUTO_BACKUP == 'true' && $SKIP_BACKUP_NOTIFICATION == 'true' ]]; then
    echo
    echo_h2 "Backup notifications are skipped. Verifying backup secret exists."
    sm_verify_secret_exists $SECRET_NAME_DB2_BACKUP "bucketName,accessKey,access_secret_key,endpointURL"
    echo_h2 "Auto DB backup feature is enabled"
    export BACKUP=true
  fi

  if [ -z $GIT_SSH ]; then
    export GIT_SSH=false
  fi


  # Set and Validate App Names
  # ---------------------------------------------------------------------------
  INSTANCE_ROOT_APP="instance.${CLUSTER_ID}.${MAS_INSTANCE_ID}"
  DB2_DATABASE_APP="db2-db.${CLUSTER_ID}.${MAS_INSTANCE_ID}.${MAS_APP_ID}"

  validate_app_name "${INSTANCE_ROOT_APP}"
  validate_app_name "${DB2_DATABASE_APP}"


  # Look for generated(<type>) directives on storage class names
  # ---------------------------------------------------------------------------

  export STORAGE_CLASS_DEFINITIONS

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def "${DB2_META_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_META_STORAGE_CLASS=$(generate_storage_class_name   "${DB2_META_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def "${DB2_DATA_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_DATA_STORAGE_CLASS=$(generate_storage_class_name   "${DB2_DATA_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def "${DB2_BACKUP_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_BACKUP_STORAGE_CLASS=$(generate_storage_class_name "${DB2_BACKUP_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def "${DB2_LOGS_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_LOGS_STORAGE_CLASS=$(generate_storage_class_name   "${DB2_LOGS_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def     "${DB2_AUDIT_LOGS_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_AUDIT_LOGS_STORAGE_CLASS=$(generate_storage_class_name "${DB2_AUDIT_LOGS_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def "${DB2_TEMP_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_TEMP_STORAGE_CLASS=$(generate_storage_class_name   "${DB2_TEMP_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  STORAGE_CLASS_DEFINITIONS=$(generate_storage_class_def      "${DB2_ARCHIVELOGS_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" "${STORAGE_CLASS_DEFINITIONS}") || exit 1
  DB2_ARCHIVELOGS_STORAGE_CLASS=$(generate_storage_class_name "${DB2_ARCHIVELOGS_STORAGE_CLASS}" "${CLUSTER_ID}" "${MAS_INSTANCE_ID}" ${MAS_APP_ID} "db2" )

  # Sort by top-level keys to guarantee idempotent output
  STORAGE_CLASS_DEFINITIONS=$(echo "${STORAGE_CLASS_DEFINITIONS}" | yq e 'sortKeys(.)' -)


  # Clone github target repo
  # ---------------------------------------------------------------------------
  if [ "$GITHUB_PUSH" == "true" ]; then
    # only create the lock branch if we plan to actually push changes to git
    clone_and_lock_target_git_repo  "${GITHUB_HOST}" "${GITHUB_ORG}" "${GITHUB_REPO}" "${GIT_BRANCH}" "${GITOPS_WORKING_DIR}" "${GIT_SSH}" "${GIT_LOCK_BRANCH}"
  fi
  mkdir -p ${GITOPS_INSTANCE_DIR}

  # Generate ArgoApps
  # ---------------------------------------------------------------------------
  echo
  echo_h2 "Generating DB2U Database Configuraton"
  echo

  # If the file doesn't exist, create a blank one
  if ! [ -f ${CONFIGS_FILE} ]; then
    jinjanate_commmon $CLI_DIR/templates/gitops/appset-configs/cluster/instance/db2-databases/ibm-db2u-database-common.yaml.j2 $CONFIGS_FILE
  fi

  # Remove any existing config with this name
  yq 'del(.ibm_db2u_databases[] | select(.db2_instance_name == "'${DB2_INSTANCE_NAME}'"))' $CONFIGS_FILE > $TEMP_DIR/configs.yaml

  # Create new file
  echo -n "" > ${TEMP_DIR}/newconfig.yaml
  # Render the appropriate template for the config into a new file. For standby database, it will be generated only if it is a replica task and replica is enabled.
  if [[ (${IS_REPLICA_TASK} == 'false') || (${IS_REPLICA_TASK} == 'true' && ${REPLICA_DB} == 'true') ]]; then
    jinjanate_commmon $CLI_DIR/templates/gitops/appset-configs/cluster/instance/db2-databases/ibm-db2u-database.yaml.j2 | yq '{"ibm_db2u_databases": [] + .}' > ${TEMP_DIR}/newconfig.yaml
  fi

  # Merge the two files
  yq eval-all '. as $item ireduce ({}; . *+ $item)' $TEMP_DIR/configs.yaml ${TEMP_DIR}/newconfig.yaml > $CONFIGS_FILE

  # sort the configs by db2_instance_name.
  # This way, we maintain the same ordering of configs in the file (even though we may have deleted and recreated a config if it's an update)
  # This eliminates confusing commits to gitops-envs and allows us to determine if anything has actually changed
  yq -i '.ibm_db2u_databases |= sort_by(.db2_instance_name)' $CONFIGS_FILE

  echo_h2 "Updated configuration file (${CONFIGS_FILE})"
  if [ -f ${CONFIGS_FILE} ]; then
    cat $CONFIGS_FILE
  else
    echo "<file was deleted>"
  fi

  # Commit and push to github target repo
  # ---------------------------------------------------------------------------
  if [ "$GITHUB_PUSH" == "true" ]; then
    echo
    echo_h2 "Commit and push changes to GitHub repo $GITHUB_ORG $GITHUB_REPO"
    save_and_unlock_target_git_repo "${GITHUB_REPO}" "${GIT_BRANCH}" "${GITOPS_WORKING_DIR}" "${GIT_COMMIT_MSG}" "${GIT_LOCK_BRANCH}" DB2_CONFIG_CHANGED
    remove_git_repo_clone $GITOPS_WORKING_DIR/$GITHUB_REPO
  fi

  rm -rf $TEMP_DIR

}
