Skip to content

Commit

Permalink
feat(synthetics): adds support for multiple browsers and devices with…
Browse files Browse the repository at this point in the history
… synthetic monitors (#2743)

Co-authored-by: vagrawal-newrelic <[email protected]>
Co-authored-by: pranav-new-relic <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent 8bd6083 commit aede342
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 138 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/newrelic/go-agent/v3 v3.30.0
github.com/newrelic/go-insights v1.0.3
github.com/newrelic/newrelic-client-go/v2 v2.44.0
github.com/newrelic/newrelic-client-go/v2 v2.45.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ github.com/newrelic/go-agent/v3 v3.30.0 h1:ZXHCT/Cot4iIPwcegCZURuRQOsfmGA6wilW+S
github.com/newrelic/go-agent/v3 v3.30.0/go.mod h1:9utrgxlSryNqRrTvII2XBL+0lpofXbqXApvVWPpbzUg=
github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ=
github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4=
github.com/newrelic/newrelic-client-go/v2 v2.44.0 h1:n4zP64Hfui8pjW/D3tbE1Hi+Acbpz8nBIrI2miEAGiI=
github.com/newrelic/newrelic-client-go/v2 v2.44.0/go.mod h1:pDFY24/6iIMEbPIdowTRrRn9YYwkXc3j+B+XpTb4oF4=
github.com/newrelic/newrelic-client-go/v2 v2.45.0 h1:3zaJqE4V2n07b8Fx3byV+WeJSdeTeR4sW6yVSXrrNN4=
github.com/newrelic/newrelic-client-go/v2 v2.45.0/go.mod h1:pDFY24/6iIMEbPIdowTRrRn9YYwkXc3j+B+XpTb4oF4=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
36 changes: 36 additions & 0 deletions newrelic/resource_helpers_synthetics.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ var SyntheticsUseLegacyRuntimeSchema = &schema.Schema{
DiffSuppressFunc: syntheticMonitorsUseUnsupportedLegacyRuntimeDiffSuppressor,
}

var browsersSchema = &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
Optional: true,
Description: "The multiple browsers list on which synthetic monitors will run. Valid values are array of CHROME," +
"and FIREFOX",
}
var devicesSchema = &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
Optional: true,
Description: "The multiple devices list on which synthetic monitors will run. Valid values are array of DESKTOP," +
" MOBILE_LANDSCAPE, MOBILE_PORTRAIT, TABLET_LANDSCAPE and TABLET_PORTRAIT",
}

var syntheticsMonitorPeriodValueMap = map[int]synthetics.SyntheticsMonitorPeriod{
1: synthetics.SyntheticsMonitorPeriodTypes.EVERY_MINUTE,
5: synthetics.SyntheticsMonitorPeriodTypes.EVERY_5_MINUTES,
Expand Down Expand Up @@ -256,6 +273,25 @@ func expandSyntheticsPublicLocations(locations []interface{}) []string {
return locationsOut
}

func expandSyntheticsBrowsers(browsers []interface{}) []synthetics.SyntheticsBrowser {
browsersOut := make([]synthetics.SyntheticsBrowser, len(browsers))

for i, v := range browsers {
browsersOut[i] = synthetics.SyntheticsBrowser(v.(string))

}
return browsersOut
}

func expandSyntheticsDevices(devices []interface{}) []synthetics.SyntheticsDevice {
devicesOut := make([]synthetics.SyntheticsDevice, len(devices))

for i, v := range devices {
devicesOut[i] = synthetics.SyntheticsDevice(v.(string))
}
return devicesOut
}

func expandSyntheticsPrivateLocations(locations []interface{}) []synthetics.SyntheticsPrivateLocationInput {
locationsOut := make([]synthetics.SyntheticsPrivateLocationInput, len(locations))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func resourceNewRelicSyntheticsBrokenLinksMonitor() *schema.Resource {
syntheticsMonitorLocationsAsStringsSchema(),
syntheticsMonitorRuntimeOptions(),
),
CustomizeDiff: validateSyntheticMonitorRuntimeAttributes,
CustomizeDiff: validateSyntheticMonitorAttributes,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func resourceNewRelicSyntheticsCertCheckMonitor() *schema.Resource {
},
SyntheticsUseLegacyRuntimeAttrLabel: SyntheticsUseLegacyRuntimeSchema,
},
CustomizeDiff: validateSyntheticMonitorRuntimeAttributes,
CustomizeDiff: validateSyntheticMonitorAttributes,
}
}

Expand Down
18 changes: 11 additions & 7 deletions newrelic/resource_newrelic_synthetics_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func resourceNewRelicSyntheticsMonitor() *schema.Resource {
Description: "Capture a screenshot during job execution",
Optional: true,
},
"browsers": browsersSchema,
"devices": devicesSchema,
"custom_header": {
Type: schema.TypeSet,
Description: "Custom headers to use in monitor job",
Expand All @@ -166,17 +168,19 @@ func resourceNewRelicSyntheticsMonitor() *schema.Resource {
},
},
"device_orientation": {
Type: schema.TypeString,
Optional: true,
Description: "The device orientation the user would like to represent. Valid values are LANDSCAPE, PORTRAIT, or NONE.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"device_type"},
Description: "The device orientation the user would like to represent. Valid values are LANDSCAPE, PORTRAIT, or NONE.",
},
"device_type": {
Type: schema.TypeString,
Optional: true,
Description: "The device type that a user can select. Valid values are MOBILE, TABLET, or NONE.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"device_orientation"},
Description: "The device type that a user can select. Valid values are MOBILE, TABLET, or NONE.",
},
},
CustomizeDiff: validateSyntheticMonitorRuntimeAttributes,
CustomizeDiff: validateSyntheticMonitorAttributes,
}
}

Expand Down
23 changes: 16 additions & 7 deletions newrelic/resource_newrelic_synthetics_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAccNewRelicSyntheticsBrowserMonitor_DeviceEmulationError(t *testing.T)
// 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`),
ExpectError: regexp.MustCompile("all of `device_orientation,device_type` must be\nspecified"),
},
},
})
Expand All @@ -51,10 +51,17 @@ func TestAccNewRelicSyntheticsBrowserMonitor_DeviceEmulationErrorUpdate(t *testi
testAccCheckNewRelicSyntheticsMonitorExists(resourceName),
),
},
// Test: Update
// Test: Update ; By removing device_type field
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulationError(rName, string(SyntheticsMonitorTypes.BROWSER)),
ExpectError: regexp.MustCompile(`both device_orientation and device_type should be specified to enable device emulation`),
ExpectError: regexp.MustCompile("all of `device_orientation,device_type` must be\nspecified"),
},
// Test: Update ; Added back removed device_type field
{
Config: testAccNewRelicSyntheticsBrowserMonitorConfig_DeviceEmulation(rName, string(SyntheticsMonitorTypes.BROWSER)),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicSyntheticsMonitorExists(resourceName),
),
},
},
})
Expand Down Expand Up @@ -288,6 +295,8 @@ func TestAccNewRelicSyntheticsSimpleBrowserMonitor(t *testing.T) {
"device_orientation",
"device_type",
SyntheticsUseLegacyRuntimeAttrLabel,
"browsers",
"devices",
},
},
},
Expand All @@ -305,6 +314,8 @@ func testAccNewRelicSyntheticsSimpleBrowserMonitorConfig(name string, monitorTyp
name = "customer-header-2"
value = "header-value-2"
}
browsers = ["CHROME", "FIREFOX"]
devices = ["DESKTOP","MOBILE_PORTRAIT", "TABLET_LANDSCAPE", "MOBILE_LANDSCAPE", "TABLET_PORTRAIT"]
enable_screenshot_on_failure_and_script = true
validation_string = "success"
verify_ssl = true
Expand All @@ -317,8 +328,6 @@ func testAccNewRelicSyntheticsSimpleBrowserMonitorConfig(name string, monitorTyp
status = "ENABLED"
type = "%s"
uri = "https://www.one.newrelic.com"
device_orientation = "PORTRAIT"
device_type = "MOBILE"
}`, name, monitorType)
}

Expand All @@ -329,6 +338,8 @@ func testAccNewRelicSyntheticsSimpleBrowserMonitorConfigUpdated(name string, mon
name = "name"
value = "simple_browser"
}
browsers = ["CHROME"]
devices = ["DESKTOP","MOBILE_PORTRAIT", "TABLET_LANDSCAPE"]
enable_screenshot_on_failure_and_script = false
validation_string = "success"
verify_ssl = false
Expand All @@ -341,8 +352,6 @@ func testAccNewRelicSyntheticsSimpleBrowserMonitorConfigUpdated(name string, mon
status = "DISABLED"
type = "%s"
uri = "https://www.one.newrelic.com"
device_orientation = "LANDSCAPE"
device_type = "TABLET"
}`, name, monitorType)
}

Expand Down
28 changes: 15 additions & 13 deletions newrelic/resource_newrelic_synthetics_script_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package newrelic

import (
"context"
"log"

"github.com/newrelic/newrelic-client-go/v2/pkg/common"
"github.com/newrelic/newrelic-client-go/v2/pkg/entities"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/newrelic/newrelic-client-go/v2/pkg/common"
"github.com/newrelic/newrelic-client-go/v2/pkg/entities"
"github.com/newrelic/newrelic-client-go/v2/pkg/synthetics"
"log"
)

func resourceNewRelicSyntheticsScriptMonitor() *schema.Resource {
Expand All @@ -28,7 +26,7 @@ func resourceNewRelicSyntheticsScriptMonitor() *schema.Resource {
syntheticsScriptMonitorLocationsSchema(),
syntheticsScriptBrowserMonitorAdvancedOptionsSchema(),
),
CustomizeDiff: validateSyntheticMonitorRuntimeAttributes,
CustomizeDiff: validateSyntheticMonitorAttributes,
}
}

Expand Down Expand Up @@ -66,23 +64,27 @@ func syntheticsScriptMonitorLocationsSchema() map[string]*schema.Schema {
}
}

// Scripted browser monitors have advanced options, but scripted API monitors do not.
// Scripted browser monitors have advanced options, browsers and devices fields, but scripted API monitors do not.
func syntheticsScriptBrowserMonitorAdvancedOptionsSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"enable_screenshot_on_failure_and_script": {
Type: schema.TypeBool,
Optional: true,
Description: "Capture a screenshot during job execution.",
},
"browsers": browsersSchema,
"devices": devicesSchema,
"device_orientation": {
Type: schema.TypeString,
Optional: true,
Description: "The device orientation the user would like to represent. Valid values are LANDSCAPE, PORTRAIT, or NONE.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"device_type"},
Description: "The device orientation the user would like to represent. Valid values are LANDSCAPE, PORTRAIT, or NONE.",
},
"device_type": {
Type: schema.TypeString,
Optional: true,
Description: "The device type that a user can select. Valid values are MOBILE, TABLET, or NONE.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"device_orientation"},
Description: "The device type that a user can select. Valid values are MOBILE, TABLET, or NONE.",
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func TestAccNewRelicSyntheticsScriptBrowserMonitor(t *testing.T) {
"device_orientation",
"device_type",
SyntheticsUseLegacyRuntimeAttrLabel,
"browsers",
"devices",
},
},
},
Expand All @@ -120,24 +122,6 @@ func testAccNewRelicSyntheticsScriptAPIMonitorConfig(name string, scriptMonitorT
}`, name, scriptMonitorType)
}

func testAccNewRelicSyntheticsScriptMonitorConfig(name string) string {
return fmt.Sprintf(`
resource "newrelic_synthetics_script_monitor" "foo" {
locations_public = [
"EU_WEST_1",
"EU_WEST_2",
]
name = "%s"
period = "EVERY_DAY"
script = "console.log('script update works!')"
status = "ENABLED"
type = "SCRIPT_BROWSER"
script_language = "JAVASCRIPT"
runtime_type = "`+SyntheticsChromeBrowserRuntimeType+`"
runtime_type_version = "`+SyntheticsChromeBrowserNewRuntimeTypeVersion+`"
}`, name)
}

func testAccNewRelicSyntheticsScriptBrowserMonitorConfig(name string) string {
return fmt.Sprintf(`
resource "newrelic_synthetics_script_monitor" "bar" {
Expand All @@ -153,9 +137,8 @@ func testAccNewRelicSyntheticsScriptBrowserMonitorConfig(name string) string {
script_language = "JAVASCRIPT"
script = "$browser.get('https://one.newrelic.com')"
device_orientation = "PORTRAIT"
device_type = "MOBILE"
browsers = ["CHROME", "FIREFOX"]
devices = ["DESKTOP", "TABLET_LANDSCAPE", "MOBILE_PORTRAIT", "MOBILE_LANDSCAPE", "TABLET_PORTRAIT"]
tag {
key = "Name"
values = ["scriptedMonitor"]
Expand Down Expand Up @@ -232,23 +215,3 @@ func testAccCheckNewRelicSyntheticsScriptMonitorDestroy(s *terraform.State) erro
}
return nil
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicSyntheticsScriptMonitorDestroy,
Steps: []resource.TestStep{

{
Config: testAccNewRelicSyntheticsScriptMonitorConfig(fmt.Sprintf(rName)),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicSyntheticsMonitorScriptUpdate(resourceName),
),
},
},
})
}
4 changes: 3 additions & 1 deletion newrelic/resource_newrelic_synthetics_step_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func resourceNewRelicSyntheticsStepMonitor() *schema.Resource {
syntheticsMonitorCommonSchema(),
syntheticsStepMonitorSchema(),
),
CustomizeDiff: validateSyntheticMonitorRuntimeAttributes,
CustomizeDiff: validateSyntheticMonitorAttributes,
}
}

Expand Down Expand Up @@ -103,6 +103,8 @@ func syntheticsStepMonitorSchema() map[string]*schema.Schema {
Description: "The specific semver version of the runtime type.",
},
SyntheticsUseLegacyRuntimeAttrLabel: SyntheticsUseLegacyRuntimeSchema,
"browsers": browsersSchema,
"devices": devicesSchema,
}
}

Expand Down
6 changes: 5 additions & 1 deletion newrelic/resource_newrelic_synthetics_step_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func TestAccNewRelicSyntheticsStepMonitor(t *testing.T) {
"tag",
"enable_screenshot_on_failure_and_script",
SyntheticsUseLegacyRuntimeAttrLabel,
"browsers",
"devices",
},
},
},
Expand All @@ -83,7 +85,9 @@ resource "newrelic_synthetics_step_monitor" "foo" {
type = "NAVIGATE"
values = ["https://google.com"]
}
browsers = ["CHROME", "FIREFOX"]
devices = ["DESKTOP","MOBILE_PORTRAIT", "TABLET_LANDSCAPE", "MOBILE_LANDSCAPE", "TABLET_PORTRAIT"]
%[2]s
}
`,
Expand Down
Loading

0 comments on commit aede342

Please sign in to comment.