---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings"
    - "allowed_warnings_regex"
- do:
    allowed_warnings:
    - "index [foo_rollup] matches multiple legacy templates [global, test], composable\
      \ templates will only match a single template"
    indices.create:
      index: "foo"
      body:
        mappings:
          properties:
            the_field:
              type: "date"
            value_field:
              type: "integer"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    indices.create:
      index: "bar"
      body:
        mappings:
          properties:
            the_field:
              type: "date"
            value_field:
              type: "integer"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    indices.create:
      index: "dummy-rollup-index"
      body:
        settings:
          number_of_shards: 1
          number_of_replicas: 0
        mappings:
          _meta:
            _rollup:
              my-id: {}
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test basic put_job:
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\": 10,\n  \"groups\": {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "acknowledged"
- do:
    rollup.get_jobs:
      id: "foo"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    jobs:
    - config:
        id: "foo"
        index_pattern: "foo"
        rollup_index: "foo_rollup"
        cron: "*/30 * * * * ?"
        page_size: 10
        groups:
          date_histogram:
            calendar_interval: "1h"
            field: "the_field"
            time_zone: "UTC"
        metrics:
        - field: "value_field"
          metrics:
          - "min"
          - "max"
          - "sum"
        timeout: "20s"
      stats:
        pages_processed: 0
        documents_processed: 0
        rollups_indexed: 0
        trigger_count: 0
        search_failures: 0
        index_failures: 0
        index_time_in_ms: 0
        index_total: 0
        search_time_in_ms: 0
        search_total: 0
        processing_time_in_ms: 0
        processing_total: 0
      status:
        job_state: "stopped"
- do:
    xpack.usage: {}
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    rollup.available: true
- match:
    rollup.enabled: true
- match:
    rollup.number_of_rollup_jobs: 1
---
Test put_job with existing name:
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "acknowledged"
- do:
    catch: "/Cannot create rollup job \\[foo\\] because job was previously created\
      \ \\(existing metadata\\)/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test put_job in non-rollup index:
- do:
    indices.create:
      index: "non-rollup"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "/Rollup data cannot be added to existing indices that contain non-rollup\
      \ data/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"non-rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Try to include headers:
- do:
    catch: "/unknown field \\[headers\\]/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"headers\": {\n    \"foo\": \"bar\"\n  },\n  \"cron\": \"*/30 * * *\
        \ * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n    \"date_histogram\":\
        \ {\n      \"field\": \"the_field\",\n      \"calendar_interval\": \"1h\"\n\
        \    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\",\n\
        \      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Validation failures:
- do:
    catch: "/Could not find a \\[numeric\\] or \\[date,date_nanos\\] field with name\
      \ \\[field_doesnt_exist\\] in any of the indices matching the index pattern/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"field_doesnt_exist\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Deprecation validation failure:
- do:
    indices.delete:
      index: "dummy-rollup-index"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "/new rollup jobs are not allowed in clusters that don't have any rollup\
      \ usage, since rollup has been deprecated/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"field_doesnt_exist\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Unknown Metric:
- do:
    catch: "/Unsupported metric \\[does_not_exist\\]/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\", \"does_not_exist\"]\n  \
        \  }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test put job with templates:
- do:
    indices.put_template:
      name: "test"
      body:
        index_patterns: "foo_*"
        mappings:
          properties:
            field:
              type: "keyword"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    allowed_warnings:
    - "index [foo_rollup] matches multiple legacy templates [global, test], composable\
      \ templates will only match a single template"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "foo"
      body: "{\n  \"index_pattern\": \"foo\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\" :10,\n  \"groups\" : {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "acknowledged"
- do:
    indices.get_mapping:
      index: "foo_rollup"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- set:
    foo_rollup.mappings._meta.rollup-version: "version"
- match:
    foo_rollup:
      mappings:
        _meta:
          _rollup:
            foo:
              id: "foo"
              index_pattern: "foo"
              rollup_index: "foo_rollup"
              cron: "*/30 * * * * ?"
              page_size: 10
              groups:
                date_histogram:
                  calendar_interval: "1h"
                  field: "the_field"
                  time_zone: "UTC"
              metrics:
              - field: "value_field"
                metrics:
                - "min"
                - "max"
                - "sum"
              timeout: "20s"
          rollup-version: "$version"
        dynamic_templates:
        - strings:
            match_mapping_type: "string"
            mapping:
              type: "keyword"
        - date_histograms:
            path_match: "*.date_histogram.timestamp"
            mapping:
              type: "date"
        properties:
          field:
            type: "keyword"
---
Test put_job with comma-delimited index pattern:
- requires:
    cluster_features:
    - "gte_v8.2.0"
    reason: "Comma delimited index pattern introduced in 8.2.0"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.put_job:
      id: "bar"
      body: "{\n  \"index_pattern\": \"foo,bar*\",\n  \"rollup_index\": \"foo_rollup\"\
        ,\n  \"cron\": \"*/30 * * * * ?\",\n  \"page_size\": 10,\n  \"groups\": {\n\
        \    \"date_histogram\": {\n      \"field\": \"the_field\",\n      \"calendar_interval\"\
        : \"1h\"\n    }\n  },\n  \"metrics\": [\n    {\n      \"field\": \"value_field\"\
        ,\n      \"metrics\": [\"min\", \"max\", \"sum\"]\n    }\n  ]\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "acknowledged"
- do:
    rollup.get_jobs:
      id: "bar"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    jobs:
    - config:
        id: "bar"
        index_pattern: "foo,bar*"
        rollup_index: "foo_rollup"
        cron: "*/30 * * * * ?"
        page_size: 10
        groups:
          date_histogram:
            calendar_interval: "1h"
            field: "the_field"
            time_zone: "UTC"
        metrics:
        - field: "value_field"
          metrics:
          - "min"
          - "max"
          - "sum"
        timeout: "20s"
      stats:
        pages_processed: 0
        documents_processed: 0
        rollups_indexed: 0
        trigger_count: 0
        search_failures: 0
        index_failures: 0
        index_time_in_ms: 0
        index_total: 0
        search_time_in_ms: 0
        search_total: 0
        processing_time_in_ms: 0
        processing_total: 0
      status:
        job_state: "stopped"
