2016-09-23 21:43:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/PagerDuty/go-pagerduty"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePagerDutyService() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePagerDutyServiceCreate,
|
|
|
|
Read: resourcePagerDutyServiceRead,
|
|
|
|
Update: resourcePagerDutyServiceUpdate,
|
|
|
|
Delete: resourcePagerDutyServiceDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourcePagerDutyServiceImport,
|
|
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "Managed by Terraform",
|
|
|
|
},
|
|
|
|
"auto_resolve_timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"acknowledgement_timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"escalation_policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildServiceStruct(d *schema.ResourceData) *pagerduty.Service {
|
|
|
|
service := pagerduty.Service{
|
|
|
|
Name: d.Get("name").(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-17 20:37:55 +02:00
|
|
|
policy := &pagerduty.EscalationPolicy{
|
|
|
|
APIObject: pagerduty.APIObject{
|
|
|
|
ID: d.Get("escalation_policy").(string),
|
|
|
|
Type: "escalation_policy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:43:06 +02:00
|
|
|
service.EscalationPolicy = *policy
|
|
|
|
|
|
|
|
return &service
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
s := buildServiceStruct(d)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Creating PagerDuty service %s", s.Name)
|
|
|
|
|
|
|
|
s, err := client.CreateService(*s)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(s.ID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading PagerDuty service %s", d.Id())
|
|
|
|
|
|
|
|
s, err := client.GetService(d.Id(), pagerduty.GetServiceOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", s.Name)
|
2016-10-16 16:48:20 +02:00
|
|
|
d.Set("escalation_policy", s.EscalationPolicy.ID)
|
2016-09-23 21:43:06 +02:00
|
|
|
d.Set("description", s.Description)
|
|
|
|
d.Set("auto_resolve_timeout", s.AutoResolveTimeout)
|
|
|
|
d.Set("acknowledgement_timeout", s.AcknowledgementTimeout)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
s := buildServiceStruct(d)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Updating PagerDuty service %s", d.Id())
|
|
|
|
|
|
|
|
s, err := client.UpdateService(*s)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
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())
|
|
|
|
|
|
|
|
err := client.DeleteService(d.Id())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyServiceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
if err := resourcePagerDutyServiceRead(d, meta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|