Remove name_regex field & old lookup (#12396)

This commit is contained in:
Alexander 2017-03-15 19:33:30 +01:00 committed by Paul Stack
parent 018ec10e36
commit 05bf45bce2
2 changed files with 5 additions and 110 deletions

View File

@ -17,14 +17,11 @@ func dataSourcePagerDutyVendor() *schema.Resource {
"name_regex": { "name_regex": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Deprecated: "Use field name instead", Removed: "Use `name` instead. This attribute will be removed in a future version",
ConflictsWith: []string{"name"},
}, },
"name": { "name": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Required: true,
Optional: true,
ConflictsWith: []string{"name_regex"},
}, },
"type": { "type": {
Type: schema.TypeString, Type: schema.TypeString,
@ -37,19 +34,6 @@ func dataSourcePagerDutyVendor() *schema.Resource {
func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error { func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client) client := meta.(*pagerduty.Client)
// Check if we're doing a normal or legacy lookup
_, ok := d.GetOk("name")
_, legacyOk := d.GetOk("name_regex")
if !ok && !legacyOk {
return fmt.Errorf("Either name or name_regex must be set")
}
// If name_regex is set, we're doing a legacy lookup
if legacyOk {
return dataSourcePagerDutyVendorLegacyRead(d, meta)
}
log.Printf("[INFO] Reading PagerDuty vendor") log.Printf("[INFO] Reading PagerDuty vendor")
searchName := d.Get("name").(string) searchName := d.Get("name").(string)
@ -84,50 +68,3 @@ func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) err
return nil return nil
} }
func dataSourcePagerDutyVendorLegacyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
log.Printf("[INFO] Reading PagerDuty vendor (legacy)")
resp, err := getVendors(client)
if err != nil {
return err
}
r := regexp.MustCompile("(?i)" + d.Get("name_regex").(string))
var vendors []pagerduty.Vendor
var vendorNames []string
for _, v := range resp {
if r.MatchString(v.Name) {
vendors = append(vendors, v)
vendorNames = append(vendorNames, v.Name)
}
}
if len(vendors) == 0 {
return fmt.Errorf("Unable to locate any vendor using the regex string: %s", r.String())
} else if len(vendors) > 1 {
return fmt.Errorf("Your query returned more than one result using the regex string: %#v. Found vendors: %#v", r.String(), vendorNames)
}
vendor := vendors[0]
genericServiceType := vendor.GenericServiceType
switch {
case genericServiceType == "email":
genericServiceType = "generic_email_inbound_integration"
case genericServiceType == "api":
genericServiceType = "generic_events_api_inbound_integration"
}
d.SetId(vendor.ID)
d.Set("name", vendor.Name)
d.Set("type", genericServiceType)
return nil
}

View File

@ -3,7 +3,6 @@ package pagerduty
import ( import (
"fmt" "fmt"
pagerduty "github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
@ -25,44 +24,3 @@ func validateValueFunc(values []string) schema.SchemaValidateFunc {
return return
} }
} }
// getVendors retrieves all PagerDuty vendors and returns a list of []pagerduty.Vendor
func getVendors(client *pagerduty.Client) ([]pagerduty.Vendor, error) {
var offset uint
var totalCount int
var vendors []pagerduty.Vendor
for {
o := &pagerduty.ListVendorOptions{
APIListObject: pagerduty.APIListObject{
Limit: 100,
Total: 1,
Offset: offset,
},
}
resp, err := client.ListVendors(*o)
if err != nil {
return nil, err
}
for _, v := range resp.Vendors {
totalCount++
vendors = append(vendors, v)
}
rOffset := uint(resp.Offset)
returnedCount := uint(len(resp.Vendors))
rTotal := uint(resp.Total)
if resp.More && uint(totalCount) != uint(rTotal) {
offset = returnedCount + rOffset
continue
}
break
}
return vendors, nil
}