From 05bf45bce2dc0634d95419784acae06f787c2ac5 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 15 Mar 2017 19:33:30 +0100 Subject: [PATCH] Remove name_regex field & old lookup (#12396) --- .../pagerduty/data_source_pagerduty_vendor.go | 73 ++----------------- builtin/providers/pagerduty/util.go | 42 ----------- 2 files changed, 5 insertions(+), 110 deletions(-) diff --git a/builtin/providers/pagerduty/data_source_pagerduty_vendor.go b/builtin/providers/pagerduty/data_source_pagerduty_vendor.go index 2c3360c6a..b3e3659ac 100644 --- a/builtin/providers/pagerduty/data_source_pagerduty_vendor.go +++ b/builtin/providers/pagerduty/data_source_pagerduty_vendor.go @@ -15,16 +15,13 @@ func dataSourcePagerDutyVendor() *schema.Resource { Schema: map[string]*schema.Schema{ "name_regex": { - Type: schema.TypeString, - Optional: true, - Deprecated: "Use field name instead", - ConflictsWith: []string{"name"}, + Type: schema.TypeString, + Optional: true, + Removed: "Use `name` instead. This attribute will be removed in a future version", }, "name": { - Type: schema.TypeString, - Computed: true, - Optional: true, - ConflictsWith: []string{"name_regex"}, + Type: schema.TypeString, + Required: true, }, "type": { Type: schema.TypeString, @@ -37,19 +34,6 @@ func dataSourcePagerDutyVendor() *schema.Resource { func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) error { 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") searchName := d.Get("name").(string) @@ -84,50 +68,3 @@ func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) err 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 -} diff --git a/builtin/providers/pagerduty/util.go b/builtin/providers/pagerduty/util.go index 20b1e70db..68181f9a7 100644 --- a/builtin/providers/pagerduty/util.go +++ b/builtin/providers/pagerduty/util.go @@ -3,7 +3,6 @@ package pagerduty import ( "fmt" - pagerduty "github.com/PagerDuty/go-pagerduty" "github.com/hashicorp/terraform/helper/schema" ) @@ -25,44 +24,3 @@ func validateValueFunc(values []string) schema.SchemaValidateFunc { 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 -}