newrelic / terraform-provider-newrelic

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

`synthetics_multilocation_alert_condition` panic: `getMonitorID()` fails with panic on wrong monitor ID #2631

Closed guineveresaenger closed 5 months ago

guineveresaenger commented 5 months ago

Title says it - when a monitor_id is incorrect, the provider crashes.

Terraform Version

Terraform v1.5.7 on darwin_arm64

Affected Resource(s)

Terraform Configuration

terraform {
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
    }
  }
}

provider "newrelic" {
  account_id = "redacted"
  api_key = "redacted"    # Usually prefixed with 'NRAK'
  region = "US"                    # Valid regions are US and EU
}

resource "newrelic_alert_policy" "policy" {
  name = "my-policy"
}

resource "newrelic_synthetics_monitor" "monitor" {
  locations_public = ["US_WEST_1"]
  name             = "my-monitor"
  period           = "EVERY_10_MINUTES"
  status           = "DISABLED"
  type             = "SIMPLE"
  uri              = "https://www.one.newrelic.com"
}

resource "newrelic_synthetics_multilocation_alert_condition" "example" {
  policy_id = newrelic_alert_policy.policy.id

  name                         = "Example condition"
  runbook_url                  = "https://example.com"
  enabled                      = true
  violation_time_limit_seconds = 3600

  entities = [
    "123"
  ]

  critical {
    threshold = 2
  }

  warning {
    threshold = 1
  }
}

Actual Behavior

Provider crashed

Expected Behavior

Provider should have errored with a message indicating an invalid monitor ID

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Run the basic example but use an incorrect monitor ID (I subbed in 123 to trigger the panic in my above code).

Debug Output

https://gist.github.com/guineveresaenger/2bd6e69b1fc6bd495fc4fe7e58eb0589

Panic Output

Apologies - there seems to be no crash.log here, but the stacktrace is short:

Stack trace from the terraform-provider-newrelic_v3.34.1 plugin:

panic: runtime error: index out of range [3] with length 1

goroutine 50 [running]:
github.com/newrelic/terraform-provider-newrelic/v2/newrelic.getMonitorID({0x140006952f8?, 0x101172b04?})
    /home/runner/work/terraform-provider-newrelic/terraform-provider-newrelic/newrelic/resource_helpers_synthetics.go:460 +0x88
github.com/newrelic/terraform-provider-newrelic/v2/newrelic.expandMultiLocationSyntheticsCondition(0x10158cc40?)
    /home/runner/work/terraform-provider-newrelic/terraform-provider-newrelic/newrelic/structures_newrelic_synthetics_multilocation_alert_condition.go:31 +0x2d8
github.com/newrelic/terraform-provider-newrelic/v2/newrelic.resourceNewRelicSyntheticsMultiLocationAlertConditionUpdate({0x10158cc08, 0x14000046ea0}, 0x14000456b00, {0x101419b60?, 0x140003c17c0?})
    /home/runner/work/terraform-provider-newrelic/terraform-provider-newrelic/newrelic/resource_newrelic_synthetics_multilocation_alert_condition.go:166 +0x64
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).update(0x140002b3ce0, {0x10158cc40, 0x140008f0f90}, 0xd?, {0x101419b60, 0x140003c17c0})
    /home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:741 +0xe8
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0x140002b3ce0, {0x10158cc40, 0x140008f0f90}, 0x14000692750, 0x14000456900, {0x101419b60, 0x140003c17c0})
    /home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/resource.go:847 +0x67c
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0x14000292048, {0x10158cc40?, 0x140008f0e70?}, 0x14000308690)
    /home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.26.1/helper/schema/grpc_provider.go:1021 +0xb70
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0x140002ea000, {0x10158cc40?, 0x140008f0450?}, 0x140001661c0)
    /home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov5/tf5server/server.go:818 +0x3b8
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x10152bd40?, 0x140002ea000}, {0x10158cc40, 0x140008f0450}, 0x14000166150, 0x0)
    /home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0x140001d01e0, {0x101591780, 0x140005824e0}, 0x14000556000, 0x140002ec030, 0x101ba6e00, 0x0)
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.57.1/server.go:1358 +0xc88
google.golang.org/grpc.(*Server).handleStream(0x140001d01e0, {0x101591780, 0x140005824e0}, 0x14000556000, 0x0)
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.57.1/server.go:1735 +0x82c
google.golang.org/grpc.(*Server).serveStreams.func1.1()
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.57.1/server.go:970 +0xb4
created by google.golang.org/grpc.(*Server).serveStreams.func1
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.57.1/server.go:981 +0x174

Error: The terraform-provider-newrelic_v3.34.1 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

Important Factoids

We know we need a valid monitor ID - but we'd like to see an error, not a panic.

References

Previous instances of this panic seemed acknowledged, but not addressed: https://github.com/newrelic/terraform-provider-newrelic/issues/2234 https://github.com/newrelic/terraform-provider-newrelic/issues/2003

pranav-new-relic commented 5 months ago

Thank you for identifying this and the proposed code changes, @guineveresaenger. Please allow us some time to look into this issue further so we can make sure the code changes are good to go :)

guineveresaenger commented 5 months ago

hi @pranav-new-relic - thanks for the quick reply! Unfortunately, I may not be able to sign your CLA due to employer restrictions I wasn't aware of :( but please of course feel free to crib my proposed code changes.

abasha1234 commented 5 months ago

Hi @guineveresaenger, changes for this have been made and merged. We've just released v3.35.1 of the Terraform Provider. Please check it out. Thanks for reporting!