Skip to content

Commit 0c19cdd

Browse files
Antoinecoutellier/sc 17816/add multistep checks to terraform (#278)
* feature: add MULTI_STEP support and check if check runtime supports MULTI_STEPS [sc-17816] * chore: update our TF provider to support up-to-date methods to get, update and delete a check [sc-17816] * test: add test case for MULTI_STEP [sc-17816] * Update checkly/resource_check_test.go Co-authored-by: Maxi Gimenez <[email protected]> * chore: update go sdk to 1.7.0 * fix: go generate --------- Co-authored-by: Maxi Gimenez <[email protected]>
1 parent 2538b81 commit 0c19cdd

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

checkly/resource_check.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func resourceCheck() *schema.Resource {
3535
"type": {
3636
Type: schema.TypeString,
3737
Required: true,
38-
Description: "The type of the check. Possible values are `API`, and `BROWSER`.",
38+
Description: "The type of the check. Possible values are `API`, `BROWSER`, and `MULTI_STEP`.",
3939
},
4040
"frequency": {
4141
Type: schema.TypeInt,
@@ -505,7 +505,22 @@ func resourceCheckCreate(d *schema.ResourceData, client interface{}) error {
505505
}
506506
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
507507
defer cancel()
508-
newCheck, err := client.(checkly.Client).Create(ctx, check)
508+
509+
if check.Type == "MULTI_STEP" {
510+
511+
checkRuntime, err := client.(checkly.Client).GetRuntime(ctx, *check.RuntimeID)
512+
513+
if err != nil {
514+
return fmt.Errorf("API error while fetching runtimes: %w", err)
515+
}
516+
517+
if !checkRuntime.MultiStepSupport {
518+
return fmt.Errorf("runtime %s does not support MUTLI_STEP checks", *check.RuntimeID)
519+
}
520+
521+
}
522+
523+
newCheck, err := client.(checkly.Client).CreateCheck(ctx, check)
509524

510525
if err != nil {
511526
checkJSON, _ := json.Marshal(check)
@@ -518,7 +533,7 @@ func resourceCheckCreate(d *schema.ResourceData, client interface{}) error {
518533
func resourceCheckRead(d *schema.ResourceData, client interface{}) error {
519534
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
520535
defer cancel()
521-
check, err := client.(checkly.Client).Get(ctx, d.Id())
536+
check, err := client.(checkly.Client).GetCheck(ctx, d.Id())
522537
if err != nil {
523538
if strings.Contains(err.Error(), "404") {
524539
//if resource is deleted remotely, then mark it as
@@ -539,7 +554,7 @@ func resourceCheckUpdate(d *schema.ResourceData, client interface{}) error {
539554
}
540555
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
541556
defer cancel()
542-
_, err = client.(checkly.Client).Update(ctx, check.ID, check)
557+
_, err = client.(checkly.Client).UpdateCheck(ctx, check.ID, check)
543558
if err != nil {
544559
checkJSON, _ := json.Marshal(check)
545560
return fmt.Errorf("API error 3: Couldn't update check, Error: %w, \nCheck: %s", err, checkJSON)
@@ -551,7 +566,7 @@ func resourceCheckUpdate(d *schema.ResourceData, client interface{}) error {
551566
func resourceCheckDelete(d *schema.ResourceData, client interface{}) error {
552567
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
553568
defer cancel()
554-
if err := client.(checkly.Client).Delete(ctx, d.Id()); err != nil {
569+
if err := client.(checkly.Client).DeleteCheck(ctx, d.Id()); err != nil {
555570
return fmt.Errorf("API error 4: Couldn't delete Check %s, Error: %w", d.Id(), err)
556571
}
557572
return nil

checkly/resource_check_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,75 @@ func TestAccApiCheckBasic(t *testing.T) {
212212
})
213213
}
214214

215+
func TestAccMultiStepCheckBasic(t *testing.T) {
216+
accTestCase(t, []resource.TestStep{
217+
{
218+
Config: multiStepCheck_basic,
219+
Check: resource.ComposeTestCheckFunc(
220+
resource.TestCheckResourceAttr(
221+
"checkly_check.test",
222+
"name",
223+
"MultiStep Check",
224+
),
225+
resource.TestCheckResourceAttr(
226+
"checkly_check.test",
227+
"type",
228+
"MULTI_STEP",
229+
),
230+
resource.TestCheckResourceAttr(
231+
"checkly_check.test",
232+
"runtime_id",
233+
"2023.09",
234+
),
235+
resource.TestCheckResourceAttr(
236+
"checkly_check.test",
237+
"activated",
238+
"true",
239+
),
240+
resource.TestCheckResourceAttr(
241+
"checkly_check.test",
242+
"script",
243+
"console.log('test')",
244+
),
245+
resource.TestCheckResourceAttr(
246+
"checkly_check.test",
247+
"locations.#",
248+
"2",
249+
),
250+
testCheckResourceAttrExpr(
251+
"checkly_check.test",
252+
"locations.*",
253+
"us-east-1",
254+
),
255+
testCheckResourceAttrExpr(
256+
"checkly_check.test",
257+
"locations.*",
258+
"eu-central-1",
259+
),
260+
resource.TestCheckResourceAttr(
261+
"checkly_check.test",
262+
"tags.#",
263+
"2",
264+
),
265+
testCheckResourceAttrExpr(
266+
"checkly_check.test",
267+
"tags.*",
268+
"browser",
269+
),
270+
testCheckResourceAttrExpr(
271+
"checkly_check.test",
272+
"tags.*",
273+
"e2e",
274+
),
275+
resource.TestCheckNoResourceAttr(
276+
"checkly_check.test",
277+
"request",
278+
),
279+
),
280+
},
281+
})
282+
}
283+
215284
func TestAccApiCheckFull(t *testing.T) {
216285
accTestCase(t, []resource.TestStep{
217286
{
@@ -431,6 +500,22 @@ const browserCheck_basic = `
431500
script = "console.log('test')"
432501
}
433502
`
503+
const multiStepCheck_basic = `
504+
resource "checkly_check" "test" {
505+
name = "MultiStep Check"
506+
type = "MULTI_STEP"
507+
activated = true
508+
should_fail = false
509+
frequency = 720
510+
double_check = true
511+
use_global_alert_settings = true
512+
locations = [ "us-east-1", "eu-central-1" ]
513+
tags = [ "api", "multi-step" ]
514+
runtime_id = "2023.09"
515+
script = "console.log('test')"
516+
}
517+
`
518+
434519
const apiCheck_basic = `
435520
resource "checkly_check" "test" {
436521
name = "API Check 1"

docs/resources/check.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ resource "checkly_check" "example_check" {
192192
- `activated` (Boolean) Determines if the check is running or not. Possible values `true`, and `false`.
193193
- `frequency` (Number) The frequency in minutes to run the check. Possible values are `0`, `1`, `2`, `5`, `10`, `15`, `30`, `60`, `120`, `180`, `360`, `720`, and `1440`.
194194
- `name` (String) The name of the check.
195-
- `type` (String) The type of the check. Possible values are `API`, and `BROWSER`.
195+
- `type` (String) The type of the check. Possible values are `API`, `BROWSER`, and `MULTI_STEP`.
196196

197197
### Optional
198198

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/aws/aws-sdk-go v1.44.122 // indirect
7-
github.com/checkly/checkly-go-sdk v1.6.9
7+
github.com/checkly/checkly-go-sdk v1.7.0
88
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
99
github.com/google/go-cmp v0.5.9
1010
github.com/gruntwork-io/terratest v0.41.16

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ github.com/checkly/checkly-go-sdk v1.6.8 h1:7rQ+zrwlLYYOc0u5jxDT1+atLFnwWmaqJluD
237237
github.com/checkly/checkly-go-sdk v1.6.8/go.mod h1:Pd6tBOggAe41NnCU5KwqA8JvD6J20/IctszT2E0AvHo=
238238
github.com/checkly/checkly-go-sdk v1.6.9 h1:2Cfr2I0VnHmB4oL7BOw25WYuyz5Lm04N50k1HUsX2Lk=
239239
github.com/checkly/checkly-go-sdk v1.6.9/go.mod h1:Pd6tBOggAe41NnCU5KwqA8JvD6J20/IctszT2E0AvHo=
240+
github.com/checkly/checkly-go-sdk v1.7.0 h1:3BXWIE1YjXfWTwuhZ5bE6QkcfJbchFE/Y/pPaQtT0+c=
241+
github.com/checkly/checkly-go-sdk v1.7.0/go.mod h1:Pd6tBOggAe41NnCU5KwqA8JvD6J20/IctszT2E0AvHo=
240242
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
241243
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
242244
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=

0 commit comments

Comments
 (0)