Skip to content

Fix: rule_id attribute ignored for elasticstack_kibana_alerting_rule resource #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2024

Conversation

b-deam
Copy link
Member

@b-deam b-deam commented May 13, 2024

Similar to #622, currently the rule_id attribute of a elasticstack_kibana_alerting_rule is ignored; we just pass in an empty string:

req := client.CreateRule(ctxWithAuth, rule.SpaceID, "").KbnXsrf("true").CreateRuleRequest(reqModel)

This commit fixes this by passing the rule_id attribute's value to the client's API call, if unset, we pass an empty string - the same as existing behaviour.

Note that there's a bug in the Alerting OpenAPI spec that means it's missing the 409 response, so I had to add a manual conflict check upon creation.

We can remove this check when elastic/kibana#183223 is closed, and the swagger version is bumped:

SWAGGER_VERSION ?= 8.7

Tested manually with this TF file:

terraform {
  required_providers {
    elasticstack = {
      source = "elastic/elasticstack"
    }
  }
}

provider "elasticstack" {
  elasticsearch {
    username  = "elastic"
    password  = "password"
    endpoints = ["http://localhost:9200"]
    insecure  = true
  }
  kibana {
    username  = "elastic"
    password  = "password"
    endpoints = ["http://localhost:5601"]
    insecure  = true
  }
}

resource "elasticstack_kibana_alerting_rule" "example_no_id" {
  name        = "my-alert-no-id"
  consumer    = "alerts"
  notify_when = "onActiveAlert"
  params = jsonencode({
    aggType             = "avg"
    groupBy             = "top"
    termSize            = 10
    timeWindowSize      = 10
    timeWindowUnit      = "s"
    threshold           = [10]
    thresholdComparator = ">"
    index               = ["kibana_sample_data_logs"]
    timeField           = "@timestamp"
    aggField            = "hour_of_day"
    termField           = "hour_of_day"
  })
  rule_type_id = ".index-threshold"
  interval     = "1m"
  enabled      = true
}


resource "elasticstack_kibana_alerting_rule" "example_id" {
  name        = "my-alert-with-id"
  rule_id = "af22bd1c-8fb3-4020-9249-a4ac5471624c"
  consumer    = "alerts"
  notify_when = "onActiveAlert"
  params = jsonencode({
    aggType             = "avg"
    groupBy             = "top"
    termSize            = 10
    timeWindowSize      = 10
    timeWindowUnit      = "s"
    threshold           = [10]
    thresholdComparator = ">"
    index               = ["kibana_sample_data_logs"]
    timeField           = "@timestamp"
    aggField            = "hour_of_day"
    termField           = "hour_of_day"
  })
  rule_type_id = ".index-threshold"
  interval     = "1m"
  enabled      = true
}

resource "elasticstack_kibana_alerting_rule" "example_conflicting_id" {
  name        = "my-alert-no-id"
  consumer    = "alerts"
  // Conflicts with example_id, should safely catch 409
  rule_id = "af22bd1c-8fb3-4020-9249-a4ac5471624c"
  notify_when = "onActiveAlert"
  params = jsonencode({
    aggType             = "avg"
    groupBy             = "top"
    termSize            = 10
    timeWindowSize      = 10
    timeWindowUnit      = "s"
    threshold           = [10]
    thresholdComparator = ">"
    index               = ["kibana_sample_data_logs"]
    timeField           = "@timestamp"
    aggField            = "hour_of_day"
    termField           = "hour_of_day"
  })
  rule_type_id = ".index-threshold"
  interval     = "1m"
  enabled      = true
}

@b-deam b-deam added bug Something isn't working Kibana Kibana related APIs go Pull requests that update Go code labels May 13, 2024
@b-deam b-deam requested review from tobio and wandergeek May 13, 2024 05:36
@b-deam b-deam self-assigned this May 13, 2024
Copy link
Contributor

@dimuon dimuon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@b-deam b-deam merged commit 2a13d1a into elastic:main May 14, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working go Pull requests that update Go code Kibana Kibana related APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants