Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/pagerduty_vendor: Match whole string and fallback to partial matching #55

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
`