github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 Removed: "Use `name` instead. This attribute will be removed in a future version", 21 }, 22 "name": { 23 Type: schema.TypeString, 24 Required: true, 25 }, 26 "type": { 27 Type: schema.TypeString, 28 Computed: true, 29 }, 30 }, 31 } 32 } 33 34 func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error { 35 client := meta.(*pagerduty.Client) 36 37 log.Printf("[INFO] Reading PagerDuty vendor") 38 39 searchName := d.Get("name").(string) 40 41 o := &pagerduty.ListVendorOptions{ 42 Query: searchName, 43 } 44 45 resp, err := client.ListVendors(*o) 46 if err != nil { 47 return err 48 } 49 50 var found *pagerduty.Vendor 51 52 r := regexp.MustCompile("(?i)" + searchName) 53 54 for _, vendor := range resp.Vendors { 55 if r.MatchString(vendor.Name) { 56 found = &vendor 57 break 58 } 59 } 60 61 if found == nil { 62 return fmt.Errorf("Unable to locate any vendor with the name: %s", searchName) 63 } 64 65 d.SetId(found.ID) 66 d.Set("name", found.Name) 67 d.Set("type", found.GenericServiceType) 68 69 return nil 70 }