From 4dbfca52f64362c59efcc34870875c1f8a85d686 Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Tue, 9 Apr 2024 17:42:59 -0700 Subject: [PATCH] Return error from getMonitorID instead of panic --- newrelic/resource_helpers_synthetics.go | 13 ++++++++++--- .../resource_newrelic_synthetics_alert_condition.go | 12 +++++++++--- ...elic_synthetics_multilocation_alert_condition.go | 6 +++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/newrelic/resource_helpers_synthetics.go b/newrelic/resource_helpers_synthetics.go index b50005cd8..1e43ae180 100644 --- a/newrelic/resource_helpers_synthetics.go +++ b/newrelic/resource_helpers_synthetics.go @@ -2,6 +2,7 @@ package newrelic import ( "encoding/base64" + "errors" "fmt" "strings" @@ -454,11 +455,17 @@ 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 "", errors.New("invalid monitor ID") + } monitorID := splitGUID[3] - return monitorID + return monitorID, nil } // This map facilitates safely setting the schema attributes which diff --git a/newrelic/resource_newrelic_synthetics_alert_condition.go b/newrelic/resource_newrelic_synthetics_alert_condition.go index f2200b75b..5bda7ece7 100644 --- a/newrelic/resource_newrelic_synthetics_alert_condition.go +++ b/newrelic/resource_newrelic_synthetics_alert_condition.go @@ -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) } @@ -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) diff --git a/newrelic/structures_newrelic_synthetics_multilocation_alert_condition.go b/newrelic/structures_newrelic_synthetics_multilocation_alert_condition.go index c6fd82d70..90b2ce624 100644 --- a/newrelic/structures_newrelic_synthetics_multilocation_alert_condition.go +++ b/newrelic/structures_newrelic_synthetics_multilocation_alert_condition.go @@ -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