terraform/builtin/providers/pagerduty/resource_pagerduty_escalati...

177 lines
4.3 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 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": {
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",
},
"num_loops": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeInt,
Optional: true,
},
"teams": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"rule": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
2016-10-16 16:48:20 +02:00
Type: schema.TypeString,
Computed: true,
},
"escalation_delay_in_minutes": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeInt,
Required: true,
},
"target": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
2016-09-23 21:43:06 +02:00
Type: schema.TypeString,
Optional: true,
Default: "user_reference",
},
"id": {
2016-09-23 21:43:06 +02:00
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
escalationPolicy := pagerduty.EscalationPolicy{
2016-09-23 21:43:06 +02:00
Name: d.Get("name").(string),
EscalationRules: expandEscalationRules(escalationRules),
2016-09-23 21:43:06 +02:00
}
if attr, ok := d.GetOk("description"); ok {
escalationPolicy.Description = attr.(string)
2016-09-23 21:43:06 +02:00
}
if attr, ok := d.GetOk("num_loops"); ok {
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
}
return &escalationPolicy
2016-09-23 21:43:06 +02:00
}
func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
escalationPolicy := buildEscalationPolicyStruct(d)
2016-09-23 21:43:06 +02:00
log.Printf("[INFO] Creating PagerDuty escalation policy: %s", escalationPolicy.Name)
2016-09-23 21:43:06 +02:00
escalationPolicy, err := client.CreateEscalationPolicy(*escalationPolicy)
2016-09-23 21:43:06 +02:00
if err != nil {
return err
}
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())
o := &pagerduty.GetEscalationPolicyOptions{}
escalationPolicy, err := client.GetEscalationPolicy(d.Id(), o)
2016-09-23 21:43:06 +02:00
if err != nil {
return err
}
d.Set("name", escalationPolicy.Name)
d.Set("teams", escalationPolicy.Teams)
d.Set("description", escalationPolicy.Description)
d.Set("num_loops", escalationPolicy.NumLoops)
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
return err
}
2016-09-23 21:43:06 +02:00
return nil
}
func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
escalationPolicy := buildEscalationPolicyStruct(d)
2016-09-23 21:43:06 +02:00
log.Printf("[INFO] Updating PagerDuty escalation policy: %s", d.Id())
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())
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
}