2016-09-23 21:43:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
pagerduty "github.com/PagerDuty/go-pagerduty"
|
2016-09-23 21:43:06 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePagerDutyService() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePagerDutyServiceCreate,
|
|
|
|
Read: resourcePagerDutyServiceRead,
|
|
|
|
Update: resourcePagerDutyServiceUpdate,
|
|
|
|
Delete: resourcePagerDutyServiceDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
2017-02-23 20:19:35 +01:00
|
|
|
State: schema.ImportStatePassthrough,
|
2016-09-23 21:43:06 +02:00
|
|
|
},
|
|
|
|
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
|
|
|
"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
|
|
|
"auto_resolve_timeout": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"last_incident_timestamp": {
|
2016-10-18 20:37:17 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"created_at": {
|
2016-10-18 20:37:17 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"status": {
|
2016-10-18 20:37:17 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"acknowledgement_timeout": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-24 16:43:53 +02:00
|
|
|
"escalation_policy": {
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-02-13 12:41:24 +01:00
|
|
|
"incident_urgency_rule": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
2017-02-23 20:35:45 +01:00
|
|
|
Optional: true,
|
2017-02-13 12:41:24 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"urgency": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"during_support_hours": {
|
|
|
|
Type: schema.TypeList,
|
|
|
|
MaxItems: 1,
|
|
|
|
MinItems: 1,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"urgency": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"outside_support_hours": {
|
|
|
|
Type: schema.TypeList,
|
|
|
|
MaxItems: 1,
|
|
|
|
MinItems: 1,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"urgency": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"support_hours": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
MaxItems: 1,
|
|
|
|
MinItems: 1,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"time_zone": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"start_time": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"end_time": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"days_of_week": {
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
MaxItems: 7,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeInt},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"scheduled_actions": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"to_urgency": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"at": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-09-23 21:43:06 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
|
|
|
|
service := pagerduty.Service{
|
2016-10-18 20:37:17 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Status: d.Get("status").(string),
|
2016-10-18 14:54:33 +02:00
|
|
|
APIObject: pagerduty.APIObject{
|
|
|
|
ID: d.Id(),
|
|
|
|
},
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("description"); ok {
|
|
|
|
service.Description = attr.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("auto_resolve_timeout"); ok {
|
|
|
|
autoResolveTimeout := uint(attr.(int))
|
|
|
|
service.AutoResolveTimeout = &autoResolveTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("acknowledgement_timeout"); ok {
|
|
|
|
acknowledgementTimeout := uint(attr.(int))
|
|
|
|
service.AcknowledgementTimeout = &acknowledgementTimeout
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy := &pagerduty.EscalationPolicy{
|
2016-10-17 20:37:55 +02:00
|
|
|
APIObject: pagerduty.APIObject{
|
|
|
|
ID: d.Get("escalation_policy").(string),
|
2016-10-20 06:53:51 +02:00
|
|
|
Type: "escalation_policy_reference",
|
2016-10-17 20:37:55 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
service.EscalationPolicy = *escalationPolicy
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2017-02-13 12:41:24 +01:00
|
|
|
if attr, ok := d.GetOk("incident_urgency_rule"); ok {
|
|
|
|
if iur, ok := expandIncidentUrgencyRule(attr); ok {
|
|
|
|
service.IncidentUrgencyRule = iur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attr, ok := d.GetOk("support_hours"); ok {
|
|
|
|
service.SupportHours = expandSupportHours(attr)
|
|
|
|
}
|
|
|
|
if attr, ok := d.GetOk("scheduled_actions"); ok {
|
|
|
|
service.ScheduledActions = expandScheduledActions(attr)
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:43:06 +02:00
|
|
|
return &service
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
service := buildServiceStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
log.Printf("[INFO] Creating PagerDuty service %s", service.Name)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
service, err := client.CreateService(*service)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.SetId(service.ID)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading PagerDuty service %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
o := &pagerduty.GetServiceOptions{}
|
|
|
|
|
|
|
|
service, err := client.GetService(d.Id(), o)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2017-01-29 17:06:44 +01:00
|
|
|
if isNotFound(err) {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.Set("name", service.Name)
|
|
|
|
d.Set("status", service.Status)
|
|
|
|
d.Set("created_at", service.CreateAt)
|
|
|
|
d.Set("escalation_policy", service.EscalationPolicy.ID)
|
|
|
|
d.Set("description", service.Description)
|
|
|
|
d.Set("auto_resolve_timeout", service.AutoResolveTimeout)
|
|
|
|
d.Set("last_incident_timestamp", service.LastIncidentTimestamp)
|
|
|
|
d.Set("acknowledgement_timeout", service.AcknowledgementTimeout)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2017-02-13 12:41:24 +01:00
|
|
|
if incidentUrgencyRule, ok := flattenIncidentUrgencyRule(service); ok {
|
|
|
|
d.Set("incident_urgency_rule", incidentUrgencyRule)
|
|
|
|
}
|
|
|
|
|
|
|
|
supportHours := flattenSupportHours(service)
|
|
|
|
d.Set("support_hours", supportHours)
|
|
|
|
|
|
|
|
scheduledActions := flattenScheduledActions(service)
|
|
|
|
d.Set("scheduled_actions", scheduledActions)
|
|
|
|
|
2016-09-23 21:43:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
service := buildServiceStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Updating PagerDuty service %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if _, err := client.UpdateService(*service); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting PagerDuty service %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if err := client.DeleteService(d.Id()); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|