---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    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: "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"
- 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 * * * * ? 2017\",\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 basic delete_job:
- 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 * * * * ? 2017"
        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:
    rollup.delete_job:
      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"
- 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: []
---
Test delete job twice:
- 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 * * * * ? 2017"
        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:
    rollup.delete_job:
      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"
- 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: []
---
Test delete running job:
- 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 * * * * ? 2017"
        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:
    rollup.start_job:
      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"
- is_true: "started"
- do:
    catch: "request"
    rollup.delete_job:
      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"
- is_false: "acknowledged"
- match:
    task_failures.0.reason.type: "illegal_state_exception"
- match:
    task_failures.0.reason.reason: "Could not delete job [foo] because indexer state\
      \ is [STARTED].  Job must be [STOPPED] before deletion."
---
Test delete non-existent job:
- do:
    catch: "/the task with id \\[does_not_exist\\] doesn't exist/"
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    rollup.delete_job:
      id: "does_not_exist"
    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"
