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

Implement retry logic in all read requests #208

Merged
merged 6 commits into from
Jun 10, 2020
Merged
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
39 changes: 23 additions & 16 deletions pagerduty/data_source_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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"
)
Expand Down Expand Up @@ -32,26 +34,31 @@ func dataSourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interf
Query: searchName,
}

resp, _, err := client.EscalationPolicies.List(o)
if err != nil {
return err
}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.EscalationPolicies.List(o)
if err != nil {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.EscalationPolicy
var found *pagerduty.EscalationPolicy

for _, policy := range resp.EscalationPolicies {
if policy.Name == searchName {
found = policy
break
for _, policy := range resp.EscalationPolicies {
if policy.Name == searchName {
found = policy
break
}
}
}

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

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

return nil
return nil
})
}
41 changes: 24 additions & 17 deletions pagerduty/data_source_pagerduty_extension_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)
Expand Down Expand Up @@ -33,27 +35,32 @@ func dataSourcePagerDutyExtensionSchemaRead(d *schema.ResourceData, meta interfa

searchName := d.Get("name").(string)

resp, _, err := client.ExtensionSchemas.List(&pagerduty.ListExtensionSchemasOptions{Query: searchName})
if err != nil {
return err
}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.ExtensionSchemas.List(&pagerduty.ListExtensionSchemasOptions{Query: searchName})
if err != nil {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.ExtensionSchema
var found *pagerduty.ExtensionSchema

for _, schema := range resp.ExtensionSchemas {
if strings.EqualFold(schema.Label, searchName) {
found = schema
break
for _, schema := range resp.ExtensionSchemas {
if strings.EqualFold(schema.Label, searchName) {
found = schema
break
}
}
}

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

d.SetId(found.ID)
d.Set("name", found.Label)
d.Set("type", found.Type)
d.SetId(found.ID)
d.Set("name", found.Label)
d.Set("type", found.Type)

return nil
return nil
})
}
39 changes: 23 additions & 16 deletions pagerduty/data_source_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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"
)
Expand Down Expand Up @@ -32,26 +34,31 @@ func dataSourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) e
Query: searchName,
}

resp, _, err := client.Schedules.List(o)
if err != nil {
return err
}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Schedules.List(o)
if err != nil {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.Schedule
var found *pagerduty.Schedule

for _, schedule := range resp.Schedules {
if schedule.Name == searchName {
found = schedule
break
for _, schedule := range resp.Schedules {
if schedule.Name == searchName {
found = schedule
break
}
}
}

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

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

return nil
return nil
})
}
42 changes: 25 additions & 17 deletions pagerduty/data_source_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ 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"
"log"
)

func dataSourcePagerDutyService() *schema.Resource {
Expand All @@ -31,26 +34,31 @@ func dataSourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) er
Query: searchName,
}

resp, _, err := client.Services.List(o)
if err != nil {
return err
}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Services.List(o)
if err != nil {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.Service
var found *pagerduty.Service

for _, service := range resp.Services {
if service.Name == searchName {
found = service
break
for _, service := range resp.Services {
if service.Name == searchName {
found = service
break
}
}
}

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

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

return nil
return nil
})
}
41 changes: 24 additions & 17 deletions pagerduty/data_source_pagerduty_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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"
)
Expand Down Expand Up @@ -37,27 +39,32 @@ func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error
Query: searchTeam,
}

resp, _, err := client.Teams.List(o)
if err != nil {
return err
}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Teams.List(o)
if err != nil {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.Team
var found *pagerduty.Team

for _, team := range resp.Teams {
if team.Name == searchTeam {
found = team
break
for _, team := range resp.Teams {
if team.Name == searchTeam {
found = team
break
}
}
}

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

d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("description", found.Description)
d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("description", found.Description)

return nil
return nil
})
}
41 changes: 24 additions & 17 deletions pagerduty/data_source_pagerduty_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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"
)
Expand Down Expand Up @@ -36,27 +38,32 @@ func dataSourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error
Query: searchEmail,
}

resp, _, err := client.Users.List(o)
if err != nil {
return err
}
return resource.Retry(2*time.Minute, func() *resource.RetryError {
resp, _, err := client.Users.List(o)
if err != nil {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}

var found *pagerduty.User
var found *pagerduty.User

for _, user := range resp.Users {
if user.Email == searchEmail {
found = user
break
for _, user := range resp.Users {
if user.Email == searchEmail {
found = user
break
}
}
}

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

d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("email", found.Email)
d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("email", found.Email)

return nil
return nil
})
}
Loading