Skip to content

Commit

Permalink
This commit is an extension of the following issue:
Browse files Browse the repository at this point in the history
PagerDuty#307

It adds the relevant schema to allow the ruleset data source to return
routing keys.
  • Loading branch information
Polski Walker committed Mar 4, 2021
1 parent d29b163 commit 1f7da41
Showing 1 changed file with 67 additions and 67 deletions.
134 changes: 67 additions & 67 deletions pagerduty/data_source_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
package pagerduty

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"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)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Rulesets.List()
if err != nil {
if isErrCode(err, 429) {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

var found *pagerduty.Ruleset

for _, ruleset := range resp.Rulesets {
if ruleset.Name == searchName {
found = ruleset
break
}
}

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any ruleset with the name: %s", searchName),
)
}

d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("routing_keys", found.RoutingKeys)

return nil
})
}
package pagerduty

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"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)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Rulesets.List()
if err != nil {
if isErrCode(err, 429) {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

var found *pagerduty.Ruleset

for _, ruleset := range resp.Rulesets {
if ruleset.Name == searchName {
found = ruleset
break
}
}

if found == nil {
return resource.NonRetryableError(
fmt.Errorf("Unable to locate any ruleset with the name: %s", searchName),
)
}

d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("routing_keys", found.RoutingKeys)

return nil
})
}

0 comments on commit 1f7da41

Please sign in to comment.