github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/pagerduty/data_source_pagerduty_escalation_policy.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	pagerduty "github.com/PagerDuty/go-pagerduty"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func dataSourcePagerDutyEscalationPolicy() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourcePagerDutyEscalationPolicyRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"name": {
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  			},
    20  		},
    21  	}
    22  }
    23  
    24  func dataSourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
    25  	client := meta.(*pagerduty.Client)
    26  
    27  	log.Printf("[INFO] Reading PagerDuty escalation policy")
    28  
    29  	searchName := d.Get("name").(string)
    30  
    31  	o := &pagerduty.ListEscalationPoliciesOptions{
    32  		Query: searchName,
    33  	}
    34  
    35  	resp, err := client.ListEscalationPolicies(*o)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	var found *pagerduty.EscalationPolicy
    41  
    42  	for _, policy := range resp.EscalationPolicies {
    43  		if policy.Name == searchName {
    44  			found = &policy
    45  			break
    46  		}
    47  	}
    48  
    49  	if found == nil {
    50  		return fmt.Errorf("Unable to locate any escalation policy with the name: %s", searchName)
    51  	}
    52  
    53  	d.SetId(found.ID)
    54  	d.Set("name", found.Name)
    55  
    56  	return nil
    57  }