apiVersion: v1
kind: ConfigMap
metadata:
  name: influx-monitoring-files
  namespace: monitoring
data:
  00_setup_dbrp: |

    #!/bin/bash
    # 

    # Create the DBRP policy 
    # This isn't _strictly_ necessary as more
    # recent versions auto-map, but as we're here
    # anyway lets be explicit
    influx v1 dbrp create \
    --bucket-id ${DOCKER_INFLUXDB_INIT_BUCKET_ID} \
    --db telegraf \
    --rp autogen \
    --default \
    --org ${DOCKER_INFLUXDB_INIT_ORG}

    # Create the write user
    influx v1 auth create \
      --username ${V1_WRITE_USERNAME} \
      --password ${V1_WRITE_PASSWORD} \
      --write-bucket ${DOCKER_INFLUXDB_INIT_BUCKET_ID} \
      --org ${DOCKER_INFLUXDB_INIT_ORG}

    influx v1 auth create \
      --username ${V1_READ_USERNAME} \
      --password ${V1_READ_PASSWORD} \
      --read-bucket ${DOCKER_INFLUXDB_INIT_BUCKET_ID} \
      --org ${DOCKER_INFLUXDB_INIT_ORG}

    if [ ! "$UPSTREAM_URL" == "" ]
    then
    # Configure EDR

    # Create the remote and capture the ID
    REMOTE_ID=`influx remote create \
    --name replicated \
    --remote-url "$UPSTREAM_URL" \
    --remote-api-token "$UPSTREAM_TOKEN" \
    --remote-org-id "$UPSTREAM_ORG" | tail -n1 | awk '{print $1}'`

    # Set up replication
    influx replication create \
      --name replicated_data \
      --remote-id $REMOTE_ID \
      --local-bucket-id "${DOCKER_INFLUXDB_INIT_BUCKET_ID}" \
      --remote-bucket "$UPSTREAM_BUCKET"

    fi