2016-09-23 21:43:06 +02:00
|
|
|
package pagerduty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/PagerDuty/go-pagerduty"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePagerDutyEscalationPolicy() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePagerDutyEscalationPolicyCreate,
|
|
|
|
Read: resourcePagerDutyEscalationPolicyRead,
|
|
|
|
Update: resourcePagerDutyEscalationPolicyUpdate,
|
|
|
|
Delete: resourcePagerDutyEscalationPolicyDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourcePagerDutyEscalationPolicyImport,
|
|
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "Managed by Terraform",
|
|
|
|
},
|
|
|
|
"num_loops": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-10-20 06:53:51 +02:00
|
|
|
"teams": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
},
|
|
|
|
},
|
2016-10-19 07:53:19 +02:00
|
|
|
"rule": &schema.Schema{
|
2016-09-23 21:43:06 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-16 16:48:20 +02:00
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-09-23 21:43:06 +02:00
|
|
|
"escalation_delay_in_minutes": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"target": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "user_reference",
|
|
|
|
},
|
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildEscalationPolicyStruct(d *schema.ResourceData) *pagerduty.EscalationPolicy {
|
2016-10-19 07:53:19 +02:00
|
|
|
escalationRules := d.Get("rule").([]interface{})
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy := pagerduty.EscalationPolicy{
|
2016-09-23 21:43:06 +02:00
|
|
|
Name: d.Get("name").(string),
|
2016-10-20 06:53:51 +02:00
|
|
|
EscalationRules: expandEscalationRules(escalationRules),
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("description"); ok {
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy.Description = attr.(string)
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("num_loops"); ok {
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy.NumLoops = uint(attr.(int))
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("teams"); ok {
|
|
|
|
escalationPolicy.Teams = expandTeams(attr.([]interface{}))
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
return &escalationPolicy
|
2016-09-23 21:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy := buildEscalationPolicyStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
log.Printf("[INFO] Creating PagerDuty escalation policy: %s", escalationPolicy.Name)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy, err := client.CreateEscalationPolicy(*escalationPolicy)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
d.SetId(escalationPolicy.ID)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
return resourcePagerDutyEscalationPolicyRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
o := &pagerduty.GetEscalationPolicyOptions{}
|
|
|
|
|
|
|
|
escalationPolicy, err := client.GetEscalationPolicy(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", escalationPolicy.Name)
|
|
|
|
d.Set("teams", escalationPolicy.Teams)
|
|
|
|
d.Set("description", escalationPolicy.Description)
|
|
|
|
d.Set("num_loops", escalationPolicy.NumLoops)
|
2016-10-17 20:36:40 +02:00
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
|
2016-10-17 20:36:40 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
escalationPolicy := buildEscalationPolicyStruct(d)
|
2016-09-23 21:43:06 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Updating PagerDuty escalation policy: %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if _, err := client.UpdateEscalationPolicy(d.Id(), escalationPolicy); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyEscalationPolicyDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*pagerduty.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting PagerDuty escalation policy: %s", d.Id())
|
|
|
|
|
2016-10-20 06:53:51 +02:00
|
|
|
if err := client.DeleteEscalationPolicy(d.Id()); err != nil {
|
2016-09-23 21:43:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePagerDutyEscalationPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
if err := resourcePagerDutyEscalationPolicyRead(d, meta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|