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 managing maintenance windows #17

Merged
merged 5 commits into from
Jul 10, 2017
Merged
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
33 changes: 33 additions & 0 deletions pagerduty/import_pagerduty_maintenance_window_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pagerduty

import (
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccPagerDutyMaintenanceWindow_import(t *testing.T) {
window := fmt.Sprintf("tf-%s", acctest.RandString(5))
windowStartTime := time.Now().Add(24 * time.Hour).Format(time.RFC3339)
windowEndTime := time.Now().Add(48 * time.Hour).Format(time.RFC3339)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyTeamMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyMaintenanceWindowConfig(window, windowStartTime, windowEndTime),
},

{
ResourceName: "pagerduty_maintenance_window.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
3 changes: 2 additions & 1 deletion pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"pagerduty_addon": resourcePagerDutyAddon(),
"pagerduty_escalation_policy": resourcePagerDutyEscalationPolicy(),
"pagerduty_maintenance_window": resourcePagerDutyMaintenanceWindow(),
"pagerduty_schedule": resourcePagerDutySchedule(),
"pagerduty_service_integration": resourcePagerDutyServiceIntegration(),
"pagerduty_service": resourcePagerDutyService(),
"pagerduty_service_integration": resourcePagerDutyServiceIntegration(),
"pagerduty_team": resourcePagerDutyTeam(),
"pagerduty_team_membership": resourcePagerDutyTeamMembership(),
"pagerduty_user": resourcePagerDutyUser(),
Expand Down
148 changes: 148 additions & 0 deletions pagerduty/resource_pagerduty_maintenance_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package pagerduty

import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func resourcePagerDutyMaintenanceWindow() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyMaintenanceWindowCreate,
Read: resourcePagerDutyMaintenanceWindowRead,
Update: resourcePagerDutyMaintenanceWindowUpdate,
Delete: resourcePagerDutyMaintenanceWindowDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"start_time": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateRFC3339,
},
"end_time": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateRFC3339,
},

"services": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"description": {
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},
},
}
}

func buildMaintenanceWindowStruct(d *schema.ResourceData) *pagerduty.MaintenanceWindow {
window := &pagerduty.MaintenanceWindow{
StartTime: d.Get("start_time").(string),
EndTime: d.Get("end_time").(string),
Services: expandServices(d.Get("services")),
}

if v, ok := d.GetOk("description"); ok {
window.Description = v.(string)
}

return window
}

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

window := buildMaintenanceWindowStruct(d)

log.Printf("[INFO] Creating PagerDuty maintenance window")

window, _, err := client.MaintenanceWindows.Create(window)
if err != nil {
return err
}

d.SetId(window.ID)

return nil
}

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

log.Printf("[INFO] Reading PagerDuty maintenance window %s", d.Id())

window, _, err := client.MaintenanceWindows.Get(d.Id())
if err != nil {
return handleNotFoundError(err, d)
}

d.Set("description", window.Description)
d.Set("start_time", window.StartTime)
d.Set("end_time", window.EndTime)

if err := d.Set("services", flattenServices(window.Services)); err != nil {
return err
}

return nil
}

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

window := buildMaintenanceWindowStruct(d)

log.Printf("[INFO] Updating PagerDuty maintenance window %s", d.Id())

if _, _, err := client.MaintenanceWindows.Update(d.Id(), window); err != nil {
return err
}

return nil
}

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

log.Printf("[INFO] Deleting PagerDuty maintenance window %s", d.Id())

if _, err := client.MaintenanceWindows.Delete(d.Id()); err != nil {
return err
}

d.SetId("")

return nil
}

func expandServices(v interface{}) []*pagerduty.ServiceReference {
var services []*pagerduty.ServiceReference

for _, srv := range v.([]interface{}) {
service := &pagerduty.ServiceReference{
Type: "service_reference",
ID: srv.(string),
}
services = append(services, service)
}

return services
}

func flattenServices(v []*pagerduty.ServiceReference) []interface{} {
var services []interface{}

for _, srv := range v {
services = append(services, srv.ID)
}

return services
}
Loading