-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Maintenance Windows (#90)
* 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
1 parent
89065bf
commit 209d7ac
Showing
3 changed files
with
181 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters