setup:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: introduced in 8.1.0

  - do:
      indices.create:
        index: tsdb
        body:
          settings:
            index:
              mode: time_series
              routing_path: [metricset, k8s.pod.uid]
              time_series:
                start_time: 2021-04-28T00:00:00Z
                end_time: 2021-04-29T00:00:00Z
              number_of_replicas: 0
              number_of_shards: 2
          mappings:
            properties:
              "@timestamp":
                type: date
              metricset:
                type: keyword
                time_series_dimension: true
              k8s:
                properties:
                  pod:
                    properties:
                      uid:
                        type: keyword
                        time_series_dimension: true
                      name:
                        type: keyword
                      ip:
                        type: ip
                      network:
                        properties:
                          tx:
                            type: long
                          rx:
                            type: long

  - do:
      bulk:
        refresh: true
        index: tsdb
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:03.142Z", "metricset": "pod", "k8s": {"pod": {"name": "cow", "uid":"1c4fc7b8-93b7-4ba8-b609-2a48af2f8e39", "ip": "10.10.55.4", "network": {"tx": 1434521831, "rx": 530575198}}}}'

---
update tag field:
  - do:
      update_by_query:
        index: tsdb
        refresh: true
        body:
          script:
            lang: painless
            source: ctx._source.k8s.pod.ip = "10.10.55.5"

  - match: {updated: 1}
  - match: {version_conflicts: 0}
  - match: {batches: 1}
  - match: {failures: []}
  - match: {throttled_millis: 0}
  - gte: { took: 0 }

  - do:
      search:
        index: tsdb
  - match: {hits.total.value: 1}
  - match: {hits.hits.0._source.k8s.pod.ip: 10.10.55.5}

---
update dimension field:
  # TODO better error message
  - do:
      catch: bad_request
      update_by_query:
        index: tsdb
        body:
          script:
            lang: painless
            source: ctx._source.k8s.pod.uid = "12342134"

  - match: {updated: 0}
  - match: {version_conflicts: 0}
  - match: {batches: 1}
  - match: {throttled_millis: 0}
  - gte:   { took: 0 }
  - match: {failures.0.cause.caused_by.reason: /_id\ must\ be\ unset\ or\ set\ to\ .+/}

---
update timestamp:
  # TODO better error message
  - do:
      catch: bad_request
      update_by_query:
        index: tsdb
        body:
          script:
            lang: painless
            source: ctx._source["@timestamp"] = "2021-04-28T18:50:33.142Z"

  - match: {updated: 0}
  - match: {version_conflicts: 0}
  - match: {batches: 1}
  - match: {throttled_millis: 0}
  - gte:   { took: 0 }
  - match: {failures.0.cause.caused_by.reason: /_id\ must\ be\ unset\ or\ set\ to\ .+/}
