github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/pagerduty/data_source_pagerduty_schedule.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 dataSourcePagerDutySchedule() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourcePagerDutyScheduleRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"name": {
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  			},
    20  		},
    21  	}
    22  }
    23  
    24  func dataSourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) error {
    25  	client := meta.(*pagerduty.Client)
    26  
    27  	log.Printf("[INFO] Reading PagerDuty schedule")
    28  
    29  	searchName := d.Get("name").(string)
    30  
    31  	o := &pagerduty.ListSchedulesOptions{
    32  		Query: searchName,
    33  	}
    34  
    35  	resp, err := client.ListSchedules(*o)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	var found *pagerduty.Schedule
    41  
    42  	for _, schedule := range resp.Schedules {
    43  		if schedule.Name == searchName {
    44  			found = &schedule
    45  			break
    46  		}
    47  	}
    48  
    49  	if found == nil {
    50  		return fmt.Errorf("Unable to locate any schedule with the name: %s", searchName)
    51  	}
    52  
    53  	d.SetId(found.ID)
    54  	d.Set("name", found.Name)
    55  
    56  	return nil
    57  }