Skip to content

Commit

Permalink
Try to perform an exact match before partial matching (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
heimweh authored Feb 14, 2018
1 parent d9e8a35 commit 3263400
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
15 changes: 13 additions & 2 deletions pagerduty/data_source_pagerduty_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,26 @@ func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) err

var found *pagerduty.Vendor

r := regexp.MustCompile("(?i)" + searchName)
er := regexp.MustCompile(fmt.Sprintf("^(?i)%s$", searchName))

for _, vendor := range resp.Vendors {
if r.MatchString(vendor.Name) {
if er.MatchString(vendor.Name) {
found = vendor
break
}
}

// We didn't find an exact match, so let's fallback to partial matching.
if found == nil {
pr := regexp.MustCompile("(?i)" + searchName)
for _, vendor := range resp.Vendors {
if pr.MatchString(vendor.Name) {
found = vendor
break
}
}
}

if found == nil {
return fmt.Errorf("Unable to locate any vendor with the name: %s", searchName)
}
Expand Down
52 changes: 25 additions & 27 deletions pagerduty/data_source_pagerduty_vendor_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourcePagerDutyVendor_Basic(t *testing.T) {
dataSourceName := "data.pagerduty_vendor.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -17,41 +16,40 @@ func TestAccDataSourcePagerDutyVendor_Basic(t *testing.T) {
{
Config: testAccDataSourcePagerDutyVendorConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyVendor("data.pagerduty_vendor.foo"),
resource.TestCheckResourceAttr(dataSourceName, "id", "PZQ6AUS"),
resource.TestCheckResourceAttr(dataSourceName, "name", "Amazon Cloudwatch"),
),
},
},
})
}

func testAccDataSourcePagerDutyVendor(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("Expected to get a vendor ID from PagerDuty")
}

if a["id"] != "PZQ6AUS" {
return fmt.Errorf("Expected the Datadog Vendor ID to be: PZQ6AUS, but got: %s", a["id"])
}

if a["name"] != "Amazon Cloudwatch" {
return fmt.Errorf("Expected the Datadog Vendor Name to be: Datadog, but got: %s", a["name"])
}

if a["type"] != "api" {
return fmt.Errorf("Expected the Datadog Vendor Type to be: api, but got: %s", a["type"])
}

return nil
}
func TestAccDataSourcePagerDutyVendor_ExactMatch(t *testing.T) {
dataSourceName := "data.pagerduty_vendor.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyScheduleDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyExactMatchConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "id", "PKG4M95"),
resource.TestCheckResourceAttr(dataSourceName, "name", "Sentry"),
),
},
},
})
}

const testAccDataSourcePagerDutyVendorConfig = `
data "pagerduty_vendor" "foo" {
name = "cloudwatch"
}
`

const testAccDataSourcePagerDutyExactMatchConfig = `
data "pagerduty_vendor" "foo" {
name = "sentry"
}
`

0 comments on commit 3263400

Please sign in to comment.