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

Handle eventual consistency for pagerduty_escalation_policy and pagerduty_service resources with retries. #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func handleNotFoundError(err error, d *schema.ResourceData) error {
return fmt.Errorf("Error reading: %s: %s", d.Id(), err)
}

func handleError(err error, d *schema.ResourceData) error {
return fmt.Errorf("Error reading: %s: %s", d.Id(), err)
}

func providerConfigure(data *schema.ResourceData, terraformVersion string) (interface{}, error) {
config := Config{
SkipCredsValidation: data.Get("skip_credentials_validation").(bool),
Expand Down
13 changes: 10 additions & 3 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,21 @@ func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interf
func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id())

o := &pagerduty.GetEscalationPolicyOptions{}

attempts := 0
return resource.Retry(2*time.Minute, func() *resource.RetryError {
attempts = attempts + 1

log.Printf("[INFO] Reading PagerDuty Escalation Policy %s. Attempt %s", d.Id(), attempts)
escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o)
if err != nil {
errResp := handleNotFoundError(err, d)
log.Printf("[WARN] Escalation Policy read error")
errResp := handleError(err, d)
if attempts > 6 {
errResp = handleNotFoundError(err, d)
}

if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand Down
12 changes: 9 additions & 3 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,19 @@ func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) er
func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty service %s", d.Id())

attempts := 0
return resource.Retry(2*time.Minute, func() *resource.RetryError {
attempts = attempts + 1

log.Printf("[INFO] Reading PagerDuty Service %s. Attempt %s", d.Id(), attempts)
service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{})
if err != nil {
log.Printf("[WARN] Service read error")
errResp := handleNotFoundError(err, d)
errResp := handleError(err, d)
if attempts > 6 {
errResp = handleNotFoundError(err, d)
}

if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand Down