setup:
  - skip:
      features: allowed_warnings
  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [simple-*] 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: [simple-*]
          data_stream: {}

---
"Delete backing index on data stream":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason: "data streams only supported in 7.9+"

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

  # rollover data stream to create new backing index
  - do:
      indices.rollover:
        alias: "simple-data-stream"

  - match: { old_index: "/\\.ds-simple-data-stream-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { new_index: "/\\.ds-simple-data-stream-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000002/" }
  - match: { rolled_over: true }
  - match: { dry_run: false }

  # save the backing index names for later use
  - do:
      indices.get_data_stream:
        name: simple-data-stream

  - set: { data_streams.0.indices.0.index_name: idx0name }
  - set: { data_streams.0.indices.1.index_name: idx1name }


  # ensure new index is created
  - do:
      indices.exists:
        index: $idx1name

  - is_true: ''

  - do:
      indices.delete:
        index: $idx0name

  - do:
      indices.exists:
        index: $idx0name

  - is_false: ''

  - do:
      indices.get_data_stream:
        name: "*"
  - match: { data_streams.0.name: simple-data-stream }
  - match: { data_streams.0.timestamp_field.name: '@timestamp' }
  - match: { data_streams.0.generation: 3 }
  - length: { data_streams.0.indices: 1 }
  - match: { data_streams.0.indices.0.index_name: '/\.ds-simple-data-stream-(\d{4}\.\d{2}\.\d{2}-)?000002/' }

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

---
"Attempt to delete write index on data stream is rejected":
  - requires:
      cluster_features: ["gte_v7.9.0"]
      reason:  "data streams available in 7.9+"

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

  # rollover data stream to create new backing index
  - do:
      indices.rollover:
        alias: "simple-data-stream"

  - match: { old_index: "/\\.ds-simple-data-stream-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000001/" }
  - match: { new_index: "/\\.ds-simple-data-stream-(\\d{4}\\.\\d{2}\\.\\d{2}-)?000002/" }
  - match: { rolled_over: true }
  - match: { dry_run: false }

  # ensure new index is created
  - do:
      indices.exists:
        index: .ds-simple-data-stream-*000002

  - is_true: ''

  - do:
      catch: bad_request
      indices.delete:
        index: .ds-simple-data-stream-*000002

  - do:
      indices.exists:
        index: .ds-simple-data-stream-*000002

  - is_true: ''

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