Skip to content

Commit 8ec33d2

Browse files
authored
feat: add support for private locations (#160)
1 parent 8cec2c8 commit 8ec33d2

13 files changed

+269
-24
lines changed

checkly/integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build integration
12
// +build integration
23

34
package checkly

checkly/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func Provider() *schema.Provider {
4040
"checkly_trigger_check": resourceTriggerCheck(),
4141
"checkly_trigger_group": resourceTriggerGroup(),
4242
"checkly_environment_variable": resourceEnvironmentVariable(),
43+
"checkly_private_location": resourcePrivateLocation(),
4344
},
4445
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
4546
debugLog := os.Getenv("CHECKLY_DEBUG_LOG")

checkly/resource_private_locations.go

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package checkly
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
checkly "github.com/checkly/checkly-go-sdk"
11+
)
12+
13+
func resourcePrivateLocation() *schema.Resource {
14+
return &schema.Resource{
15+
Create: resourcePrivateLocationCreate,
16+
Read: resourcePrivateLocationRead,
17+
Update: resourcePrivateLocationUpdate,
18+
Delete: resourcePrivateLocationDelete,
19+
Importer: &schema.ResourceImporter{
20+
StateContext: schema.ImportStatePassthroughContext,
21+
},
22+
Schema: map[string]*schema.Schema{
23+
"name": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
Description: "The private location name.",
27+
},
28+
"slug_name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
Description: "Valid slug name.",
32+
},
33+
"icon": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Description: "Icon assigned to the private location.",
37+
},
38+
"keys": {
39+
Type: schema.TypeSet,
40+
Computed: true,
41+
Sensitive: true,
42+
Elem: &schema.Schema{
43+
Type: schema.TypeString,
44+
},
45+
Description: "Private location API keys.",
46+
},
47+
},
48+
}
49+
}
50+
51+
func privateLocationFromResourceData(d *schema.ResourceData) (checkly.PrivateLocation, error) {
52+
return checkly.PrivateLocation{
53+
Name: d.Get("name").(string),
54+
SlugName: d.Get("slug_name").(string),
55+
Icon: d.Get("icon").(string),
56+
}, nil
57+
}
58+
59+
func resourcePrivateLocationCreate(d *schema.ResourceData, client interface{}) error {
60+
pl, err := privateLocationFromResourceData(d)
61+
if err != nil {
62+
return fmt.Errorf("resourcePrivateLocationCreate: translation error: %w", err)
63+
}
64+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
65+
defer cancel()
66+
result, err := client.(checkly.Client).CreatePrivateLocation(ctx, pl)
67+
if err != nil {
68+
return fmt.Errorf("CreatePrivateLocation: API error: %w", err)
69+
}
70+
d.SetId(result.ID)
71+
72+
var keys = []string{result.Keys[0].RawKey}
73+
d.Set("keys", keys)
74+
return resourcePrivateLocationRead(d, client)
75+
}
76+
77+
func resourceDataFromPrivateLocation(pl *checkly.PrivateLocation, d *schema.ResourceData) error {
78+
d.Set("name", pl.Name)
79+
d.Set("slug_name", pl.SlugName)
80+
d.Set("icon", pl.Icon)
81+
return nil
82+
}
83+
84+
func resourcePrivateLocationUpdate(d *schema.ResourceData, client interface{}) error {
85+
pl, err := privateLocationFromResourceData(d)
86+
if err != nil {
87+
return fmt.Errorf("resourcePrivateLocationUpdate: translation error: %w", err)
88+
}
89+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
90+
defer cancel()
91+
_, err = client.(checkly.Client).UpdatePrivateLocation(ctx, d.Id(), pl)
92+
if err != nil {
93+
return fmt.Errorf("resourcePrivateLocationUpdate: API error: %w", err)
94+
}
95+
return resourcePrivateLocationRead(d, client)
96+
}
97+
98+
func resourcePrivateLocationRead(d *schema.ResourceData, client interface{}) error {
99+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
100+
defer cancel()
101+
pl, err := client.(checkly.Client).GetPrivateLocation(ctx, d.Id())
102+
if err != nil {
103+
if strings.Contains(err.Error(), "404") {
104+
//if resource is deleted remotely, then mark it as
105+
//successfully gone by unsetting it's ID
106+
d.SetId("")
107+
return nil
108+
}
109+
return fmt.Errorf("resourcePrivateLocationRead: %w", err)
110+
}
111+
return resourceDataFromPrivateLocation(pl, d)
112+
}
113+
114+
func resourcePrivateLocationDelete(d *schema.ResourceData, client interface{}) error {
115+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
116+
defer cancel()
117+
err := client.(checkly.Client).DeletePrivateLocation(ctx, d.Id())
118+
if err != nil {
119+
return fmt.Errorf("resourcePrivateLocationDelete: API error: %w", err)
120+
}
121+
return nil
122+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package checkly
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccPrivateLocationCheckRequiredFields(t *testing.T) {
11+
config := `resource "checkly_private_location" "test" {}`
12+
accTestCase(t, []resource.TestStep{
13+
{
14+
Config: config,
15+
ExpectError: regexp.MustCompile(`The argument "name" is required`),
16+
},
17+
{
18+
Config: config,
19+
ExpectError: regexp.MustCompile(`The argument "slug_name" is required`),
20+
},
21+
})
22+
}
23+
24+
func TestAccPrivateLocationSuccess(t *testing.T) {
25+
config := `resource "checkly_private_location" "test" {
26+
name = "New Private Location"
27+
slug_name = "new-private-location"
28+
icon = "location"
29+
}`
30+
accTestCase(t, []resource.TestStep{
31+
{
32+
Config: config,
33+
Check: resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttr(
35+
"checkly_private_location.test",
36+
"name",
37+
"New Private Location",
38+
),
39+
resource.TestCheckResourceAttr(
40+
"checkly_private_location.test",
41+
"slug_name",
42+
"new-private-location",
43+
),
44+
resource.TestCheckResourceAttr(
45+
"checkly_private_location.test",
46+
"icon",
47+
"location",
48+
),
49+
),
50+
},
51+
})
52+
}

demo/dashboard.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Simple public dashboard example
22
resource "checkly_dashboard" "dashboard-1" {
3-
custom_url = "tf-demo-status"
4-
tags = ["checks"]
3+
custom_url = "tf-demo-status"
4+
tags = ["checks"]
55
}
66

77
# Complete public dashboard example
@@ -14,5 +14,5 @@ resource "checkly_dashboard" "dashboard-2" {
1414
pagination_rate = 30
1515
hide_tags = false
1616
width = "960PX"
17-
tags = ["checks"]
17+
tags = ["checks"]
1818
}

demo/environment_variables.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Simple Enviroment Variable example
22
resource "checkly_environment_variable" "variable-1" {
3-
key = "API_KEY"
4-
value = "loZd9hOGHDUrGvmW"
3+
key = "API_KEY"
4+
value = "loZd9hOGHDUrGvmW"
55
locked = true
66
}
77

88
resource "checkly_environment_variable" "variable-2" {
9-
key = "API_URL"
9+
key = "API_URL"
1010
value = "http://localhost:3000"
1111
}

demo/maintenance_windows.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Simple Maintenance windows example
22
resource "checkly_maintenance_windows" "maintenance-1" {
3-
name = "MW Simple"
4-
starts_at = "2028-08-24T00:00:00.000Z"
5-
ends_at = "2028-08-25T00:00:00.000Z"
3+
name = "MW Simple"
4+
starts_at = "2028-08-24T00:00:00.000Z"
5+
ends_at = "2028-08-25T00:00:00.000Z"
66
}
77

88
# Complete Maintenance windows example

demo/private_locations.tf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "checkly_private_location" "location" {
2+
name = "New Private Location"
3+
slug_name = "new-private-location"
4+
icon = "location"
5+
}

demo/versions.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ variable "api_url" {
2020
}
2121

2222
provider "checkly" {
23-
api_key = var.api_key
23+
api_key = var.api_key
2424
account_id = var.account_id
25-
api_url = var.api_url
25+
api_url = var.api_url
2626
}

docs/resources/private_locations.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "checkly_private_location Resource - terraform-provider-checkly"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# checkly_private_location (Resource)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Simple Private Location example
17+
resource "checkly_private_location" "location" {
18+
name = "New Private Location"
19+
slug_name = "new-private-location"
20+
icon = "location"
21+
}
22+
```
23+
24+
<!-- schema generated by tfplugindocs -->
25+
## Schema
26+
27+
### Required
28+
29+
- `name` (String)
30+
- `slug_name` (String)
31+
32+
### Optional
33+
34+
- `icon` (String) The icon that will represent the private location.
35+
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "checkly_private_location" "location" {
2+
name = "New Private Location"
3+
slug_name = "new-private-location"
4+
icon = "location"
5+
}

go.mod

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ module github.com/checkly/terraform-provider-checkly
33
go 1.14
44

55
require (
6-
github.com/checkly/checkly-go-sdk v1.5.7
6+
github.com/aws/aws-sdk-go v1.42.35 // indirect
7+
github.com/checkly/checkly-go-sdk v1.6.0
8+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
79
github.com/google/go-cmp v0.5.8
810
github.com/gruntwork-io/terratest v0.40.17
911
github.com/hashicorp/terraform-plugin-sdk/v2 v2.12.0
1012
github.com/hashicorp/yamux v0.0.0-20190923154419-df201c70410d // indirect
1113
github.com/mattn/go-colorable v0.1.12 // indirect
1214
github.com/oklog/run v1.1.0 // indirect
15+
github.com/xanzy/ssh-agent v0.3.1 // indirect
16+
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
17+
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
18+
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
19+
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a // indirect
1320
)

0 commit comments

Comments
 (0)