github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/resource_pagerduty_escalation_policy.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/PagerDuty/go-pagerduty"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourcePagerDutyEscalationPolicy() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourcePagerDutyEscalationPolicyCreate,
    13  		Read:   resourcePagerDutyEscalationPolicyRead,
    14  		Update: resourcePagerDutyEscalationPolicyUpdate,
    15  		Delete: resourcePagerDutyEscalationPolicyDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: schema.ImportStatePassthrough,
    18  		},
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  			"description": {
    25  				Type:     schema.TypeString,
    26  				Optional: true,
    27  				Default:  "Managed by Terraform",
    28  			},
    29  			"num_loops": {
    30  				Type:     schema.TypeInt,
    31  				Optional: true,
    32  			},
    33  			"teams": {
    34  				Type:     schema.TypeList,
    35  				Optional: true,
    36  				Elem: &schema.Schema{
    37  					Type: schema.TypeString,
    38  				},
    39  			},
    40  			"rule": {
    41  				Type:     schema.TypeList,
    42  				Required: true,
    43  				Elem: &schema.Resource{
    44  					Schema: map[string]*schema.Schema{
    45  						"id": {
    46  							Type:     schema.TypeString,
    47  							Computed: true,
    48  						},
    49  						"escalation_delay_in_minutes": {
    50  							Type:     schema.TypeInt,
    51  							Required: true,
    52  						},
    53  						"target": {
    54  							Type:     schema.TypeList,
    55  							Required: true,
    56  							Elem: &schema.Resource{
    57  								Schema: map[string]*schema.Schema{
    58  									"type": {
    59  										Type:     schema.TypeString,
    60  										Optional: true,
    61  										Default:  "user_reference",
    62  									},
    63  									"id": {
    64  										Type:     schema.TypeString,
    65  										Required: true,
    66  									},
    67  								},
    68  							},
    69  						},
    70  					},
    71  				},
    72  			},
    73  		},
    74  	}
    75  }
    76  
    77  func buildEscalationPolicyStruct(d *schema.ResourceData) *pagerduty.EscalationPolicy {
    78  	escalationRules := d.Get("rule").([]interface{})
    79  
    80  	escalationPolicy := pagerduty.EscalationPolicy{
    81  		Name:            d.Get("name").(string),
    82  		EscalationRules: expandEscalationRules(escalationRules),
    83  	}
    84  
    85  	if attr, ok := d.GetOk("description"); ok {
    86  		escalationPolicy.Description = attr.(string)
    87  	}
    88  
    89  	if attr, ok := d.GetOk("num_loops"); ok {
    90  		escalationPolicy.NumLoops = uint(attr.(int))
    91  	}
    92  
    93  	if attr, ok := d.GetOk("teams"); ok {
    94  		escalationPolicy.Teams = expandTeams(attr.([]interface{}))
    95  	}
    96  
    97  	return &escalationPolicy
    98  }
    99  
   100  func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interface{}) error {
   101  	client := meta.(*pagerduty.Client)
   102  
   103  	escalationPolicy := buildEscalationPolicyStruct(d)
   104  
   105  	log.Printf("[INFO] Creating PagerDuty escalation policy: %s", escalationPolicy.Name)
   106  
   107  	escalationPolicy, err := client.CreateEscalationPolicy(*escalationPolicy)
   108  
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	d.SetId(escalationPolicy.ID)
   114  
   115  	return resourcePagerDutyEscalationPolicyRead(d, meta)
   116  }
   117  
   118  func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
   119  	client := meta.(*pagerduty.Client)
   120  
   121  	log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id())
   122  
   123  	o := &pagerduty.GetEscalationPolicyOptions{}
   124  
   125  	escalationPolicy, err := client.GetEscalationPolicy(d.Id(), o)
   126  
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	d.Set("name", escalationPolicy.Name)
   132  	d.Set("teams", escalationPolicy.Teams)
   133  	d.Set("description", escalationPolicy.Description)
   134  	d.Set("num_loops", escalationPolicy.NumLoops)
   135  
   136  	if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
   137  		return err
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
   144  	client := meta.(*pagerduty.Client)
   145  
   146  	escalationPolicy := buildEscalationPolicyStruct(d)
   147  
   148  	log.Printf("[INFO] Updating PagerDuty escalation policy: %s", d.Id())
   149  
   150  	if _, err := client.UpdateEscalationPolicy(d.Id(), escalationPolicy); err != nil {
   151  		return err
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  func resourcePagerDutyEscalationPolicyDelete(d *schema.ResourceData, meta interface{}) error {
   158  	client := meta.(*pagerduty.Client)
   159  
   160  	log.Printf("[INFO] Deleting PagerDuty escalation policy: %s", d.Id())
   161  
   162  	if err := client.DeleteEscalationPolicy(d.Id()); err != nil {
   163  		return err
   164  	}
   165  
   166  	d.SetId("")
   167  
   168  	return nil
   169  }