Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Maintenance Windows configuration #90

Merged
merged 10 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_check": resourceCheck(),
"checkly_check_group": resourceCheckGroup(),
"checkly_snippet": resourceSnippet(),
"checkly_maintenance_windows": resourceMaintenanceWindows(),
"checkly_alert_channel": resourceAlertChannel(),
// "checkly_environment_variable": resourceEnvironmentVariable(),
"checkly_alert_channel": resourceAlertChannel(),

},
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
debugLog := os.Getenv("CHECKLY_DEBUG_LOG")
Expand Down
168 changes: 168 additions & 0 deletions checkly/resource_maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
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,
},
"created_at": {
ianaya89 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
},
"updated_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),
CreatedAt: d.Get("created_at").(string),
RepeatInterval: d.Get("repeat_interval").(int),
UpdatedAt: d.Get("updated_at").(string),
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("created_at", s.CreatedAt)
d.Set("repeat_interval", s.RepeatInterval)
d.Set("updated_at", s.UpdatedAt)
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)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ require (
github.com/zclconf/go-cty v1.4.2 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
)

14 changes: 14 additions & 0 deletions test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,18 @@ resource "checkly_check_group" "group-with-alert-channels" {
channel_id = checkly_alert_channel.sms_ac.id
activated = false
}
}

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 = "WEEK"
repeat_ends_at = "2014-08-24T00:00:00.000Z"
created_at = "2014-08-24T00:00:00.000Z"
repeat_interval = 1
updated_at = "2014-08-24T00:00:00.000Z"
tags= [
"string",
]
}