-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from stmcallister/master
Adding Ruleset Data Source
- Loading branch information
Showing
14 changed files
with
284 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/heimweh/go-pagerduty/pagerduty" | ||
) | ||
|
||
func dataSourcePagerDutyRuleset() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourcePagerDutyRulesetRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*pagerduty.Client) | ||
|
||
log.Printf("[INFO] Reading PagerDuty ruleset") | ||
|
||
searchName := d.Get("name").(string) | ||
|
||
resp, _, err := client.Rulesets.List() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var found *pagerduty.Ruleset | ||
|
||
for _, ruleset := range resp.Rulesets { | ||
if ruleset.Name == searchName { | ||
found = ruleset | ||
break | ||
} | ||
} | ||
|
||
if found == nil { | ||
return fmt.Errorf("Unable to locate any ruleset with the name: %s", searchName) | ||
} | ||
|
||
d.SetId(found.ID) | ||
d.Set("name", found.Name) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccDataSourcePagerDutyRuleset_Basic(t *testing.T) { | ||
ruleset := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourcePagerDutyRulesetConfig(ruleset), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourcePagerDutyRuleset("pagerduty_ruleset.test", "data.pagerduty_ruleset.by_name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourcePagerDutyRuleset(src, n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
||
srcR := s.RootModule().Resources[src] | ||
srcA := srcR.Primary.Attributes | ||
|
||
r := s.RootModule().Resources[n] | ||
a := r.Primary.Attributes | ||
|
||
if a["id"] == "" { | ||
return fmt.Errorf("Expected to get a ruleset ID from PagerDuty") | ||
} | ||
|
||
testAtts := []string{"id", "name"} | ||
|
||
for _, att := range testAtts { | ||
if a[att] != srcA[att] { | ||
return fmt.Errorf("Expected the ruleset %s to be: %s, but got: %s", att, srcA[att], a[att]) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccDataSourcePagerDutyRulesetConfig(ruleset string) string { | ||
return fmt.Sprintf(` | ||
resource "pagerduty_ruleset" "test" { | ||
name = "%s" | ||
} | ||
data "pagerduty_ruleset" "by_name" { | ||
name = pagerduty_ruleset.test.name | ||
} | ||
`, ruleset) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
79 changes: 65 additions & 14 deletions
79
vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
30 changes: 27 additions & 3 deletions
30
vendor/github.com/heimweh/go-pagerduty/pagerduty/ruleset.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.