Skip to content

Commit

Permalink
feat: add support for Maintenance Windows (#90)
Browse files Browse the repository at this point in the history
* fix: fix id on maintenance window struct

* fix: remove createAt and UpdateAt fields from struct

* fix: change client for prod one

* fix: change gosdk for prod one

* fix: change gosdk version

* refactor: change gosdk version

* fix: add test data for dashboard

* style: indentation fixed

* style: indentation fixed

Co-authored-by: Pilar Checkly <pilarcheckly@192.168.1.3>
  • Loading branch information
pilimartinez and Pilar Checkly authored Nov 9, 2021
1 parent 89065bf commit 209d7ac
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 12 deletions.
1 change: 1 addition & 0 deletions checkly/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Provider() *schema.Provider {
"checkly_check_group": resourceCheckGroup(),
"checkly_snippet": resourceSnippet(),
"checkly_dashboard": resourceDashboard(),
"checkly_maintenance_windows": resourceMaintenanceWindows(),
"checkly_alert_channel": resourceAlertChannel(),
// "checkly_environment_variable": resourceEnvironmentVariable(),

Expand Down
156 changes: 156 additions & 0 deletions checkly/resource_maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package checkly

import (
"context"
"fmt"
"strconv"
"strings"

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

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

func resourceMaintenanceWindows() *schema.Resource {
return &schema.Resource{
Create: resourceMaintenanceWindowsCreate,
Read: resourceMaintenanceWindowsRead,
Update: resourceMaintenanceWindowsUpdate,
Delete: resourceMaintenanceWindowsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"starts_at": {
Type: schema.TypeString,
Required: true,
},
"ends_at": {
Type: schema.TypeString,
Required: true,
},
"repeat_unit": {
Type: schema.TypeString,
Required: true,
},
"repeat_ends_at": {
Type: schema.TypeString,
Required: true,
},
"repeat_interval": {
Type: schema.TypeInt,
Required: true,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func maintenanceWindowsFromResourceData(d *schema.ResourceData) (checkly.MaintenanceWindow, error) {
ID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
if d.Id() != "" {
return checkly.MaintenanceWindow{}, err
}
ID = 0
}
a := checkly.MaintenanceWindow{
ID: ID,
Name: d.Get("name").(string),
StartsAt: d.Get("starts_at").(string),
EndsAt: d.Get("ends_at").(string),
RepeatUnit: d.Get("repeat_unit").(string),
RepeatEndsAt: d.Get("repeat_ends_at").(string),
RepeatInterval: d.Get("repeat_interval").(int),
Tags: stringsFromSet(d.Get("tags").(*schema.Set)),
}

fmt.Printf("%v", a)

return a, nil
}

func resourceDataFromMaintenanceWindows(s *checkly.MaintenanceWindow, d *schema.ResourceData) error {
d.Set("name", s.Name)
d.Set("starts_at", s.StartsAt)
d.Set("ends_at", s.EndsAt)
d.Set("repeat_unit", s.RepeatUnit)
d.Set("repeat_ends_at", s.RepeatEndsAt)
d.Set("repeat_interval", s.RepeatInterval)
d.Set("tags", s.Tags)
return nil
}

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

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

d.SetId(fmt.Sprintf("%d", result.ID))
return resourceMaintenanceWindowsRead(d, client)
}

func resourceMaintenanceWindowsUpdate(d *schema.ResourceData, client interface{}) error {
mw, err := maintenanceWindowsFromResourceData(d)
if err != nil {
return fmt.Errorf("resourceMaintenanceWindowUpdate: translation error: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
_, err = client.(checkly.Client).UpdateMaintenanceWindow(ctx, mw.ID, mw)
if err != nil {
return fmt.Errorf("resourceMaintenanceWindowUpdate: API error: %w", err)
}
d.SetId(fmt.Sprintf("%d", mw.ID))
return resourceMaintenanceWindowsRead(d, client)
}

func resourceMaintenanceWindowsDelete(d *schema.ResourceData, client interface{}) error {
ID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return fmt.Errorf("resourceMaintenanceWindowsDelete: ID %s is not numeric: %w", d.Id(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
defer cancel()
err = client.(checkly.Client).DeleteMaintenanceWindow(ctx, ID)
if err != nil {
return fmt.Errorf("resourceMaintenanceWindowsDelete: API error: %w", err)
}
return nil
}

func resourceMaintenanceWindowsRead(d *schema.ResourceData, client interface{}) error {
ID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return fmt.Errorf("resourceMaintenanceWindowsRead: ID %s is not numeric: %w", d.Id(), err)
}
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
mw, err := client.(checkly.Client).GetMaintenanceWindow(ctx, ID)
defer cancel()
if err != nil {
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
}
return fmt.Errorf("resourceMaintenanceWindowsRead: API error: %w", err)
}
return resourceDataFromMaintenanceWindows(mw, d)
}
36 changes: 24 additions & 12 deletions test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -538,17 +538,29 @@ resource "checkly_check_group" "group-with-alert-channels" {
}
}

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= [
resource "checkly_maintenance_windows" "maintenance-1" {
name = "string"
starts_at = "2014-08-24T00:00:00.000Z"
ends_at = "2014-08-25T00:00:00.000Z"
repeat_unit = "MONTH"
repeat_ends_at = "2014-08-24T00:00:00.000Z"
repeat_interval = 1
tags = [
"string",
]
}
}

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 209d7ac

Please sign in to comment.