# Stolen from https://raw.githubusercontent.com/monotek/elastic-helm-charts/21a4cc738b4a2cea35bac00e7a7bca9639634185/kibana/templates/configmap-dashboardimport.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: kibana-importscript
  labels:
    app: kibana
data:
  dashboardImport.sh: |
    #!/usr/bin/env bash
    #
    # kibana dashboard import script
    #
    DASHBOARD_FOLDER=/kibanadashboards
    SAVED_OBJECT_FOLDER=/kibana_saved_objects

    set -x

    for i in {1..60}; do
      curl -s localhost:5601/api/status | python -c 'import sys, json; print json.load(sys.stdin)["status"]["overall"]["state"]' 2> /dev/null | grep green > /dev/null && break || sleep 1
    done

    # Usage: import <dashboard|saved_object> <filename>
    import() {
      IMPORT_TYPE=$1

      KIBANA_ENDPOINT="localhost:5601/api/kibana/dashboards/import?force=true"

      if [ "$IMPORT_TYPE" = "saved_object" ]; then
        KIBANA_ENDPOINT="localhost:5601/api/saved_objects/_import?overwrite=true"
      fi

      DASHBOARD_FILE=$2
      if ! python -c 'import sys, json; print json.load(sys.stdin)' < "${DASHBOARD_FILE}" &> /dev/null ; then
        echo "${DASHBOARD_FILE} is not valid JSON, assuming it's an URL..."
        TMP_FILE="$(mktemp)"
        curl -s $(cat ${DASHBOARD_FILE}) > ${TMP_FILE}
        curl -v -s --connect-timeout 60 --max-time 60 -XPOST $KIBANA_ENDPOINT -H 'kbn-xsrf:true' -H 'Content-type:application/json' -d @${TMP_FILE}
        rm ${TMP_FILE}
      else
        echo "Valid JSON found in ${DASHBOARD_FILE}, importing..."
        if [ "$IMPORT_TYPE" = "saved_object" ]; then
          curl -v -s --connect-timeout 60 --max-time 60 -X POST "localhost:5601/api/saved_objects/_import" -H "kbn-xsrf: true" --form file=@${DASHBOARD_FILE}
        else
          curl -v -s --connect-timeout 60 --max-time 60 -XPOST $KIBANA_ENDPOINT -H 'kbn-xsrf:true' -H 'Content-type:application/json' -d @${DASHBOARD_FILE}
        fi
      fi

      if [ "$?" != "0" ]; then
        echo -e "\nImport of ${DASHBOARD_FILE} dashboard failed... Exiting..."
        exit 1
      else
        echo -e "\nImport of ${DASHBOARD_FILE} dashboard finished :-)"
      fi
    }



    for DASHBOARD_FILE in $(ls -d $DASHBOARD_FOLDER/*.ndjson); do
      echo -e "Importing ${DASHBOARD_FILE} dashboard..."

      import dashboard DASHBOARD_FILE
    done

    for SAVED_OBJECT_FILE in $(ls -d $SAVED_OBJECT_FOLDER/*.ndjson); do
      echo -e "Importing $SAVED_OBJECT_FILE saved object..."

      import saved_object $SAVED_OBJECT_FILE
    done
