newrelic / terraform-provider-newrelic

Terraform provider for New Relic
https://registry.terraform.io/providers/newrelic/newrelic/latest/docs
Mozilla Public License 2.0
200 stars 247 forks source link

Data Source `newrelic_key_transaction`: Version 3.50.0 returns key transaction from incorrect New Relic account #2762

Open garotinguyen opened 3 weeks ago

garotinguyen commented 3 weeks ago

Terraform Version

1.5.7

Provider Version

3.50.0

Affected Resource(s)

Terraform Configuration

this is a part of our terraform code used to deploy to our 4 environment/newrelic accounts. We will set appropriate API Key using environment variables.

  1. Have multiple Newrelic Accounts (we have 4 ones)
  2. Create KeyTransaction with the same name in all accounts
terraform {
  required_version = ">= 1.0"
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "= 3.50.0"
    }
  }
}

data "newrelic_key_transaction" "my_key_transaction" {
  name = "MyTransactionName"   # we have this name in all 4 newrelic accounts
}

output "my_key_transaction_id" {
  value = newrelic_key_transaction.my_key_transaction.id
}

# this is an example resource which will use 
resource "newrelic_alert_condition" "my_alert_condition" {
  policy_id = "a_policy_id"

  name     = "MyAlertCondition Apdex"
  type     = "apm_kt_metric"
  entities = [data.newrelic_key_transaction.my_key_transaction.id]
  metric   = "apdex"

  condition_scope = "application"

  term {
    duration      = 5
    operator      = "below"
    priority      = "critical"
    threshold     = "0.70"
    time_function = "all"
  }
}

Actual Behavior

  1. We have 4 KeyTransaction with the same name, say MyTransactionName, in 4 Newrelic Accounts (says Dev, Demo, Staging, and Prod)
  2. The problem was, when we deploying to an account, say Dev1 account, the provider will return the KeyTransanction of the other newrelic account consistently (instead of Dev1). It only happens to the KeyTransaction resource. And it only happens when using terraform because I did verify with a rest tool like postman and I could get the correct KeyTransaction item using the same API key set for terrform. And then because the KeyTransaction is not correct, it will fail to create any dependent resources, e.g. newrelic_alert_condition.my_alert_condition in the example terraform configuration above.

Expected Behavior

We expect the terraform provider should load KeyTransaction from the correct newrelic account (associated with the API key)

Steps to Reproduce

We found the problem with both terraform plan and terraform apply

Debug Output

No thing special. It outputs the incorrect value of the KeyTransaction.

pranav-new-relic commented 3 weeks ago

Hi @garotinguyen, are you noticing this behaviour only with v3.50.0 and not with previous versions? This would help us debug the issue better, as we've recently changed the working of the newrelic_key_transaction data source to use NerdGraph instead of REST v2, starting v3.50.0. Thanks!

cc @vagrawal-newrelic

garotinguyen commented 3 weeks ago

hi @pranav-new-relic I can confirm version 3.48 work correctly because we reverted back to 3.48 to fix the issue. I didn't test with 3.49 so I cannot tell.

pranav-new-relic commented 3 weeks ago

@garotinguyen we just included a probable fix to this in our latest release (v3.51.0) as we were anyway going to release a version today :) can you please check if it works as desired now?

garotinguyen commented 3 weeks ago

@pranav-new-relic thank you for the quick fix, we will give it a try next week and will let you know the result