2016-09-23 21:43:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/PagerDuty/go-pagerduty"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePagerDutySchedule() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePagerDutyScheduleCreate,
|
|
|
|
Read: resourcePagerDutyScheduleRead,
|
|
|
|
Update: resourcePagerDutyScheduleUpdate,
|
|
|
|
Delete: resourcePagerDutyScheduleDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourcePagerDutyScheduleImport,
|
|
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-24 16:43:53 +02:00
|
|
|
"name": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"time_zone": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"description": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "Managed by Terraform",
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"layer": {
|
2016-10-18 14:54:33 +02:00
|
|
|
Type: schema.TypeList,
|
2016-09-23 21:43:06 +02:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-24 16:43:53 +02:00
|
|
|
"id": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"name": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-10-18 14:54:33 +02:00
|
|
|
Computed: true,
|
2016-09-23 21:43:06 +02:00
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"start": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
2016-10-18 14:54:33 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
if old == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2016-09-23 21:43:06 +02:00
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"end": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"rotation_virtual_start": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
2016-10-18 14:54:33 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
if old == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2016-09-23 21:43:06 +02:00
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"rotation_turn_length_seconds": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"users": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"restriction": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Optional: true,
|
2016-10-18 14:54:33 +02:00
|
|
|
Type: schema.TypeList,
|
2016-09-23 21:43:06 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-24 16:43:53 +02:00
|
|
|
"type": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"start_time_of_day": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"duration_seconds": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
func buildScheduleStruct(d *schema.ResourceData) *pagerduty.Schedule {
|
2016-10-19 07:49:35 +02:00
|
|
|
scheduleLayers := d.Get("layer").([]interface{})
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
schedule := pagerduty.Schedule{
|
2016-10-17 16:24:27 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
TimeZone: d.Get("time_zone").(string),
|
2016-10-20 06:53:51 +02:00
|
|
|
ScheduleLayers: expandScheduleLayers(scheduleLayers),
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("description"); ok {
|
|
|
|
schedule.Description = attr.(string)
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
return &schedule
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyScheduleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
schedule := buildScheduleStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
log.Printf("[INFO] Creating PagerDuty schedule: %s", schedule.Name)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
schedule, err := client.CreateSchedule(*schedule)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.SetId(schedule.ID)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-18 14:54:33 +02:00
|
|
|
return resourcePagerDutyScheduleRead(d, meta)
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading PagerDuty schedule: %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
schedule, err := client.GetSchedule(d.Id(), pagerduty.GetScheduleOptions{})
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.Set("name", schedule.Name)
|
|
|
|
d.Set("time_zone", schedule.TimeZone)
|
|
|
|
d.Set("description", schedule.Description)
|
2016-10-17 20:42:11 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if err := d.Set("layer", flattenScheduleLayers(schedule.ScheduleLayers)); err != nil {
|
2016-10-17 20:42:11 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyScheduleUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
schedule := buildScheduleStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Updating PagerDuty schedule: %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if _, err := client.UpdateSchedule(d.Id(), *schedule); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyScheduleDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting PagerDuty schedule: %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if err := client.DeleteSchedule(d.Id()); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyScheduleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
if err := resourcePagerDutyScheduleRead(d, meta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|