Skip to content

Commit 9e1daa7

Browse files
author
Ignacio Anaya
committed
feat: add new environment variable resource
1 parent cfe1ffb commit 9e1daa7

File tree

9 files changed

+230
-15
lines changed

9 files changed

+230
-15
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ dist/*
1010
.local
1111
*.tfvars
1212
__debug_bin
13-
local/*
13+
local/*

checkly/provider.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ func Provider() *schema.Provider {
3131
},
3232
},
3333
ResourcesMap: map[string]*schema.Resource{
34-
"checkly_check": resourceCheck(),
35-
"checkly_check_group": resourceCheckGroup(),
36-
"checkly_snippet": resourceSnippet(),
37-
"checkly_dashboard": resourceDashboard(),
38-
"checkly_maintenance_windows": resourceMaintenanceWindow(),
39-
"checkly_alert_channel": resourceAlertChannel(),
40-
"checkly_trigger_check": resourceTriggerCheck(),
41-
"checkly_trigger_group": resourceTriggerGroup(),
42-
// "checkly_environment_variable": resourceEnvironmentVariable(),
34+
"checkly_check": resourceCheck(),
35+
"checkly_check_group": resourceCheckGroup(),
36+
"checkly_snippet": resourceSnippet(),
37+
"checkly_dashboard": resourceDashboard(),
38+
"checkly_maintenance_windows": resourceMaintenanceWindow(),
39+
"checkly_alert_channel": resourceAlertChannel(),
40+
"checkly_trigger_check": resourceTriggerCheck(),
41+
"checkly_trigger_group": resourceTriggerGroup(),
42+
"checkly_environment_variable": resourceEnvironmentVariable(),
4343
},
4444
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
4545
debugLog := os.Getenv("CHECKLY_DEBUG_LOG")
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 resourceEnvironmentVariable() *schema.Resource {
14+
return &schema.Resource{
15+
Create: resourceEnvironmentVariableCreate,
16+
Read: resourceEnvironmentVariableRead,
17+
Update: resourceEnvironmentVariableUpdate,
18+
Delete: resourceEnvironmentVariableDelete,
19+
Importer: &schema.ResourceImporter{
20+
State: schema.ImportStatePassthrough,
21+
},
22+
Schema: map[string]*schema.Schema{
23+
"key": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"value": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
},
31+
"locked": {
32+
Type: schema.TypeBool,
33+
Optional: true,
34+
Default: false,
35+
},
36+
},
37+
}
38+
}
39+
40+
func resourceEnvironmentVariableCreate(d *schema.ResourceData, client interface{}) error {
41+
envVar, err := enviromentVariableFromResourceData(d)
42+
if err != nil {
43+
return fmt.Errorf("resourceEnvironmentVariableCreate: translation error: %w", err)
44+
}
45+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
46+
defer cancel()
47+
_, err = client.(checkly.Client).CreateEnvironmentVariable(ctx, envVar)
48+
if err != nil {
49+
return fmt.Errorf("CreateEnvironmentVariable: API error: %w", err)
50+
}
51+
d.SetId(envVar.Key)
52+
return resourceEnvironmentVariableRead(d, client)
53+
}
54+
55+
func enviromentVariableFromResourceData(d *schema.ResourceData) (checkly.EnvironmentVariable, error) {
56+
return checkly.EnvironmentVariable{
57+
Key: d.Get("key").(string),
58+
Value: d.Get("value").(string),
59+
Locked: d.Get("locked").(bool),
60+
}, nil
61+
}
62+
63+
func resourceDataFromEnvironemtnVariable(s *checkly.EnvironmentVariable, d *schema.ResourceData) error {
64+
d.Set("key", s.Key)
65+
d.Set("value", s.Value)
66+
d.Set("locked", s.Locked)
67+
return nil
68+
}
69+
70+
func resourceEnvironmentVariableRead(d *schema.ResourceData, client interface{}) error {
71+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
72+
defer cancel()
73+
envVar, err := client.(checkly.Client).GetEnvironmentVariable(ctx, d.Id())
74+
if err != nil {
75+
if strings.Contains(err.Error(), "404") {
76+
//if resource is deleted remotely, then mark it as
77+
//successfully gone by unsetting it's ID
78+
d.SetId("")
79+
return nil
80+
}
81+
return fmt.Errorf("resourceEnvironmentVariableRead: API error: %w", err)
82+
}
83+
return resourceDataFromEnvironemtnVariable(envVar, d)
84+
}
85+
86+
func resourceEnvironmentVariableUpdate(d *schema.ResourceData, client interface{}) error {
87+
envVar, err := enviromentVariableFromResourceData(d)
88+
if err != nil {
89+
return fmt.Errorf("resourceEnvironmentVariableUpdate: translation error: %w", err)
90+
}
91+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
92+
defer cancel()
93+
_, err = client.(checkly.Client).UpdateEnvironmentVariable(ctx, d.Id(), envVar)
94+
if err != nil {
95+
return fmt.Errorf("resourceEnvironmentVariableUpdate: API error: %w", err)
96+
}
97+
98+
return resourceEnvironmentVariableRead(d, client)
99+
}
100+
101+
func resourceEnvironmentVariableDelete(d *schema.ResourceData, client interface{}) error {
102+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
103+
defer cancel()
104+
err := client.(checkly.Client).DeleteEnvironmentVariable(ctx, d.Id())
105+
if err != nil {
106+
return fmt.Errorf("resourceEnvironmentVariableDelete: API error: %w", err)
107+
}
108+
return nil
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 TestAccEnvVarCheckRequiredFields(t *testing.T) {
11+
config := `resource "checkly_environment_variable" "test" {}`
12+
accTestCase(t, []resource.TestStep{
13+
{
14+
Config: config,
15+
ExpectError: regexp.MustCompile(`The argument "key" is required`),
16+
},
17+
{
18+
Config: config,
19+
ExpectError: regexp.MustCompile(`The argument "value" is required`),
20+
},
21+
})
22+
}
23+
24+
func TestAccEnvVarSuccess(t *testing.T) {
25+
config := `resource "checkly_environment_variable" "test" {
26+
key = "API_URL"
27+
value = "https://api.checklyhq.com"
28+
}`
29+
accTestCase(t, []resource.TestStep{
30+
{
31+
Config: config,
32+
Check: resource.ComposeTestCheckFunc(
33+
resource.TestCheckResourceAttr(
34+
"checkly_environment_variable.test",
35+
"key",
36+
"API_URL",
37+
),
38+
resource.TestCheckResourceAttr(
39+
"checkly_environment_variable.test",
40+
"value",
41+
"https://api.checklyhq.com",
42+
),
43+
),
44+
},
45+
})
46+
}

demo/environment_variables.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Simple Enviroment Variable example
2+
resource "checkly_environment_variable" "variable-1" {
3+
key = "API_KEY"
4+
value = "loZd9hOGHDUrGvmW"
5+
locked = true
6+
}
7+
8+
resource "checkly_environment_variable" "variable-2" {
9+
key = "API_URL"
10+
value = "http://localhost:3000"
11+
}

docs/index.md

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ resource "checkly_check" "example-check" {
5656
should_fail = false
5757
frequency = 10
5858
double_check = true
59-
ssl_check = true
6059
use_global_alert_settings = true
6160
6261
locations = [

docs/resources/check.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ resource "checkly_check" "example-check" {
2121
should_fail = false
2222
frequency = 1
2323
double_check = true
24-
ssl_check = true
2524
use_global_alert_settings = true
2625
2726
locations = [
@@ -115,7 +114,6 @@ resource "checkly_check" "browser-check-1" {
115114
should_fail = false
116115
frequency = 10
117116
double_check = true
118-
ssl_check = true
119117
use_global_alert_settings = true
120118
locations = [
121119
"us-west-1"
@@ -150,7 +148,6 @@ resource "checkly_check" "browser-check-1" {
150148
should_fail = false
151149
frequency = 10
152150
double_check = true
153-
ssl_check = true
154151
use_global_alert_settings = true
155152
locations = [
156153
"us-west-1"
@@ -221,7 +218,7 @@ resource "checkly_check" "example-check" {
221218
- `script` (String) A valid piece of Node.js JavaScript code describing a browser interaction with the Puppeteer/Playwright framework or a reference to an external JavaScript file.
222219
- `setup_snippet_id` (Number) An ID reference to a snippet to use in the setup phase of an API check.
223220
- `should_fail` (Boolean) Allows to invert the behaviour of when a check is considered to fail. Allows for validating error status like 404.
224-
- `ssl_check` (Boolean) Determines if the SSL certificate should be validated for expiry.
221+
- `ssl_check` (Boolean, Deprecated) Determines if the SSL certificate should be validated for expiry.
225222
- `tags` (Set of String) A list of tags for organizing and filtering checks.
226223
- `teardown_snippet_id` (Number) An ID reference to a snippet to use in the teardown phase of an API check.
227224
- `use_global_alert_settings` (Boolean) When true, the account level alert settings will be used, not the alert setting defined on this check.
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "checkly_environment_variable Resource - terraform-provider-checkly"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# checkly_environment_variable (Resource)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Simple Enviroment Variable example
17+
resource "checkly_environment_variable" "variable-1" {
18+
key = "API_KEY"
19+
value = "loZd9hOGHDUrGvmW"
20+
locked = true
21+
}
22+
23+
resource "checkly_environment_variable" "variable-2" {
24+
key = "API_URL"
25+
value = "http://localhost:3000"
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- `key` (String)
35+
- `value` (String)
36+
37+
### Optional
38+
39+
- `id` (String) The ID of this resource.
40+
- `locked` (Boolean)
41+
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Simple Enviroment Variable example
2+
resource "checkly_environment_variable" "variable-1" {
3+
key = "API_KEY"
4+
value = "loZd9hOGHDUrGvmW"
5+
locked = true
6+
}
7+
8+
resource "checkly_environment_variable" "variable-2" {
9+
key = "API_URL"
10+
value = "http://localhost:3000"
11+
}

0 commit comments

Comments
 (0)