setup:
  - skip:
      features: ["allowed_warnings", "headers"]
  - do:
      indices.create:
        index: foo
        body:
          mappings:
            properties:
              the_field:
                type: date
              value_field:
                type: integer

  - do:
      indices.create:
        index: foo2
        body:
          mappings:
            properties:
              the_field:
                type: date
              value_field:
                type: integer

  - do:
      indices.create:
        index: foo3
        body:
          mappings:
            properties:
              the_field:
                type: date
              value_field:
                type: integer

  - do:
      indices.create:
        index: dummy-rollup-index
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            _meta:
              _rollup:
                my-id: { }

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }

  - do:
      indices.delete:
        index: dummy-rollup-index

---
"Verify one job caps by rollup index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_rollup_index_caps:
        index: "foo_rollup"

  - match:
      foo_rollup:
        rollup_jobs:
          - job_id: "foo"
            rollup_index: "foo_rollup"
            index_pattern: "foo"
            fields:
              the_field:
                - agg: "date_histogram"
                  calendar_interval: "1h"
                  time_zone: "UTC"
              value_field:
                - agg: "min"
                - agg: "max"
                - agg: "sum"

---
"Verify two job caps by rollup index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }
  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_rollup_index_caps:
        index: "foo_rollup"

  - match:
      foo_rollup:
        rollup_jobs:
          - job_id: "foo"
            rollup_index: "foo_rollup"
            index_pattern: "foo"
            fields:
              the_field:
                - agg: "date_histogram"
                  calendar_interval: "1h"
                  time_zone: "UTC"
              value_field:
                - agg: "min"
                - agg: "max"
                - agg: "sum"
          - job_id: "foo2"
            rollup_index: "foo_rollup"
            index_pattern: "foo"
            fields:
              the_field:
                - agg: "date_histogram"
                  calendar_interval: "1h"
                  time_zone: "UTC"
              value_field:
                - agg: "min"
                - agg: "max"
                - agg: "sum"


---
"Verify two different job caps by rollup index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo2",
            "rollup_index": "foo_rollup2",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }
  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_rollup_index_caps:
        index: "foo_rollup"

  - match:
      foo_rollup:
        rollup_jobs:
          - job_id: "foo"
            rollup_index: "foo_rollup"
            index_pattern: "foo"
            fields:
              the_field:
                - agg: "date_histogram"
                  calendar_interval: "1h"
                  time_zone: "UTC"
              value_field:
                - agg: "min"
                - agg: "max"
                - agg: "sum"

---
"Verify all job caps by rollup index":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo2",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }
  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo3
        body:  >
          {
            "index_pattern": "foo3",
            "rollup_index": "foo_rollup2",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_rollup_index_caps:
        index: "_all"

  - match:
      $body:
        foo_rollup:
          rollup_jobs:
            - job_id: "foo"
              rollup_index: "foo_rollup"
              index_pattern: "foo"
              fields:
                the_field:
                  - agg: "date_histogram"
                    calendar_interval: "1h"
                    time_zone: "UTC"
                value_field:
                  - agg: "min"
                  - agg: "max"
                  - agg: "sum"
            - job_id: "foo2"
              rollup_index: "foo_rollup"
              index_pattern: "foo2"
              fields:
                the_field:
                  - agg: "date_histogram"
                    calendar_interval: "1h"
                    time_zone: "UTC"
                value_field:
                  - agg: "min"
                  - agg: "max"
                  - agg: "sum"
        foo_rollup2:
          rollup_jobs:
            - job_id: "foo3"
              rollup_index: "foo_rollup2"
              index_pattern: "foo3"
              fields:
                the_field:
                  - agg: "date_histogram"
                    calendar_interval: "1h"
                    time_zone: "UTC"
                value_field:
                  - agg: "min"
                  - agg: "max"
                  - agg: "sum"

---
"Verify job caps by rollup index comma delimited list":

- do:
    allowed_warnings:
      - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
    rollup.put_job:
      id: foo2
      body:  >
        {
          "index_pattern": "foo2",
          "rollup_index": "foo_rollup",
          "cron": "*/30 * * * * ?",
          "page_size" :10,
          "groups" : {
            "date_histogram": {
              "field": "the_field",
              "calendar_interval": "1h"
            }
          },
          "metrics": [
            {
              "field": "value_field",
              "metrics": ["min", "max", "sum"]
            }
          ]
        }
- do:
    allowed_warnings:
      - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
    rollup.put_job:
      id: foo3
      body:  >
        {
          "index_pattern": "foo3",
          "rollup_index": "foo_rollup2",
          "cron": "*/30 * * * * ?",
          "page_size" :10,
          "groups" : {
            "date_histogram": {
              "field": "the_field",
              "calendar_interval": "1h"
            }
          },
          "metrics": [
            {
              "field": "value_field",
              "metrics": ["min", "max", "sum"]
            }
          ]
        }

- do:
    allowed_warnings:
      - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
    rollup.get_rollup_index_caps:
      index: "foo_rollup2,foo_rollup"

- match:
    $body:
      foo_rollup:
        rollup_jobs:
        - job_id: "foo"
          rollup_index: "foo_rollup"
          index_pattern: "foo"
          fields:
            the_field:
            - agg: "date_histogram"
              calendar_interval: "1h"
              time_zone: "UTC"
            value_field:
            - agg: "min"
            - agg: "max"
            - agg: "sum"
        - job_id: "foo2"
          rollup_index: "foo_rollup"
          index_pattern: "foo2"
          fields:
            the_field:
            - agg: "date_histogram"
              calendar_interval: "1h"
              time_zone: "UTC"
            value_field:
            - agg: "min"
            - agg: "max"
            - agg: "sum"
      foo_rollup2:
        rollup_jobs:
        - job_id: "foo3"
          rollup_index: "foo_rollup2"
          index_pattern: "foo3"
          fields:
            the_field:
            - agg: "date_histogram"
              calendar_interval: "1h"
              time_zone: "UTC"
            value_field:
            - agg: "min"
            - agg: "max"
            - agg: "sum"

---
"Verify index pattern":

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo2
        body:  >
          {
            "index_pattern": "foo2",
            "rollup_index": "foo_rollup",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }
  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      rollup.put_job:
        id: foo3
        body:  >
          {
            "index_pattern": "foo3",
            "rollup_index": "foo_rollup2",
            "cron": "*/30 * * * * ?",
            "page_size" :10,
            "groups" : {
              "date_histogram": {
                "field": "the_field",
                "calendar_interval": "1h"
              }
            },
            "metrics": [
              {
                "field": "value_field",
                "metrics": ["min", "max", "sum"]
              }
            ]
          }

  - do:
      allowed_warnings:
        - "The rollup functionality will be removed in Elasticsearch 10.0. See docs for more information."
      rollup.get_rollup_index_caps:
        index: "*_rollup2"

  - match:
      $body:
        foo_rollup2:
          rollup_jobs:
            - job_id: "foo3"
              rollup_index: "foo_rollup2"
              index_pattern: "foo3"
              fields:
                the_field:
                  - agg: "date_histogram"
                    calendar_interval: "1h"
                    time_zone: "UTC"
                value_field:
                  - agg: "min"
                  - agg: "max"
                  - agg: "sum"

