Skip to content

Commit

Permalink
fix: improve multi-step runtime validation [sc-19433] (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
clample committed Feb 27, 2024
1 parent f611325 commit aec6a22
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
45 changes: 31 additions & 14 deletions checkly/resource_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,23 +529,14 @@ func resourceCheckCreate(d *schema.ResourceData, client interface{}) error {
if err != nil {
return fmt.Errorf("translation error: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()

if check.Type == "MULTI_STEP" {

checkRuntime, err := client.(checkly.Client).GetRuntime(ctx, *check.RuntimeID)

if err != nil {
return fmt.Errorf("API error while fetching runtimes: %w", err)
}

if !checkRuntime.MultiStepSupport {
return fmt.Errorf("runtime %s does not support MUTLI_STEP checks", *check.RuntimeID)
}

validationErr := validateRuntimeSupport(check, client)
if validationErr != nil {
return validationErr
}

ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
newCheck, err := client.(checkly.Client).CreateCheck(ctx, check)

if err != nil {
Expand Down Expand Up @@ -578,6 +569,12 @@ func resourceCheckUpdate(d *schema.ResourceData, client interface{}) error {
if err != nil {
return fmt.Errorf("translation error: %w", err)
}

validationErr := validateRuntimeSupport(check, client)
if validationErr != nil {
return validationErr
}

ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
_, err = client.(checkly.Client).UpdateCheck(ctx, check.ID, check)
Expand Down Expand Up @@ -1063,3 +1060,23 @@ func getResourceEnvironmentVariables(d *schema.ResourceData) ([]checkly.Environm

return deprecatedEnvironmentVariables, nil
}

func validateRuntimeSupport(check checkly.Check, client interface{}) error {
// If the check has a runtime ID set, then validate that the runtime supports multistep.
// Note that if the runtime ID is coming from the account defaults or group, then we don't validate it.
// Adding validation there as well would be a nice improvement, though.
if check.Type == "MULTI_STEP" && check.RuntimeID != nil {
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
checkRuntime, err := client.(checkly.Client).GetRuntime(ctx, *check.RuntimeID)

if err != nil {
return fmt.Errorf("API error while fetching runtimes: %w", err)
}

if !checkRuntime.MultiStepSupport {
return fmt.Errorf("runtime %s does not support MULTI_STEP checks", *check.RuntimeID)
}
}
return nil
}
33 changes: 33 additions & 0 deletions checkly/resource_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,39 @@ func TestAccApiCheckBasic(t *testing.T) {
})
}

func TestAccMultiStepCheckRuntimeValidation(t *testing.T) {
unsupportedRuntime := `resource "checkly_check" "test" {
name = "test"
type = "MULTI_STEP"
activated = true
frequency = 5
locations = ["eu-central-1"]
script = "console.log('test')"
runtime_id = "2023.02"
}`
noSpecifiedRuntime := `resource "checkly_check" "test" {
name = "test"
type = "MULTI_STEP"
activated = true
frequency = 5
locations = ["eu-central-1"]
script = "console.log('test')"
}`
accTestCase(t, []resource.TestStep{
{
Config: unsupportedRuntime,
ExpectError: regexp.MustCompile("Error: runtime 2023.02 does not support MULTI_STEP checks"),
},
{
Config: noSpecifiedRuntime,
Check: resource.TestCheckNoResourceAttr(
"checkly_check.test",
"runtime_id",
),
},
})
}

func TestAccMultiStepCheckBasic(t *testing.T) {
accTestCase(t, []resource.TestStep{
{
Expand Down

0 comments on commit aec6a22

Please sign in to comment.