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

     1  package pagerduty
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	pagerduty "github.com/PagerDuty/go-pagerduty"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourcePagerDutyVendor() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourcePagerDutyVendorRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name_regex": {
    18  				Type:          schema.TypeString,
    19  				Optional:      true,
    20  				Deprecated:    "Use field name instead",
    21  				ConflictsWith: []string{"name"},
    22  			},
    23  			"name": {
    24  				Type:          schema.TypeString,
    25  				Computed:      true,
    26  				Optional:      true,
    27  				ConflictsWith: []string{"name_regex"},
    28  			},
    29  			"type": {
    30  				Type:     schema.TypeString,
    31  				Computed: true,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error {
    38  	client := meta.(*pagerduty.Client)
    39  
    40  	// Check if we're doing a normal or legacy lookup
    41  	_, ok := d.GetOk("name")
    42  	_, legacyOk := d.GetOk("name_regex")
    43  
    44  	if !ok && !legacyOk {
    45  		return fmt.Errorf("Either name or name_regex must be set")
    46  	}
    47  
    48  	// If name_regex is set, we're doing a legacy lookup
    49  	if legacyOk {
    50  		return dataSourcePagerDutyVendorLegacyRead(d, meta)
    51  	}
    52  
    53  	log.Printf("[INFO] Reading PagerDuty vendor")
    54  
    55  	searchName := d.Get("name").(string)
    56  
    57  	o := &pagerduty.ListVendorOptions{
    58  		Query: searchName,
    59  	}
    60  
    61  	resp, err := client.ListVendors(*o)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	var found *pagerduty.Vendor
    67  
    68  	r := regexp.MustCompile("(?i)" + searchName)
    69  
    70  	for _, vendor := range resp.Vendors {
    71  		if r.MatchString(vendor.Name) {
    72  			found = &vendor
    73  			break
    74  		}
    75  	}
    76  
    77  	if found == nil {
    78  		return fmt.Errorf("Unable to locate any vendor with the name: %s", searchName)
    79  	}
    80  
    81  	d.SetId(found.ID)
    82  	d.Set("name", found.Name)
    83  	d.Set("type", found.GenericServiceType)
    84  
    85  	return nil
    86  }
    87  
    88  func dataSourcePagerDutyVendorLegacyRead(d *schema.ResourceData, meta interface{}) error {
    89  	client := meta.(*pagerduty.Client)
    90  
    91  	log.Printf("[INFO] Reading PagerDuty vendor (legacy)")
    92  
    93  	resp, err := getVendors(client)
    94  
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	r := regexp.MustCompile("(?i)" + d.Get("name_regex").(string))
   100  
   101  	var vendors []pagerduty.Vendor
   102  	var vendorNames []string
   103  
   104  	for _, v := range resp {
   105  		if r.MatchString(v.Name) {
   106  			vendors = append(vendors, v)
   107  			vendorNames = append(vendorNames, v.Name)
   108  		}
   109  	}
   110  
   111  	if len(vendors) == 0 {
   112  		return fmt.Errorf("Unable to locate any vendor using the regex string: %s", r.String())
   113  	} else if len(vendors) > 1 {
   114  		return fmt.Errorf("Your query returned more than one result using the regex string: %#v. Found vendors: %#v", r.String(), vendorNames)
   115  	}
   116  
   117  	vendor := vendors[0]
   118  
   119  	genericServiceType := vendor.GenericServiceType
   120  
   121  	switch {
   122  	case genericServiceType == "email":
   123  		genericServiceType = "generic_email_inbound_integration"
   124  	case genericServiceType == "api":
   125  		genericServiceType = "generic_events_api_inbound_integration"
   126  	}
   127  
   128  	d.SetId(vendor.ID)
   129  	d.Set("name", vendor.Name)
   130  	d.Set("type", genericServiceType)
   131  
   132  	return nil
   133  }