Skip to content

Commit

Permalink
feat: add public dashboard support (#86)
Browse files Browse the repository at this point in the history
* chore: add CURD functions for dashboard support

* fix: change client url for prod one

* fix: change go-sdk route

* fix: change go-sdk route

* fix: change go-sdk route

* refactor: adapt resource to work with DashboardID

* fix: change id for dashbaordid

* refactor: change gosdk version

Co-authored-by: Pilar Checkly <pilarcheckly@192.168.1.3>
Co-authored-by: Ignacio Anaya <nacho@checklyhq.com>
  • Loading branch information
3 people authored Nov 9, 2021
1 parent 336669a commit 89065bf
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 14 deletions.
10 changes: 6 additions & 4 deletions checkly/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"checkly_check": resourceCheck(),
"checkly_check_group": resourceCheckGroup(),
"checkly_snippet": resourceSnippet(),
// "checkly_environment_variable": resourceEnvironmentVariable(),
"checkly_check": resourceCheck(),
"checkly_check_group": resourceCheckGroup(),
"checkly_snippet": resourceSnippet(),
"checkly_dashboard": resourceDashboard(),
"checkly_alert_channel": resourceAlertChannel(),
// "checkly_environment_variable": resourceEnvironmentVariable(),

},
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
debugLog := os.Getenv("CHECKLY_DEBUG_LOG")
Expand Down
157 changes: 157 additions & 0 deletions checkly/resource_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package checkly

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

checkly "github.com/checkly/checkly-go-sdk"
)

func resourceDashboard() *schema.Resource {
return &schema.Resource{
Create: resourceDashboardCreate,
Read: resourceDashboardRead,
Update: resourceDashboardUpdate,
Delete: resourceDashboardDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"custom_url": {
Type: schema.TypeString,
Required: true,
},
"custom_domain": {
Type: schema.TypeString,
Required: true,
},
"logo": {
Type: schema.TypeString,
Required: true,
},
"header": {
Type: schema.TypeString,
Required: true,
},
"width": {
Type: schema.TypeString,
Optional: true,
},
"refresh_rate": {
Type: schema.TypeInt,
Required: true,
},
"paginate": {
Type: schema.TypeBool,
Required: true,
},
"pagination_rate": {
Type: schema.TypeInt,
Required: true,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"hide_tags": {
Type: schema.TypeBool,
Required: true,
},
},
}
}

func dashboardFromResourceData(d *schema.ResourceData) (checkly.Dashboard, error) {
a := checkly.Dashboard{
CustomDomain: d.Get("custom_domain").(string),
CustomUrl: d.Get("custom_url").(string),
Logo: d.Get("logo").(string),
Header: d.Get("header").(string),
RefreshRate: d.Get("refresh_rate").(int),
Paginate: d.Get("paginate").(bool),
PaginationRate: d.Get("pagination_rate").(int),
HideTags: d.Get("hide_tags").(bool),
Width: d.Get("width").(string),
Tags: stringsFromSet(d.Get("tags").(*schema.Set)),
}

fmt.Printf("%v", a)

return a, nil
}

func resourceDataFromDashboard(s *checkly.Dashboard, d *schema.ResourceData) error {
d.Set("custom_domain", s.CustomDomain)
d.Set("custom_url", s.CustomUrl)
d.Set("logo", s.Logo)
d.Set("header", s.Header)
d.Set("refresh_rate", s.RefreshRate)
d.Set("paginate", s.Paginate)
d.Set("pagination_rate", s.PaginationRate)
d.Set("hide_tags", s.HideTags)
d.Set("tags", s.Tags)
d.Set("width", s.Width)
return nil
}

func resourceDashboardCreate(d *schema.ResourceData, client interface{}) error {
dashboard, err := dashboardFromResourceData(d)
if err != nil {
return fmt.Errorf("resourceDashboardCreate: translation error: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
result, err := client.(checkly.Client).CreateDashboard(ctx, dashboard)

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

d.SetId(result.DashboardID)
return resourceDashboardRead(d, client)
}

func resourceDashboardUpdate(d *schema.ResourceData, client interface{}) error {
dashboard, err := dashboardFromResourceData(d)
if err != nil {
return fmt.Errorf("resourceDashboardUpdate: translation error: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
result, err := client.(checkly.Client).UpdateDashboard(ctx, d.Id(), dashboard)
if err != nil {
return fmt.Errorf("resourceDashboardUpdate: API error: %w", err)
}
d.SetId(result.DashboardID)
return resourceDashboardRead(d, client)
}

func resourceDashboardDelete(d *schema.ResourceData, client interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
err := client.(checkly.Client).DeleteDashboard(ctx, d.Id())
if err != nil {
return fmt.Errorf("resourceDashboardDelete: API error: %w", err)
}
return nil
}

func resourceDashboardRead(d *schema.ResourceData, client interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
dashboard, err := client.(checkly.Client).GetDashboard(ctx, d.Id())
defer cancel()
if err != nil {
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
}
return fmt.Errorf("resourceDashboardRead: API error: %w", err)
}
return resourceDataFromDashboard(dashboard, d)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/aws/aws-sdk-go v1.31.11 // indirect
github.com/checkly/checkly-go-sdk v1.3.1
github.com/checkly/checkly-go-sdk v1.4.0
github.com/fatih/color v1.9.0 // indirect
github.com/google/go-cmp v0.5.0
github.com/gruntwork-io/terratest v0.18.3
Expand All @@ -24,4 +24,3 @@ require (
github.com/zclconf/go-cty v1.4.2 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
)

10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,8 @@ github.com/aws/aws-sdk-go v1.31.11/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/checkly/checkly-go-sdk v1.0.1 h1:VXq27maQzihS3ZdbgaNGSJckI2BvlUxUg4qu5y4fy30=
github.com/checkly/checkly-go-sdk v1.0.1/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
github.com/checkly/checkly-go-sdk v1.1.0 h1:a28JXarOuR6wQ8OO9I3p6puAnIvxjM+2XKsZL+7Mn74=
github.com/checkly/checkly-go-sdk v1.1.0/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
github.com/checkly/checkly-go-sdk v1.3.0 h1:eDaohYTyrjkPEWRmf3vDmzYseRE5Vek2i2r5yBzup7w=
github.com/checkly/checkly-go-sdk v1.3.0/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
github.com/checkly/checkly-go-sdk v1.3.1 h1:0ltTCmo3qGqbh457XcA8vhuSOLdzQ3vOpznz00A7SlY=
github.com/checkly/checkly-go-sdk v1.3.1/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
github.com/checkly/checkly-go-sdk v1.4.0 h1:SPQFD5GQ4DUNw2vtg2ww7kz6GRZ5S5ndBRChNvPRV0g=
github.com/checkly/checkly-go-sdk v1.4.0/go.mod h1:wkAoXD2cVCNQEXi9lHZqy/zONIAZc5D9frih6Gas3Rs=
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down
15 changes: 15 additions & 0 deletions test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,19 @@ resource "checkly_check_group" "group-with-alert-channels" {
channel_id = checkly_alert_channel.sms_ac.id
activated = false
}
}

resource "checkly_dashboard" "dashboard-1" {
custom_url = "testurl"
custom_domain = "testdomain"
logo= "logo"
header= "header"
refresh_rate= 60
paginate= false
pagination_rate= 30
hide_tags= false
width= "FULL"
tags= [
"string",
]
}

0 comments on commit 89065bf

Please sign in to comment.