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

Return error from getMonitorID instead of panic #2632

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 @@ -2,6 +2,7 @@ package newrelic

import (
"encoding/base64"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -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
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 @@ -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