Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

WIP : Maintenance window resource #63

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
88 changes: 88 additions & 0 deletions pingdom/data_source_pingdom_maintenance_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package pingdom

import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/schema"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to use github.com/hashicorp/terraform-plugin-sdk/helper/schema?

"github.com/russellcardullo/go-pingdom/pingdom"
)

func dataSourcePingdomMaintenanceWindow() *schema.Resource {
return &schema.Resource{
Read: dataSourcePingdomUserRead,
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Required: true,
},
"from": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
},
"to": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
},
"recurrence_type": {
Type: schema.TypeString,
Required: true,
// Default: "none",
},
"repeat_every": {
Type: schema.TypeInt,
Required: true,
// Default: 0,
},
"effective_to": {
Type: schema.TypeInt,
Required: true,
},
"uptimeids": {
Type: schema.TypeSet,
Required: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeInt},
},
"tmsids": {
Type: schema.TypeSet,
Required: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}

func dataSourcePingdomMainteannceWindowRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)
id := d.Get("id").(int)
window, err := client.Maintenances.Read(id)
if err != nil {
return fmt.Errorf("Error retrieving maintenance window: %s", err)
}

d.Set("descrition", window.Description)
d.Set("from", window.From)
d.Set("to", window.To)
d.Set("recurence_type", window.RecurrenceType)
d.Set("repeat_every", window.RepeatEvery)
d.Set("effective_to", window.EffectiveTo)

uptimeids := make([]string, len(window.Checks.Uptime))
for i, x := range window.Checks.Uptime {
uptimeids[i] = strconv.Itoa(x)
}
d.Set("uptimeids", strings.Join(uptimeids, ","))

tmids := make([]string, len(window.Checks.Tms))
for i, x := range window.Checks.Tms {
tmids[i] = strconv.Itoa(x)
}
d.Set("tmids", strings.Join(tmids, ","))

return nil
}
9 changes: 5 additions & 4 deletions pingdom/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ func Provider() terraform.ResourceProvider {
},
},
ResourcesMap: map[string]*schema.Resource{
"pingdom_check": resourcePingdomCheck(),
"pingdom_team": resourcePingdomTeam(),
"pingdom_user": resourcePingdomUser(),
"pingdom_contact": resourcePingdomContact(),
"pingdom_check": resourcePingdomCheck(),
"pingdom_team": resourcePingdomTeam(),
"pingdom_user": resourcePingdomUser(),
"pingdom_contact": resourcePingdomContact(),
"pingdom_maintenance_window": resourcePingdomMaintenanceWindow(),
},
DataSourcesMap: map[string]*schema.Resource{
"pingdom_user": dataSourcePingdomUser(),
Expand Down
242 changes: 242 additions & 0 deletions pingdom/resource_pingdom_maintenance_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package pingdom

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to use github.com/hashicorp/terraform-plugin-sdk/helper/schema?

"github.com/russellcardullo/go-pingdom/pingdom"
)

func resourcePingdomMaintenanceWindow() *schema.Resource {
return &schema.Resource{
Create: resourcePingdomMaintenanceWindowCreate,
Read: resourcePingdomMaintenanceWindowRead,
Update: resourcePingdomMaintenanceWindowUpdate,
Delete: resourcePingdomMaintenanceWindowDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Required: true,
},
"from": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
},
"to": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
},
"recurrence_type": {
Type: schema.TypeString,
Optional: true,
Default: "none",
},
"repeat_every": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
"effective_to": {
Type: schema.TypeInt,
Optional: true,
},
"uptimeids": {
Type: schema.TypeSet,
Required: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeInt},
},
"tmsids": {
Type: schema.TypeSet,
Required: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}

type commonMaintenanceWindowParams struct {
Description string
From int64
To int64
RecurenceType string
RepeatEvery int
EffectiveTo int
UptimeIds []int
TmsIds []int
}

func maintenanceWindowForResource(d *schema.ResourceData) (*pingdom.MaintenanceWindow, error) {
windowParams := commonMaintenanceWindowParams{}

if v, ok := d.GetOk("description"); ok {
windowParams.Description = v.(string)
}
if v, ok := d.GetOk("from"); ok {
windowParams.From = v.(int64)
}
if v, ok := d.GetOk("to"); ok {
windowParams.To = v.(int64)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get a panic on this line: panic: interface conversion: interface {} is int, not int64. I think this and the from param should probably be changed to int as the terraform SDK only has TypeInt.

}
if v, ok := d.GetOk("recurrence_type"); ok {
windowParams.RecurenceType = v.(string)
}
if v, ok := d.GetOk("repeat_every"); ok {
windowParams.RepeatEvery = v.(int)
}
if v, ok := d.GetOk("effective_to"); ok {
windowParams.EffectiveTo = v.(int)
}
if v, ok := d.GetOk("uptimeids"); ok {
interfaceSlice := v.(*schema.Set).List()
var intSlice []int
for i := range interfaceSlice {
intSlice = append(intSlice, interfaceSlice[i].(int))
}
windowParams.UptimeIds = intSlice
}
if v, ok := d.GetOk("tmsids"); ok {
interfaceSlice := v.(*schema.Set).List()
var intSlice []int
for i := range interfaceSlice {
intSlice = append(intSlice, interfaceSlice[i].(int))
}
windowParams.TmsIds = intSlice
}

uptimeIds := ""
for _, id := range windowParams.UptimeIds {
uptimeIds += strconv.Itoa(id)
}

tmsIds := ""
for _, id := range windowParams.TmsIds {
tmsIds += strconv.Itoa(id)
}

return &pingdom.MaintenanceWindow{
Description: windowParams.Description,
From: windowParams.From,
To: windowParams.To,
RecurrenceType: windowParams.RecurenceType,
RepeatEvery: windowParams.RepeatEvery,
EffectiveTo: windowParams.EffectiveTo,
UptimeIDs: uptimeIds,
TmsIDs: tmsIds,
}, nil
}

func resourcePingdomMaintenanceWindowRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)

log.Printf("[DEBUG] Read Maintenance window with ID: %v", d.Id())
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Error retrieving id for resource: %s", err)
}

// log.Printf("========================== Maintenances")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would you mind remving the commented out code when you're ready for this to be merged?

// maintenances, err := client.Maintenances.List()
// log.Println("Maintenances: ", maintenances) // [{ID Description} ...]
// for _, m := range maintenances {
// log.Println("Maintenance: ", m)
// }

window, err := client.Maintenances.Read(id)
if err != nil {
return fmt.Errorf("Error retrieving maintenance window: %s", err)
}
log.Printf("[DEBUG] Maintenance: %+v\n", window)

d.Set("descrition", window.Description)
d.Set("from", window.From)
d.Set("to", window.To)
d.Set("recurence_type", window.RecurrenceType)
d.Set("repeat_every", window.RepeatEvery)
d.Set("effective_to", window.EffectiveTo)

uptimeIds := schema.NewSet(
func(id interface{}) int { return id.(int) },
[]interface{}{},
)
for _, id := range window.Checks.Uptime {
log.Println("Uptime: ", id)
uptimeIds.Add(id)
}
d.Set("uptimeids", uptimeIds)

tmsIds := schema.NewSet(
func(id interface{}) int { return id.(int) },
[]interface{}{},
)
for _, id := range window.Checks.Tms {
log.Println("Tm: ", id)
tmsIds.Add(id)
}
d.Set("tmsids", tmsIds)

return nil
}

func resourcePingdomMaintenanceWindowCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)

log.Printf("[DEBUG] Create Maintenance window with ID: %v", d.Id())
window, err := maintenanceWindowForResource(d)
if err != nil {
return err
}

log.Printf("[DEBUG] Maintenance window create configuration: %#v", d.Get("description"))
result, err := client.Maintenances.Create(window)
if err != nil {
return err
}

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

func resourcePingdomMaintenanceWindowDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)

log.Printf("[DEBUG] Delete Maintenance window with ID: %v", d.Id())
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Error retrieving id for resource: %s", err)
}
if _, err := client.Maintenances.Delete(id); err != nil {
return fmt.Errorf("Error deleting maintenance window: %s", err)
}
return nil
}

func resourcePingdomMaintenanceWindowUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)

log.Printf("[DEBUG] Update Maintenance window with ID: %v", d.Id())
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Error retrieving id for resource: %s", err)
}
window, err := maintenanceWindowForResource(d)
if err != nil {
return err
}

log.Printf("[DEBUG] Maintenance window update configuration: %#v", d.Get("description"))

if _, err = client.Maintenances.Update(id, window); err != nil {
return fmt.Errorf("Error updating maintenance window: %s", err)
}

return nil
}