Skip to content
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

fix: Handle monitorID error not found #2640

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions newrelic/resource_helpers_synthetics.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,18 @@ func getRuntimeValuesFromEntityTags(tags []entities.EntityTag) (runtimeType stri
return runtimeType, runtimeTypeVersion
}

func getMonitorID(monitorGUID string) string {
decodedGUID, _ := base64.RawStdEncoding.DecodeString(monitorGUID)
func getMonitorID(monitorGUID string) (string, error) {
decodedGUID, err := base64.RawStdEncoding.DecodeString(monitorGUID)
if err != nil {
return "", err
}
splitGUID := strings.Split(string(decodedGUID), "|")

if len(splitGUID) < 4 {
return "", fmt.Errorf("invalid monitor GUID '%s'", monitorGUID)
}
monitorID := splitGUID[3]
return monitorID
return monitorID, nil
}

// This map facilitates safely setting the schema attributes which
Expand Down
12 changes: 9 additions & 3 deletions newrelic/resource_newrelic_synthetics_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ func resourceNewRelicSyntheticsAlertConditionCreate(ctx context.Context, d *sche

monitorGUID := d.Get("monitor_id").(string)

monitorID := getMonitorID(monitorGUID)
monitorID, err := getMonitorID(monitorGUID)
if err != nil {
return diag.FromErr(err)
}

condition := expandSyntheticsCondition(d, monitorID)

log.Printf("[INFO] Creating New Relic Synthetics alert condition %s", condition.Name)

condition, err := client.Alerts.CreateSyntheticsConditionWithContext(ctx, policyID, *condition)
condition, err = client.Alerts.CreateSyntheticsConditionWithContext(ctx, policyID, *condition)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -158,7 +161,10 @@ func resourceNewRelicSyntheticsAlertConditionUpdate(ctx context.Context, d *sche

monitorGUID := d.Get("monitor_id").(string)

monitorID := getMonitorID(monitorGUID)
monitorID, err := getMonitorID(monitorGUID)
if err != nil {
return diag.FromErr(err)
}

condition := expandSyntheticsCondition(d, monitorID)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package newrelic

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -45,6 +46,22 @@ func TestAccNewRelicSyntheticsMultiLocationAlertCondition_Basic(t *testing.T) {
})
}

func TestAccNewRelicSyntheticsMultiLocationAlertCondition_InvalidEntities(t *testing.T) {
rName := generateNameForIntegrationTestResource()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicMultiLocationAlertConditionDestroy,
Steps: []resource.TestStep{
{
Config: testAccNewRelicSyntheticsMultiLocationConditionConfigInvalidEntities(rName, "1", "2"),
ExpectError: regexp.MustCompile(`invalid monitor GUID`),
},
},
})
}

func testAccCheckNewRelicSyntheticsMultiLocationAlertConditionExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
Expand Down Expand Up @@ -81,6 +98,33 @@ func testAccCheckNewRelicSyntheticsMultiLocationAlertConditionExists(n string) r
}
}

func testAccCheckNewRelicMultiLocationAlertConditionDestroy(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
client := providerConfig.NewClient

for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_nrql_alert_condition" {
continue
}

var err error

ids, err := parseHashedIDs(r.Primary.ID)
if err != nil {
return err
}

conditionID := ids[1]
policyID := ids[0]

if _, err = client.Alerts.GetMultiLocationSyntheticsCondition(policyID, conditionID); err == nil {
return fmt.Errorf("Synthetics multi-location condition still exists") //nolint:golint
}
}

return nil
}

func testAccNewRelicSyntheticsMultiLocationConditionConfigBasic(
name string,
criticalThreshold string,
Expand Down Expand Up @@ -126,29 +170,42 @@ resource "newrelic_synthetics_multilocation_alert_condition" "foo" {
`, name, criticalThreshold, warningThreshold, conditionalAttrs)
}

func testAccCheckNewRelicMultiLocationAlertConditionDestroy(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
client := providerConfig.NewClient

for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_nrql_alert_condition" {
continue
}
func testAccNewRelicSyntheticsMultiLocationConditionConfigInvalidEntities(
name string,
criticalThreshold string,
warningThreshold string,
) string {
return fmt.Sprintf(`
resource "newrelic_alert_policy" "policy" {
name = "tf-test-%[1]s"
}

var err error
resource "newrelic_synthetics_monitor" "monitor" {
locations_public = ["US_WEST_1"]
name = "tf-test-%[1]s"
period = "EVERY_10_MINUTES"
status = "DISABLED"
type = "SIMPLE"
uri = "https://www.one.newrelic.com"
}

ids, err := parseHashedIDs(r.Primary.ID)
if err != nil {
return err
}
resource "newrelic_synthetics_multilocation_alert_condition" "foo" {
policy_id = newrelic_alert_policy.policy.id
name = "tf-test-%[1]s"
runbook_url = "https://foo.example.com"
enabled = true
violation_time_limit_seconds = "3600"

conditionID := ids[1]
policyID := ids[0]
entities = ["1234"]
// Providing an invalid value here

if _, err = client.Alerts.GetMultiLocationSyntheticsCondition(policyID, conditionID); err == nil {
return fmt.Errorf("Synthetics multi-location condition still exists") //nolint:golint
}
}
critical {
threshold = %[2]s
}

return nil
warning {
threshold = %[3]s
}
}
`, name, criticalThreshold, warningThreshold)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func expandMultiLocationSyntheticsCondition(d *schema.ResourceData) (*alerts.Mul

var entities []string
for _, x := range d.Get("entities").([]interface{}) {
entities = append(entities, getMonitorID(x.(string)))
monitorID, err := getMonitorID(x.(string))
if err != nil {
return &condition, err
}
entities = append(entities, monitorID)
}

condition.Entities = entities
Expand Down
Loading