Skip to content

Commit

Permalink
Merge pull request #237 from stmcallister/master
Browse files Browse the repository at this point in the history
Adding Ruleset Data Source
  • Loading branch information
Scott McAllister authored Jun 1, 2020
2 parents b2ad327 + 40655a7 commit 8bdde9b
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-pagerduty
require (
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/heimweh/go-pagerduty v0.0.0-20200429000711-fdd3a48907e7
github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/heimweh/go-pagerduty v0.0.0-20200415004831-bc2b3224a572 h1:UPbUDXwtlg
github.com/heimweh/go-pagerduty v0.0.0-20200415004831-bc2b3224a572/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200429000711-fdd3a48907e7 h1:8groiuI/kigz8jYT46EKNqDYVcEfSDnhv5p+VgZfPGU=
github.com/heimweh/go-pagerduty v0.0.0-20200429000711-fdd3a48907e7/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24 h1:ga84s0DaOnrYN7IL7jMug6ins5QpiMuNpXFxCKqFAvg=
github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
53 changes: 53 additions & 0 deletions pagerduty/data_source_pagerduty_ruleset.go
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
}
64 changes: 64 additions & 0 deletions pagerduty/data_source_pagerduty_ruleset_test.go
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)
}
3 changes: 2 additions & 1 deletion pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Provider() terraform.ResourceProvider {
"pagerduty_service": dataSourcePagerDutyService(),
"pagerduty_business_service": dataSourcePagerDutyBusinessService(),
"pagerduty_priority": dataSourcePagerDutyPriority(),
"pagerduty_ruleset": dataSourcePagerDutyRuleset(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -74,7 +75,7 @@ func Provider() terraform.ResourceProvider {
}

func isErrCode(err error, code int) bool {
if e, ok := err.(*pagerduty.Error); ok && e.ErrorResponse.StatusCode == code {
if e, ok := err.(*pagerduty.Error); ok && e.ErrorResponse.Response.StatusCode == code {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/heimweh/go-pagerduty/pagerduty/error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 65 additions & 14 deletions 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.

30 changes: 27 additions & 3 deletions 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.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ github.com/hashicorp/terraform-svchost/auth
github.com/hashicorp/terraform-svchost/disco
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/heimweh/go-pagerduty v0.0.0-20200429000711-fdd3a48907e7
# github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24
github.com/heimweh/go-pagerduty/pagerduty
# github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af
github.com/jmespath/go-jmespath
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/priority.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-

# pagerduty\_priority

Use this data source to get information about a specific [priority][1] that you can use for other PagerDuty resources.
Use this data source to get information about a specific [priority][1] that you can use for other PagerDuty resources. A priority is a label representing the importance and impact of an incident. This feature is only available on Standard and Enterprise plans.

## Example Usage

Expand Down
Loading

0 comments on commit 8bdde9b

Please sign in to comment.