thousandeyes / terraform-provider-thousandeyes

ThousandEyes Terraform Provider
Apache License 2.0
21 stars 26 forks source link

[Bug]: Association between Alert Rules and Tests are lost when the thousandeyes_alert_rule resources are modified #158

Closed omaslara closed 3 months ago

omaslara commented 8 months ago

What versions are you using?

❯ terraform version
Terraform v1.4.6
on darwin_amd64
+ provider registry.terraform.io/thousandeyes/thousandeyes v2.0.6

What did you expect to happen?

When a change is made to a thousandeyes_alert_rule resource associated with one or more tests, the association is expected to remain the same.

What actually happened?

When a change is made to a thousandeyes_alert_rule resource, all tests are disassociated from that alert rule.

Terraform code to reproduce the bug

This is the minimum code required to reproduce the bug:

terraform {
  required_providers {
    thousandeyes = {
      source  = "thousandeyes/thousandeyes"
      version = ">= 2.0.6"
    }
  }
}

provider "thousandeyes" {
  token            = "your_token"
  account_group_id = "your_account_group_id"
}

data "thousandeyes_agent" "miami" {
  agent_name = "Miami, FL"
}

resource "thousandeyes_alert_rule" "nprod_a2s_alert_major" {
  severity                  = "MAJOR"
  rule_name                 = "NPROD - Network Server Alert Rule - Major"
  alert_type                = "End-to-End (Server)"
  expression                = "((loss >= 50%) || (probDetail != \"\") || (avgLatency >= 200 ms))"
  minimum_sources           = 2
  rounds_violating_required = 3
  rounds_violating_out_of   = 4
}

resource "thousandeyes_agent_to_server" "agent_to_server_webexapi" {
  test_name        = "NPROD - Webex APIs"
  interval         = 300
  alerts_enabled   = true
  server           = "webexapis.com"
  protocol         = "TCP"
  port             = 443
  enabled          = true
  bgp_measurements = true
  use_public_bgp   = true
  mtu_measurements = false

  agents {
    agent_id = data.thousandeyes_agent.miami.agent_id
  }

  alert_rules {
    rule_id = thousandeyes_alert_rule.nprod_a2s_alert_major.id
  }
}

Any additional comments or code?

The provided terraform configuration is for only one alert type, but the same issue happens with any other alert type I tested:

I can provide examples for all the above if needed.

Steps to reproduce the bug

  1. Run terraform apply
  2. Check the Alert Rule in the UI to confirm it is associated with a Test.
  3. Modify the thousandeyes_alert_rule. For example, change the rounds_violating_required value from 3 to 4.
  4. Apply the change: terraform apply
  5. Check the Alert Rule in the UI to confirm it is NOT associated with any Test.

A workaround to get the associations back is to replace the Terraform resource. Running the command below will re-associate the provided alert rule with the test:

terraform apply -auto-approve -replace=thousandeyes_alert_rule.nprod_a2s_alert_major

Sample captures:

❯ terraform apply -auto-approve
data.thousandeyes_agent.miami: Reading...
data.thousandeyes_agent.miami: Read complete after 2s [id=0xc0004606b0]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # thousandeyes_agent_to_server.agent_to_server_webexapi will be created
  + resource "thousandeyes_agent_to_server" "agent_to_server_webexapi" {
      + alerts_enabled         = true
      + api_links              = (known after apply)
      + bandwidth_measurements = false
      + bgp_measurements       = true
      + created_by             = (known after apply)
      + created_date           = (known after apply)
      + enabled                = true
      + groups                 = (known after apply)
      + id                     = (known after apply)
      + interval               = 300
      + live_share             = (known after apply)
      + modified_by            = (known after apply)
      + modified_date          = (known after apply)
      + network_measurements   = true
      + path_trace_mode        = "classic"
      + port                   = 443
      + probe_mode             = "AUTO"
      + protocol               = "TCP"
      + saved_event            = (known after apply)
      + server                 = "webexapis.com"
      + test_id                = (known after apply)
      + test_name              = "NPROD - Webex APIs"
      + type                   = (known after apply)
      + use_public_bgp         = true

      + agents {
          + agent_id = 275
        }

      + alert_rules {
          + rule_id = (known after apply)
        }
    }

  # thousandeyes_alert_rule.nprod_a2s_alert_major will be created
  + resource "thousandeyes_alert_rule" "nprod_a2s_alert_major" {
      + alert_rule_id             = (known after apply)
      + alert_type                = "End-to-End (Server)"
      + default                   = false
      + expression                = "((loss >= 50%) || (probDetail != \"\") || (avgLatency >= 200 ms))"
      + id                        = (known after apply)
      + minimum_sources           = 2
      + notify_on_clear           = true
      + rounds_violating_mode     = "ANY"
      + rounds_violating_out_of   = 4
      + rounds_violating_required = 3
      + rule_id                   = (known after apply)
      + rule_name                 = "NPROD - Network Server Alert Rule - Major"
      + severity                  = "MAJOR"
    }

Plan: 2 to add, 0 to change, 0 to destroy.
thousandeyes_alert_rule.nprod_a2s_alert_major: Creating...
thousandeyes_alert_rule.nprod_a2s_alert_major: Creation complete after 2s [id=9957582]
thousandeyes_agent_to_server.agent_to_server_webexapi: Creating...
thousandeyes_agent_to_server.agent_to_server_webexapi: Creation complete after 6s [id=4528322]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
image
❯ terraform apply -auto-approve
data.thousandeyes_agent.miami: Reading...
thousandeyes_alert_rule.nprod_a2s_alert_major: Refreshing state... [id=9957582]
data.thousandeyes_agent.miami: Read complete after 1s [id=0xc0000dd6a0]
thousandeyes_agent_to_server.agent_to_server_webexapi: Refreshing state... [id=4528322]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # thousandeyes_agent_to_server.agent_to_server_webexapi will be updated in-place
  ~ resource "thousandeyes_agent_to_server" "agent_to_server_webexapi" {
        id                     = "4528322"
      ~ mtu_measurements       = true -> false
        # (22 unchanged attributes hidden)

        # (2 unchanged blocks hidden)
    }

  # thousandeyes_alert_rule.nprod_a2s_alert_major will be updated in-place
  ~ resource "thousandeyes_alert_rule" "nprod_a2s_alert_major" {
        id                        = "9957582"
      ~ rounds_violating_required = 3 -> 4
        # (10 unchanged attributes hidden)
    }

Plan: 0 to add, 2 to change, 0 to destroy.
thousandeyes_alert_rule.nprod_a2s_alert_major: Modifying... [id=9957582]
thousandeyes_alert_rule.nprod_a2s_alert_major: Modifications complete after 3s [id=9957582]
thousandeyes_agent_to_server.agent_to_server_webexapi: Modifying... [id=4528322]
thousandeyes_agent_to_server.agent_to_server_webexapi: Modifications complete after 5s [id=4528322]

Apply complete! Resources: 0 added, 2 changed, 0 destroyed.
❯
image