forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit is an extension of the following issue:
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.
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
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 | ||
}) | ||
} |