terraform/builtin/providers/pagerduty/resource_pagerduty_schedule.go

206 lines
4.7 KiB
Go
Raw Normal View History

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{
"name": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Optional: true,
},
"time_zone": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Required: true,
},
"description": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},
"layer": {
Type: schema.TypeList,
2016-09-23 21:43:06 +02:00
Required: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Computed: true,
},
"name": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Optional: true,
Computed: true,
2016-09-23 21:43:06 +02:00
},
"start": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
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
},
"end": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Optional: true,
},
"rotation_virtual_start": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
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
},
"rotation_turn_length_seconds": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeInt,
Required: true,
},
"users": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"restriction": {
2016-09-23 21:43:06 +02:00
Optional: true,
Type: schema.TypeList,
2016-09-23 21:43:06 +02:00
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Required: true,
},
"start_time_of_day": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Required: true,
},
"start_day_of_week": {
Type: schema.TypeInt,
Optional: true,
},
"duration_seconds": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
},
}
}
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),
ScheduleLayers: expandScheduleLayers(scheduleLayers),
2016-09-23 21:43:06 +02:00
}
if attr, ok := d.GetOk("description"); ok {
schedule.Description = attr.(string)
}
return &schedule
2016-09-23 21:43:06 +02:00
}
func resourcePagerDutyScheduleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
schedule := buildScheduleStruct(d)
2016-09-23 21:43:06 +02:00
log.Printf("[INFO] Creating PagerDuty schedule: %s", schedule.Name)
2016-09-23 21:43:06 +02:00
schedule, err := client.CreateSchedule(*schedule)
2016-09-23 21:43:06 +02:00
if err != nil {
return err
}
d.SetId(schedule.ID)
2016-09-23 21:43:06 +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())
schedule, err := client.GetSchedule(d.Id(), pagerduty.GetScheduleOptions{})
2016-09-23 21:43:06 +02:00
if err != nil {
return err
}
d.Set("name", schedule.Name)
d.Set("time_zone", schedule.TimeZone)
d.Set("description", schedule.Description)
if err := d.Set("layer", flattenScheduleLayers(schedule.ScheduleLayers)); err != nil {
return err
}
2016-09-23 21:43:06 +02:00
return nil
}
func resourcePagerDutyScheduleUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
schedule := buildScheduleStruct(d)
2016-09-23 21:43:06 +02:00
log.Printf("[INFO] Updating PagerDuty schedule: %s", d.Id())
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())
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
}