#
# Initialize Elastic with a schema and with an ingest pipeline for our API logs
#
apiVersion: batch/v1
kind: Job
metadata:
  name: es-initdata-job
spec:
  parallelism: 1
  completions: 1
  template:
    metadata:
      name: es-initdata-job
    spec:
      restartPolicy: Never
      initContainers:
        - name: wait-for-elasticearch
          image: alpine
          command:
            # Wait for Elasticsearch to come up
            [
              "sh",
              "-c",
              "for i in $(seq 1 300); do nc -zvw1 elasticsearch-svc 9200 && exit 0 || sleep 2; done; exit 1",
            ]
      containers:
        - name: es-initdata-job
          image: appropriate/curl
          env:
            - name: ELASTICSEARCH_PASSWORD
              value: Password1
          command:
            # Create the typed API logs schema, and also an ingestion pipeline to make minor adjustments to incoming data
            - /bin/sh
            - -c
            - |
              curl -k -u elastic:$ELASTICSEARCH_PASSWORD -X PUT https://elasticsearch-svc:9200/_template/apilogs -H 'Content-Type: application/json' -d '
              {
                "index_patterns": ["apilogs*"],
                "mappings": 
                {
                  "properties": 
                  {
                    "id": 
                    {
                      "type": "keyword"
                    },
                    "utcTime": 
                    {
                      "type": "date"
                    },
                    "apiName": 
                    {
                      "type": "keyword"
                    },
                    "operationName": 
                    {
                      "type": "keyword"
                    },
                    "hostName": 
                    {
                      "type": "keyword"
                    },
                    "method": 
                    {
                      "type": "keyword"
                    },
                    "path": 
                    {
                      "type": "keyword"
                    },
                    "resourceId": 
                    {
                      "type": "keyword"
                    },
                    "clientApplicationName": 
                    {
                      "type": "keyword"
                    },
                    "userId": 
                    {
                      "type": "keyword"
                    },
                    "statusCode": 
                    {
                      "type": "integer"
                    },
                    "errorCode": 
                    {
                      "type": "keyword"
                    },
                    "errorId": 
                    {
                      "type": "integer"
                    },
                    "millisecondsTaken": 
                    {
                      "type": "integer"
                    },
                    "millisecondsThreshold": 
                    {
                      "type": "integer"
                    },
                    "correlationId": 
                    {
                      "type": "keyword"
                    },
                    "sessionId": 
                    {
                      "type": "keyword"
                    },
                    "performance": {
                      "type": "object",
                      "enabled": false
                    },
                    "errorData": {
                      "type": "object",
                      "enabled": false
                    },
                    "infoData": {
                      "type": "object",
                      "enabled": false
                    }
                  }
                }
              }
              '
              curl -k -u elastic:$ELASTICSEARCH_PASSWORD -X PUT https://elasticsearch-svc:9200/_ingest/pipeline/apilogs -H 'Content-Type: application/json' -d '
              {
                "description": "Ingest Pipeline for API Logs",
                "processors": [
                  {
                    "drop": {
                      "if" : "ctx.apiName == null"
                    },
                    "script": {
                      "lang": "painless",
                      "description": "Use a client side unique id to prevent duplicates",
                      "source": "ctx._id = ctx.id"
                    },
                    "remove": {
                      "field": ["@timestamp", "log", "stream"],
                      "ignore_missing": true
                    }
                  }
                ]
              }
              '
