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{
|
|
|
|
State: resourcePagerDutyServiceImport,
|
|
|
|
},
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|