---
"Test apis that do not supported data streams":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [logs-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template
        body:
          index_patterns: [logs-*]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: logs-foobar
  - is_true: acknowledged

  - do:
      index:
        index:  logs-foobar
        refresh: true
        body:
          '@timestamp': '2020-12-12'
          foo: bar
  - match: {_index: "/\\.ds-logs-foobar-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/"}

  - do:
      search:
        index: logs-foobar
        body: { query: { match_all: {} } }
  - length:   { hits.hits: 1  }
  - match: { hits.hits.0._index: "/\\.ds-logs-foobar-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { hits.hits.0._source.foo: 'bar' }

  - do:
      catch: missing
      indices.delete:
        index: logs-foobar

  # close request will not fail but will not match any data streams
  - do:
      indices.close:
        index: logs-*
  - is_true: acknowledged
  - length: { indices: 0 }

  - do:
      indices.delete_data_stream:
        name: logs-foobar
  - is_true: acknowledged

---
"Prohibit clone on data stream's write index":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream1
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      catch: bad_request
      indices.clone:
        index: $idx0name
        target: "target"
        wait_for_active_shards: 1
        master_timeout: 10s
        body:
          settings:
            index.number_of_replicas: 0
            index.number_of_shards: 2

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

---
"Prohibit shrink on data stream's write index":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream1
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      catch: bad_request
      indices.shrink:
        index: $idx0name
        target: "target"
        wait_for_active_shards: 1
        master_timeout: 10s
        body:
          settings:
            index.number_of_replicas: 0

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

---
"Prohibit split on data stream's write index":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [simple-data-stream1] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [simple-data-stream1]
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream1
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      catch: bad_request
      indices.split:
        index: $idx0name
        target: "target"
        wait_for_active_shards: 1
        master_timeout: 10s
        body:
          settings:
            index.number_of_replicas: 0
            index.number_of_shards: 4

  - do:
      indices.delete_data_stream:
        name: simple-data-stream1
  - is_true: acknowledged

---
"Non append-only writes into a data stream":
  - requires:
      cluster_features: ["gte_v7.9.4"]
      reason: "validation bug only backported to 7.9.3"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template1] has index patterns [logs-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [logs-*]
          data_stream: {}

  - do:
      catch: bad_request
      index:
        index:  logs-foobar
        id: "1"
        body:
          '@timestamp': '2020-12-12'

  - do:
      bulk:
        body:
          - index:
              _index: logs-foobar
              _id: "1"
          - '@timestamp': '2020-12-12'
          - create:
              _index: logs-foobar
              _id: "1"
          - '@timestamp': '2020-12-12'
          - index:
              _index: logs-foobar
              _id: "1"
          - '@timestamp': '2020-12-12'
          - delete:
              _index: logs-foobar
              _id: "10"
  - match: { errors: true }
  - match: { items.0.index.status: 400 }
  - match: { items.0.index.error.type: illegal_argument_exception }
  - match: { items.0.index.error.reason: "only write ops with an op_type of create are allowed in data streams" }
  - match: { items.1.create.result: created }
  - match: { items.1.create._index: "/\\.ds-logs-foobar-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { items.2.index.status: 400 }
  - match: { items.2.index.error.type: illegal_argument_exception }
  - match: { items.2.index.error.reason: "only write ops with an op_type of create are allowed in data streams" }
  - match: { items.3.delete.status: 400 }
  - match: { items.3.delete.error.type: illegal_argument_exception }
  - match: { items.3.delete.error.reason: "only write ops with an op_type of create are allowed in data streams" }

  - do:
      indices.delete_data_stream:
        name: logs-foobar
  - is_true: acknowledged
