Skip to content

Commit 7e80cbd

Browse files
kjdabitfield
andauthored
fixing optional configurations behaviour (#22)
* requiring checkly-sdk v0.4.2 * added a makefile * changed plan command * possible fix to issue #14 by forcing defaults to be there for optional parameters * changed commands * making some basic_auth/query_parameters/headers work as optional configs * removing request default for a browser check * Update resource_check.go Co-authored-by: John Arundel <[email protected]> * putting some examples back into RM * better checking for check type * added a note about secret data Co-authored-by: John Arundel <[email protected]>
1 parent 92989e2 commit 7e80cbd

File tree

6 files changed

+374
-126
lines changed

6 files changed

+374
-126
lines changed

Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
setup:
2+
go get github.com/hashicorp/terraform
3+
go get github.com/terraform-providers/terraform-provider-template
4+
go install github.com/hashicorp/terraform
5+
go install github.com/terraform-providers/terraform-provider-template
6+
#After running the above commands, both Terraform core and the template
7+
# provider will both be installed in the current GOPATH and $GOPATH/bin
8+
# will contain both terraform and terraform-provider-template executables.
9+
# This terraform executable will find and use the template provider plugin
10+
# alongside it in the bin directory in preference to downloading and
11+
# installing an official release.
12+
13+
replace-dep:
14+
go mod edit -replace github.com/checkly/checkly-go-sdk=../checkly-go-sdk
15+
16+
plan:
17+
go build -o terraform-provider-checkly
18+
terraform init
19+
terraform plan
20+
21+
apply:
22+
terraform apply

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ export TF_VAR_checkly_api_key=<my_api_key>
6363
```
6464

6565
## Using the provider
66+
We are working on more fleshed out examples and documentation at the moment. For more usage examples please look into `test.tf`(https://github.com/checkly/terraform-provider-checkly/blob/master/test.tf) to run these examples, set env variable `TF_VAR_checkly_api_key` to your checkly API key, then run `make plan` followed by `make apply`.
6667

67-
We are working on more fleshed out examples and documentation at the moment. Make sure to also test the [`test.ts`](https://github.com/checkly/terraform-provider-checkly/blob/master/test.tf) file in this repo for resources we use in our integration tests.
68+
Before we have full Terraform-style documentation, make sure to also reference the [Checkly public API documentation](https://www.checklyhq.com/docs/api) as the Terraform provider *talks* to this API.
6869

69-
> Before we have full Terraform-style documentation, make sure to also reference the [Checkly public API documentation](https://www.checklyhq.com/docs/api) as the Terraform provider *talks* to this API.
70+
We are still working on more fleshed out examples and documentation at the moment.
7071

7172
### Checks
7273

@@ -297,6 +298,8 @@ resource "checkly_check" "test-check1" {
297298

298299
The `group_order` attribute specifies in which order the checks will be executed: 1, 2, 3, etc.
299300

301+
302+
300303
## Developing the provider
301304

302305
Clone the repo, build the project and add it to your Terraform plugins directory. You will need to have Go installed.

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx2
110110
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
111111
github.com/checkly/checkly-go-sdk v0.4.1 h1:EfYOjVRESRYAHUTbz/AQsY3t0cOP8s2d0iO0sw36hIk=
112112
github.com/checkly/checkly-go-sdk v0.4.1/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
113+
github.com/checkly/checkly-go-sdk v0.4.2 h1:7FaHIQex8cVQ3pkqGj3kmc9RuLUXHYFuL869BJHYcTM=
114+
github.com/checkly/checkly-go-sdk v0.4.2/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
113115
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
114116
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
115117
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
@@ -423,6 +425,7 @@ github.com/svanharmelen/jsonapi v0.0.0-20180618144545-0c0828c3f16d/go.mod h1:BST
423425
github.com/tencentcloud/tencentcloud-sdk-go v3.0.82+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
424426
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20190808065407-f07404cefc8c/go.mod h1:wk2XFUg6egk4tSDNZtXeKfe2G6690UVyt163PuUxBZk=
425427
github.com/terraform-providers/terraform-provider-openstack v1.15.0/go.mod h1:2aQ6n/BtChAl1y2S60vebhyJyZXBsuAI5G4+lHrT1Ew=
428+
github.com/terraform-providers/terraform-provider-template v1.0.0/go.mod h1:/J+B8me5DCMa0rEBH5ic2aKPjhtpWNeScmxFJWxB1EU=
426429
github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
427430
github.com/ugorji/go v0.0.0-20180813092308-00b869d2f4a5/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
428431
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=

resource_check.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"sort"
67
"time"
@@ -244,10 +245,18 @@ func resourceCheck() *schema.Resource {
244245
"headers": {
245246
Type: schema.TypeMap,
246247
Optional: true,
248+
Computed: true,
249+
DefaultFunc: func() (interface{}, error) {
250+
return []tfMap{}, nil
251+
},
247252
},
248253
"query_parameters": {
249254
Type: schema.TypeMap,
250255
Optional: true,
256+
Computed: true,
257+
DefaultFunc: func() (interface{}, error) {
258+
return []tfMap{}, nil
259+
},
251260
},
252261
"follow_redirects": {
253262
Type: schema.TypeBool,
@@ -288,7 +297,12 @@ func resourceCheck() *schema.Resource {
288297
},
289298
"basic_auth": {
290299
Type: schema.TypeSet,
291-
Required: true,
300+
MaxItems: 1,
301+
Optional: true,
302+
Computed: true,
303+
DefaultFunc: func()(interface{}, error){
304+
return []tfMap{}, nil
305+
},
292306
Elem: &schema.Resource{
293307
Schema: map[string]*schema.Schema{
294308
"username": {
@@ -320,11 +334,12 @@ func resourceCheck() *schema.Resource {
320334
func resourceCheckCreate(d *schema.ResourceData, client interface{}) error {
321335
check, err := checkFromResourceData(d)
322336
if err != nil {
323-
return fmt.Errorf("translation error: %v", err)
337+
return fmt.Errorf("translation error: %w", err)
324338
}
325339
gotCheck, err := client.(*checkly.Client).Create(check)
326340
if err != nil {
327-
return fmt.Errorf("API error: %v", err)
341+
checkJSON, _ := json.Marshal(check)
342+
return fmt.Errorf("API error 1: %s, Check: %s", err.Error(), string(checkJSON))
328343
}
329344
d.SetId(gotCheck.ID)
330345
return resourceCheckRead(d, client)
@@ -333,27 +348,29 @@ func resourceCheckCreate(d *schema.ResourceData, client interface{}) error {
333348
func resourceCheckRead(d *schema.ResourceData, client interface{}) error {
334349
check, err := client.(*checkly.Client).Get(d.Id())
335350
if err != nil {
336-
return fmt.Errorf("API error: %v", err)
351+
return fmt.Errorf("API error 2: could not read check %s, Error: %s", d.Id(), err.Error())
337352
}
338353
return resourceDataFromCheck(&check, d)
339354
}
340355

341356
func resourceCheckUpdate(d *schema.ResourceData, client interface{}) error {
342357
check, err := checkFromResourceData(d)
358+
343359
if err != nil {
344-
return fmt.Errorf("translation error: %v", err)
360+
return fmt.Errorf("translation error: %s", err.Error())
345361
}
346362
_, err = client.(*checkly.Client).Update(check.ID, check)
347363
if err != nil {
348-
return fmt.Errorf("API error: %v", err)
364+
checkJSON, _ := json.Marshal(check)
365+
return fmt.Errorf("API error 3: Couldn't update check, Error: %s, \nCheck: %s", err.Error(), checkJSON)
349366
}
350367
d.SetId(check.ID)
351368
return resourceCheckRead(d, client)
352369
}
353370

354371
func resourceCheckDelete(d *schema.ResourceData, client interface{}) error {
355372
if err := client.(*checkly.Client).Delete(d.Id()); err != nil {
356-
return fmt.Errorf("API error: %v", err)
373+
return fmt.Errorf("API error 4: Couldn't delete Check %s, Error: %s", d.Id(), err.Error())
357374
}
358375
return nil
359376
}
@@ -384,8 +401,11 @@ func resourceDataFromCheck(c *checkly.Check, d *schema.ResourceData) error {
384401
return fmt.Errorf("error setting alert settings for resource %s: %s", d.Id(), err)
385402
}
386403
d.Set("use_global_alert_settings", c.UseGlobalAlertSettings)
387-
if err := d.Set("request", setFromRequest(c.Request)); err != nil {
388-
return fmt.Errorf("error setting request for resource %s: %s", d.Id(), err)
404+
if c.Type == checkly.TypeAPI {
405+
err := d.Set("request", setFromRequest(c.Request));
406+
if err != nil {
407+
return fmt.Errorf("error setting request for resource %s: %s", d.Id(), err)
408+
}
389409
}
390410
d.Set("group_id", c.GroupID)
391411
d.Set("group_order", c.GroupOrder)
@@ -467,6 +487,9 @@ func mapFromKeyValues(kvs []checkly.KeyValue) tfMap {
467487
}
468488

469489
func setFromBasicAuth(b checkly.BasicAuth) []tfMap {
490+
if b.Username == "" && b.Password == "" {
491+
return []tfMap{}
492+
}
470493
return []tfMap{
471494
{
472495
"username": b.Username,
@@ -498,10 +521,13 @@ func checkFromResourceData(d *schema.ResourceData) (checkly.Check, error) {
498521
LocalTearDownScript: d.Get("local_teardown_script").(string),
499522
AlertSettings: alertSettingsFromSet(d.Get("alert_settings").(*schema.Set)),
500523
UseGlobalAlertSettings: d.Get("use_global_alert_settings").(bool),
501-
Request: requestFromSet(d.Get("request").(*schema.Set)),
502524
GroupID: int64(d.Get("group_id").(int)),
503525
GroupOrder: d.Get("group_order").(int),
504526
}
527+
if check.Type == "API" {
528+
// this will prevent subsequent apply from causing a tf config change in browser checks
529+
check.Request = requestFromSet(d.Get("request").(*schema.Set))
530+
}
505531
return check, nil
506532
}
507533

resource_check_group.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,18 @@ func resourceCheckGroup() *schema.Resource {
160160
},
161161
"api_check_defaults": {
162162
Type: schema.TypeSet,
163-
Optional: true,
164163
MaxItems: 1,
164+
Optional: true,
165+
Computed: true,
166+
DefaultFunc: func() (interface{}, error) {
167+
return []tfMap{
168+
tfMap{
169+
"url": "",
170+
"headers": []tfMap{},
171+
"query_parameters": []tfMap{},
172+
"basic_auth": tfMap{},
173+
}}, nil
174+
},
165175
Elem: &schema.Resource{
166176
Schema: map[string]*schema.Schema{
167177
"url": {
@@ -171,10 +181,18 @@ func resourceCheckGroup() *schema.Resource {
171181
"headers": {
172182
Type: schema.TypeMap,
173183
Optional: true,
184+
Computed: true,
185+
DefaultFunc: func()(interface{}, error){
186+
return []tfMap{}, nil
187+
},
174188
},
175189
"query_parameters": {
176190
Type: schema.TypeMap,
177191
Optional: true,
192+
Computed: true,
193+
DefaultFunc: func()(interface{}, error){
194+
return []tfMap{}, nil
195+
},
178196
},
179197
"assertion": {
180198
Type: schema.TypeSet,
@@ -202,7 +220,12 @@ func resourceCheckGroup() *schema.Resource {
202220
},
203221
"basic_auth": {
204222
Type: schema.TypeSet,
205-
Required: true,
223+
MaxItems: 1,
224+
Optional: true,
225+
Computed: true,
226+
DefaultFunc: func()(interface{}, error){
227+
return []tfMap{}, nil
228+
},
206229
Elem: &schema.Resource{
207230
Schema: map[string]*schema.Schema{
208231
"username": {
@@ -329,6 +352,7 @@ func setFromAPICheckDefaults(a checkly.APICheckDefaults) []tfMap {
329352
s["headers"] = mapFromKeyValues(a.Headers)
330353
s["query_parameters"] = mapFromKeyValues(a.QueryParameters)
331354
s["assertion"] = setFromAssertions(a.Assertions)
355+
332356
s["basic_auth"] = setFromBasicAuth(a.BasicAuth)
333357
return []tfMap{s}
334358
}
@@ -338,11 +362,26 @@ func apiCheckDefaultsFromSet(s *schema.Set) checkly.APICheckDefaults {
338362
return checkly.APICheckDefaults{}
339363
}
340364
res := s.List()[0].(tfMap)
365+
341366
return checkly.APICheckDefaults{
342367
BaseURL: res["url"].(string),
343368
Headers: keyValuesFromMap(res["headers"].(tfMap)),
344369
QueryParameters: keyValuesFromMap(res["query_parameters"].(tfMap)),
345370
Assertions: assertionsFromSet(res["assertion"].(*schema.Set)),
346-
BasicAuth: basicAuthFromSet(res["basic_auth"].(*schema.Set)),
371+
BasicAuth: checkGroupBasicAuthFromSet(res["basic_auth"].(*schema.Set)),
347372
}
348373
}
374+
375+
func checkGroupBasicAuthFromSet(s *schema.Set) checkly.BasicAuth {
376+
if s.Len() == 0 {
377+
return checkly.BasicAuth{
378+
Username: "",
379+
Password: "",
380+
}
381+
}
382+
res := s.List()[0].(tfMap)
383+
return checkly.BasicAuth{
384+
Username: res["username"].(string),
385+
Password: res["password"].(string),
386+
}
387+
}

0 commit comments

Comments
 (0)