Skip to content

Commit

Permalink
fix(synthetics): Add better error handling for device emulation scena…
Browse files Browse the repository at this point in the history
…rios (#2547)
  • Loading branch information
pjarugula authored Jan 23, 2024
1 parent 8e469ee commit 85ab2a5
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 29 deletions.
10 changes: 8 additions & 2 deletions newrelic/resource_newrelic_synthetics_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ func resourceNewRelicSyntheticsMonitorCreate(ctx context.Context, d *schema.Reso
simpleMonitorInput := buildSyntheticsSimpleMonitor(d)
resp, err = client.Synthetics.SyntheticsCreateSimpleMonitorWithContext(ctx, accountID, simpleMonitorInput)
case string(SyntheticsMonitorTypes.BROWSER):
simpleBrowserMonitorInput := buildSyntheticsSimpleBrowserMonitor(d)
simpleBrowserMonitorInput, browserMonitorErr := buildSyntheticsSimpleBrowserMonitor(d)
if browserMonitorErr != nil {
return diag.FromErr(browserMonitorErr)
}
resp, err = client.Synthetics.SyntheticsCreateSimpleBrowserMonitorWithContext(ctx, accountID, simpleBrowserMonitorInput)
}

Expand Down Expand Up @@ -362,7 +365,10 @@ func resourceNewRelicSyntheticsMonitorUpdate(ctx context.Context, d *schema.Reso
setSimpleMonitorAttributesFromUpdate(resp, d)

case string(SyntheticsMonitorTypes.BROWSER):
simpleBrowserMonitorUpdateInput := buildSyntheticsSimpleBrowserMonitorUpdateStruct(d)
simpleBrowserMonitorUpdateInput, err := buildSyntheticsSimpleBrowserMonitorUpdateStruct(d)
if err != nil {
return diag.FromErr(err)
}
resp, err := client.Synthetics.SyntheticsUpdateSimpleBrowserMonitorWithContext(ctx, guid, simpleBrowserMonitorUpdateInput)
if err != nil {
return diag.FromErr(err)
Expand Down
171 changes: 171 additions & 0 deletions newrelic/resource_newrelic_synthetics_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package newrelic

import (
"fmt"
"regexp"
"testing"
"time"

Expand All @@ -18,6 +19,176 @@ import (

var tv bool = true

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicSyntheticsMonitorDestroy,
Steps: []resource.TestStep{
// Test: Create
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationError(rName, string(SyntheticsMonitorTypes.BROWSER)),
ExpectError: regexp.MustCompile(`both device_orientation and device_type should be specified to enable device emulation`),
},
},
})
}

func TestAccNewRelicSyntheticsBrowserMonitor_DeviceEmulationErrorUpdate(t *testing.T) {
rName := generateNameForIntegrationTestResource()
resourceName := "newrelic_synthetics_monitor.foo"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicSyntheticsMonitorDestroy,
Steps: []resource.TestStep{
// Test: Create
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulation(rName, string(SyntheticsMonitorTypes.BROWSER)),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicSyntheticsMonitorExists(resourceName),
),
},
// Test: Update
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationError(rName, string(SyntheticsMonitorTypes.BROWSER)),
ExpectError: regexp.MustCompile(`both device_orientation and device_type should be specified to enable device emulation`),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicSyntheticsMonitorDestroy,
Steps: []resource.TestStep{
// Test: Create
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationLegacyRuntimeError(rName, string(SyntheticsMonitorTypes.BROWSER)),
ExpectError: regexp.MustCompile(`device emulation is not supported by legacy runtime`),
},
},
})
}

func TestAccNewRelicSyntheticsBrowserMonitor_DeviceEmulationLegacyRuntimeErrorUpdate(t *testing.T) {
rName := generateNameForIntegrationTestResource()
resourceName := "newrelic_synthetics_monitor.foo"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicSyntheticsMonitorDestroy,
Steps: []resource.TestStep{
// Test: Create
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulation(rName, string(SyntheticsMonitorTypes.BROWSER)),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicSyntheticsMonitorExists(resourceName),
),
},
// Test: Update
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationLegacyRuntimeError(rName, string(SyntheticsMonitorTypes.BROWSER)),
ExpectError: regexp.MustCompile(`device emulation is not supported by legacy runtime`),
},
},
})
}
func testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationError(name string, monitorType string) string {
return fmt.Sprintf(`
resource "newrelic_synthetics_monitor" "foo" {
status = "ENABLED"
name = "%s"
period = "EVERY_MINUTE"
uri = "https://www.one.newrelic.com"
type = "%s"
locations_public = ["AP_SOUTH_1"]
custom_header {
name = "Name"
value = "browserMonitor"
}
enable_screenshot_on_failure_and_script = true
validation_string = "success"
verify_ssl = true
runtime_type_version = "100"
runtime_type = "CHROME_BROWSER"
script_language = "JAVASCRIPT"
device_orientation = "LANDSCAPE"
tag {
key = "butterscotch"
values = ["cake"]
}
}`, name, monitorType)
}

func testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulation(name string, monitorType string) string {
return fmt.Sprintf(`
resource "newrelic_synthetics_monitor" "foo" {
status = "ENABLED"
name = "%s"
period = "EVERY_MINUTE"
uri = "https://www.one.newrelic.com"
type = "%s"
locations_public = ["AP_SOUTH_1"]
custom_header {
name = "Name"
value = "browserMonitor"
}
enable_screenshot_on_failure_and_script = true
validation_string = "success"
verify_ssl = true
runtime_type_version = "100"
runtime_type = "CHROME_BROWSER"
script_language = "JAVASCRIPT"
device_orientation = "LANDSCAPE"
device_type = "MOBILE"
tag {
key = "butterscotch"
values = ["cake"]
}
}`, name, monitorType)
}

func testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationLegacyRuntimeError(name string, monitorType string) string {
return fmt.Sprintf(`
resource "newrelic_synthetics_monitor" "foo" {
status = "ENABLED"
name = "%s"
period = "EVERY_MINUTE"
uri = "https://www.one.newrelic.com"
type = "%s"
locations_public = ["AP_SOUTH_1"]
custom_header {
name = "Name"
value = "browserMonitor"
}
enable_screenshot_on_failure_and_script = true
validation_string = "success"
verify_ssl = true
device_orientation = "LANDSCAPE"
device_type = "MOBILE"
tag {
key = "butterscotch"
values = ["cake"]
}
}`, name, monitorType)
}
func TestAccNewRelicSyntheticsSimpleMonitor(t *testing.T) {
resourceName := "newrelic_synthetics_monitor.foo"
rName := generateNameForIntegrationTestResource()
Expand Down
77 changes: 50 additions & 27 deletions newrelic/structures_newrelic_synthetics_simple_browser_monitor.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package newrelic

import (
"errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/newrelic/newrelic-client-go/v2/pkg/synthetics"
)

func buildSyntheticsSimpleBrowserMonitor(d *schema.ResourceData) synthetics.SyntheticsCreateSimpleBrowserMonitorInput {
func buildSyntheticsSimpleBrowserMonitor(d *schema.ResourceData) (synthetics.SyntheticsCreateSimpleBrowserMonitorInput, error) {
inputBase := expandSyntheticsMonitorBase(d)

simpleBrowserMonitorInput := synthetics.SyntheticsCreateSimpleBrowserMonitorInput{
simpleBrowserMonitorInput := &synthetics.SyntheticsCreateSimpleBrowserMonitorInput{
Name: inputBase.Name,
Period: inputBase.Period,
Status: inputBase.Status,
Expand Down Expand Up @@ -46,15 +47,24 @@ func buildSyntheticsSimpleBrowserMonitor(d *schema.ResourceData) synthetics.Synt
simpleBrowserMonitorInput.AdvancedOptions.UseTlsValidation = &vs
}

sciptLang, scriptLangOk := d.GetOk("script_language")
err := buildSyntheticsSimpleBrowserMonitorRuntimeAndDeviceEmulation(d, simpleBrowserMonitorInput)
if err != nil {
return *simpleBrowserMonitorInput, err
}

return *simpleBrowserMonitorInput, nil
}

func buildSyntheticsSimpleBrowserMonitorRuntimeAndDeviceEmulation(d *schema.ResourceData, simpleBrowserMonitorInput *synthetics.SyntheticsCreateSimpleBrowserMonitorInput) error {
scriptLang, scriptLangOk := d.GetOk("script_language")
runtimeType, runtimeTypeOk := d.GetOk("runtime_type")
runtimeTypeVersion, runtimeTypeVersionOk := d.GetOk("runtime_type_version")

if scriptLangOk || runtimeTypeOk || runtimeTypeVersionOk {
simpleBrowserMonitorInput.Runtime = &synthetics.SyntheticsRuntimeInput{}

if scriptLangOk {
simpleBrowserMonitorInput.Runtime.ScriptLanguage = sciptLang.(string)
simpleBrowserMonitorInput.Runtime.ScriptLanguage = scriptLang.(string)
}

if runtimeTypeOk {
Expand All @@ -69,25 +79,27 @@ func buildSyntheticsSimpleBrowserMonitor(d *schema.ResourceData) synthetics.Synt
do, doOk := d.GetOk("device_orientation")
dt, dtOk := d.GetOk("device_type")

if doOk || dtOk {
simpleBrowserMonitorInput.AdvancedOptions.DeviceEmulation = &synthetics.SyntheticsDeviceEmulationInput{}

if doOk {
simpleBrowserMonitorInput.AdvancedOptions.DeviceEmulation.DeviceOrientation = synthetics.SyntheticsDeviceOrientation(do.(string))
}
if !(runtimeTypeOk && runtimeTypeVersionOk) && (doOk && dtOk) {
return errors.New("device emulation is not supported by legacy runtime")
}

if dtOk {
simpleBrowserMonitorInput.AdvancedOptions.DeviceEmulation.DeviceType = synthetics.SyntheticsDeviceType(dt.(string))
if doOk && dtOk {
simpleBrowserMonitorInput.AdvancedOptions.DeviceEmulation = &synthetics.SyntheticsDeviceEmulationInput{}
simpleBrowserMonitorInput.AdvancedOptions.DeviceEmulation.DeviceOrientation = synthetics.SyntheticsDeviceOrientation(do.(string))
simpleBrowserMonitorInput.AdvancedOptions.DeviceEmulation.DeviceType = synthetics.SyntheticsDeviceType(dt.(string))
} else {
if doOk || dtOk {
return errors.New("both device_orientation and device_type should be specified to enable device emulation")
}
}

return simpleBrowserMonitorInput
return nil
}

func buildSyntheticsSimpleBrowserMonitorUpdateStruct(d *schema.ResourceData) synthetics.SyntheticsUpdateSimpleBrowserMonitorInput {
func buildSyntheticsSimpleBrowserMonitorUpdateStruct(d *schema.ResourceData) (synthetics.SyntheticsUpdateSimpleBrowserMonitorInput, error) {
inputBase := expandSyntheticsMonitorBase(d)

simpleBrowserMonitorUpdateInput := synthetics.SyntheticsUpdateSimpleBrowserMonitorInput{
simpleBrowserMonitorUpdateInput := &synthetics.SyntheticsUpdateSimpleBrowserMonitorInput{
Name: inputBase.Name,
Period: inputBase.Period,
Status: inputBase.Status,
Expand Down Expand Up @@ -125,15 +137,24 @@ func buildSyntheticsSimpleBrowserMonitorUpdateStruct(d *schema.ResourceData) syn
simpleBrowserMonitorUpdateInput.AdvancedOptions.UseTlsValidation = &vs
}

sciptLang, scriptLangOk := d.GetOk("script_language")
err := buildSyntheticsSimpleBrowserMonitorRuntimeAndDeviceEmulationUpdateStruct(d, simpleBrowserMonitorUpdateInput)
if err != nil {
return *simpleBrowserMonitorUpdateInput, err
}

return *simpleBrowserMonitorUpdateInput, nil
}

func buildSyntheticsSimpleBrowserMonitorRuntimeAndDeviceEmulationUpdateStruct(d *schema.ResourceData, simpleBrowserMonitorUpdateInput *synthetics.SyntheticsUpdateSimpleBrowserMonitorInput) error {
scriptLang, scriptLangOk := d.GetOk("script_language")
runtimeType, runtimeTypeOk := d.GetOk("runtime_type")
runtimeTypeVersion, runtimeTypeVersionOk := d.GetOk("runtime_type_version")

if scriptLangOk || runtimeTypeOk || runtimeTypeVersionOk {
simpleBrowserMonitorUpdateInput.Runtime = &synthetics.SyntheticsRuntimeInput{}

if scriptLangOk {
simpleBrowserMonitorUpdateInput.Runtime.ScriptLanguage = sciptLang.(string)
simpleBrowserMonitorUpdateInput.Runtime.ScriptLanguage = scriptLang.(string)
}

if runtimeTypeOk {
Expand All @@ -148,17 +169,19 @@ func buildSyntheticsSimpleBrowserMonitorUpdateStruct(d *schema.ResourceData) syn
do, doOk := d.GetOk("device_orientation")
dt, dtOk := d.GetOk("device_type")

if doOk || dtOk {
simpleBrowserMonitorUpdateInput.AdvancedOptions.DeviceEmulation = &synthetics.SyntheticsDeviceEmulationInput{}

if doOk {
simpleBrowserMonitorUpdateInput.AdvancedOptions.DeviceEmulation.DeviceOrientation = synthetics.SyntheticsDeviceOrientation(do.(string))
}
if !(runtimeTypeOk && runtimeTypeVersionOk) && (doOk && dtOk) {
return errors.New("device emulation is not supported by legacy runtime")
}

if dtOk {
simpleBrowserMonitorUpdateInput.AdvancedOptions.DeviceEmulation.DeviceType = synthetics.SyntheticsDeviceType(dt.(string))
if doOk && dtOk {
simpleBrowserMonitorUpdateInput.AdvancedOptions.DeviceEmulation = &synthetics.SyntheticsDeviceEmulationInput{}
simpleBrowserMonitorUpdateInput.AdvancedOptions.DeviceEmulation.DeviceOrientation = synthetics.SyntheticsDeviceOrientation(do.(string))
simpleBrowserMonitorUpdateInput.AdvancedOptions.DeviceEmulation.DeviceType = synthetics.SyntheticsDeviceType(dt.(string))
} else {
if doOk || dtOk {
return errors.New("both device_orientation and device_type should be specified to enable device emulation")
}
}

return simpleBrowserMonitorUpdateInput
return nil
}

0 comments on commit 85ab2a5

Please sign in to comment.