---
"Create data stream alias":
  - requires:
      cluster_features: ["gte_v7.14.0"]
      reason: "data streams aliases are available from 7.14.0"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [events-*] 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: [events-*]
          template:
            settings:
              index.number_of_replicas: 1
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: events-app1
  - is_true: acknowledged

  - do:
      index:
        index:  events-app1
        refresh: true
        body:
          '@timestamp': '2022-12-12'
          foo: bar

  - do:
      indices.create_data_stream:
        name: events-app2
  - is_true: acknowledged

  - do:
      index:
        index:  events-app2
        refresh: true
        body:
          '@timestamp': '2022-12-12'
          foo: bar

  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: events-app1
                alias: events
            - add:
                index: events-app2
                alias: events

  - do:
      indices.get_data_stream:
        name: "*"
  - match: { data_streams.0.name: events-app1 }
  - match: { data_streams.1.name: events-app2 }

  - do:
      indices.get_alias: {}

  - match: {events-app1.aliases.events: {}}
  - match: {events-app2.aliases.events: {}}

  - do:
      search:
        index: events
        body: { query: { match_all: {} } }
  - length:   { hits.hits: 2  }

---
"Create data stream alias with filter":
  - requires:
      cluster_features: ["gte_v7.15.0"]
      reason: "data stream alias filter supported was added in 7.15.0"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [log-*] 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: [ log-* ]
          template:
            settings:
              index.number_of_replicas: 1
          data_stream: { }

  - do:
      index:
        index: log-app1
        refresh: true
        body:
          '@timestamp': '2022-12-12'
          zone: 'a'

  - do:
      index:
        index: log-app1
        refresh: true
        body:
          '@timestamp': '2022-12-12'
          zone: 'b'

  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: log-app1
                alias: app1-zone-a
                filter:
                  term:
                    zone: 'a'
            - add:
                index: log-app1
                alias: app1-zone-b
                filter:
                  term:
                    zone: 'b'
            - add:
                index: log-app1
                alias: app1

  - do:
      indices.get_data_stream:
        name: "*"
  - match: { data_streams.0.name: log-app1 }

  - do:
      indices.get_alias: {}
  - match: {log-app1.aliases.app1: {}}
  - match: {log-app1.aliases.app1-zone-a.filter.term.zone: 'a'}
  - match: {log-app1.aliases.app1-zone-b.filter.term.zone: 'b'}

  - do:
      search:
        index: app1
  - length:   { hits.hits: 2  }

  - do:
      search:
        index: app1-zone-a
  - length:   { hits.hits: 1  }
  - do:
      search:
        index: app1-zone-b
  - length:   { hits.hits: 1  }

---
"Create data stream aliases using wildcard expression":
  - requires:
      cluster_features: ["gte_v7.14.1"]
      reason: "bugfix fixed from 7.14.1 and later"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [log-*] 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: [ log-* ]
          template:
            settings:
              index.number_of_replicas: 0
          data_stream: { }

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

  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: log-*
                alias: my-alias
  - is_true: acknowledged

  - do:
      indices.get_data_stream:
        name: "*"
  - match: { data_streams.0.name: log-foobar }

  - do:
      indices.get_alias: {}
  - match: {log-foobar.aliases.my-alias: {}}

---
"Fix IndexNotFoundException error when handling remove alias action":
  - requires:
      cluster_features: ["gte_v7.16.0"]
      reason: "Fix available from 7.16.0"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [log-*] 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: [ log-* ]
          template:
            settings:
              index.number_of_replicas: 0
          data_stream: { }

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

  - do:
      indices.create:
        index: test1
        body:
          aliases:
            test: {}

  - do:
      indices.create:
        index: test2
        body:
          aliases:
            test: {}

  - do:
      indices.create:
        index: test3
        body:
          aliases:
            test: {}

  - do:
      indices.get_alias:
        index: test*
  - match: { test1.aliases.test: { } }
  - match: { test2.aliases.test: { } }
  - match: { test3.aliases.test: { } }

  - do:
      indices.update_aliases:
        body:
          actions:
            - remove:
                index: '*'
                alias: test
  - is_true: acknowledged

  - do:
      indices.get_alias:
        index: test*
  - match: {test1.aliases: {}}
  - match: {test2.aliases: {}}
  - match: {test3.aliases: {}}

---
"Test get alias with non-matching data streams":
  - requires:
      cluster_features: ["gte_v8.12.2"]
      reason: "bugfix fixed from 8.12.1 and later"
      test_runner_features: allowed_warnings

  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [ds-*] 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: [ ds-* ]
          template:
            settings:
              index.number_of_replicas: 0
          data_stream: { }

  - do:
      indices.create_data_stream:
        name: ds-first
  - is_true: acknowledged

  - do:
      indices.create_data_stream:
        name: ds-second
  - is_true: acknowledged

  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: ds-first
                alias: my-alias
  - is_true: acknowledged

  - do:
      indices.get_alias:
        name: my-al*
  - match: {ds-first.aliases.my-alias: {}}

  - do:
      indices.get_alias:
        name: this-does-not-exist*
  - is_false: ds-first.aliases.my-alias
---
"Action Results with multiple matching aliases":
  - requires:
      cluster_features: ["gte_v8.14.0"]
      reason: "alias action results do not work until 8.14"
      test_runner_features: ["allowed_warnings"]
  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [log-*] 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: [ log-* ]
          template:
            settings:
              index.number_of_replicas: 0
          data_stream: { }
  - do:
      indices.create_data_stream:
        name: log-foobar
  - is_true: acknowledged
  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: log-foobar
                aliases: test_alias1
            - remove:
                index: log-foobar
                aliases: test_non_existing
                must_exist: false
  - is_true: errors
  - length: { action_results: 2 }
  - match: { action_results.0.status: 200 }
  - match: { action_results.0.action: { 'type': 'add', 'indices': ['log-foobar'], 'aliases': ['test_alias1'] } }
  - match: { action_results.0.error: null }
  - match: { action_results.1.status: 404 }
  - match: { action_results.1.action: { 'type': 'remove', 'indices': ['log-foobar'], 'aliases': ['test_non_existing'] } }
  - match: { action_results.1.error.type: aliases_not_found_exception }
---
"Single action result per action":
  - requires:
      cluster_features: [ "gte_v8.14.0" ]
      reason: "alias action results do not work until 8.14"
      test_runner_features: [ "allowed_warnings" ]
  - do:
      allowed_warnings:
        - "index template [my-template] has index patterns [log-*] 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: [ log-* ]
          template:
            settings:
              index.number_of_replicas: 0
          data_stream: { }
  - do:
      indices.create_data_stream:
        name: log-test-1
  - do:
      indices.create_data_stream:
        name: log-test-2
  - is_true: acknowledged
  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: log-test-*
                aliases: test_alias1
            - remove:
                index: log-test-*
                aliases: test_non_existing
                must_exist: false
  - is_true: errors
  - length: { action_results: 2 }
  - match: { action_results.0.status: 200}
  - match: { action_results.0.action: { 'type': 'add', 'indices': ['log-test-1', 'log-test-2'], 'aliases': ['test_alias1'] } }
  - match: { action_results.0.error: null }
  - match: { action_results.1.status: 404 }
  - match: { action_results.1.action: { 'type': 'remove', 'indices': ['log-test-1', 'log-test-2'], 'aliases': ['test_non_existing'] } }
  - match: { action_results.1.error.type: aliases_not_found_exception }
