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

Fix create race condition #380

Merged
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
7 changes: 5 additions & 2 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,17 @@ func isErrCode(err error, code int) bool {
return false
}

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

func handleNotFoundError(err error, d *schema.ResourceData) error {
if isErrCode(err, 404) {
log.Printf("[WARN] Removing %s because it's gone", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("Error reading: %s: %s", d.Id(), err)
return genError(err, d)
}

func providerConfigure(data *schema.ResourceData, terraformVersion string) (interface{}, error) {
Expand Down
47 changes: 25 additions & 22 deletions pagerduty/resource_pagerduty_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,13 @@ func buildAddonStruct(d *schema.ResourceData) *pagerduty.Addon {
return addon
}

func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

addon := buildAddonStruct(d)

log.Printf("[INFO] Creating PagerDuty add-on %s", addon.Name)

addon, _, err := client.Addons.Install(addon)
if err != nil {
return err
}

d.SetId(addon.ID)

return resourcePagerDutyAddonRead(d, meta)
}

func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyAddon(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, _ := meta.(*Config).Client()

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

return resource.Retry(2*time.Minute, func() *resource.RetryError {
addon, _, err := client.Addons.Get(d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
log.Printf("[WARN] Service read error")
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand All @@ -82,6 +63,28 @@ func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error
})
}

func resourcePagerDutyAddonCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

addon := buildAddonStruct(d)

log.Printf("[INFO] Creating PagerDuty add-on %s", addon.Name)

addon, _, err := client.Addons.Install(addon)
if err != nil {
return err
}

d.SetId(addon.ID)
// Retrying on creates incase of eventual consistency on creation
return fetchPagerDutyAddon(d, meta, genError)
}

func resourcePagerDutyAddonRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Reading PagerDuty add-on %s", d.Id())
return fetchPagerDutyAddon(d, meta, handleNotFoundError)
}

func resourcePagerDutyAddonUpdate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

Expand Down
46 changes: 24 additions & 22 deletions pagerduty/resource_pagerduty_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,12 @@ func buildExtensionStruct(d *schema.ResourceData) *pagerduty.Extension {
return Extension
}

func resourcePagerDutyExtensionCreate(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyExtension(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, _ := meta.(*Config).Client()

extension := buildExtensionStruct(d)

log.Printf("[INFO] Creating PagerDuty extension %s", extension.Name)

extension, _, err := client.Extensions.Create(extension)
if err != nil {
return err
}

d.SetId(extension.ID)

return resourcePagerDutyExtensionRead(d, meta)
}

func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

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

return resource.Retry(2*time.Minute, func() *resource.RetryError {
extension, _, err := client.Extensions.Get(d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand All @@ -137,6 +117,28 @@ func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) er
})
}

func resourcePagerDutyExtensionCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

extension := buildExtensionStruct(d)

log.Printf("[INFO] Creating PagerDuty extension %s", extension.Name)

extension, _, err := client.Extensions.Create(extension)
if err != nil {
return err
}

d.SetId(extension.ID)

return fetchPagerDutyExtension(d, meta, genError)
}

func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Reading PagerDuty extension %s", d.Id())
return fetchPagerDutyExtension(d, meta, handleNotFoundError)
}

func resourcePagerDutyExtensionUpdate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

Expand Down
45 changes: 23 additions & 22 deletions pagerduty/resource_pagerduty_extension_servicenow.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,32 +121,12 @@ func buildExtensionServiceNowStruct(d *schema.ResourceData) *pagerduty.Extension
return Extension
}

func resourcePagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

extension := buildExtensionServiceNowStruct(d)

log.Printf("[INFO] Creating PagerDuty extension %s", extension.Name)

extension, _, err := client.Extensions.Create(extension)
if err != nil {
return err
}

d.SetId(extension.ID)

return resourcePagerDutyExtensionServiceNowRead(d, meta)
}

func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta interface{}) error {
func fetchPagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, _ := meta.(*Config).Client()

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

return resource.Retry(2*time.Minute, func() *resource.RetryError {
extension, _, err := client.Extensions.Get(d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
Expand Down Expand Up @@ -178,6 +158,27 @@ func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta inter
})
}

func resourcePagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

extension := buildExtensionServiceNowStruct(d)

log.Printf("[INFO] Creating PagerDuty extension %s", extension.Name)

extension, _, err := client.Extensions.Create(extension)
if err != nil {
return err
}

d.SetId(extension.ID)
return fetchPagerDutyExtensionServiceNowCreate(d, meta, genError)
}

func resourcePagerDutyExtensionServiceNowRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Reading PagerDuty extension %s", d.Id())
return fetchPagerDutyExtensionServiceNowCreate(d, meta, handleNotFoundError)
}

func resourcePagerDutyExtensionServiceNowUpdate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

Expand Down
55 changes: 29 additions & 26 deletions pagerduty/resource_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ func flattenTeam(v *pagerduty.RulesetObject) []interface{} {

return []interface{}{team}
}

func fetchPagerDutyRuleset(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, _ := meta.(*Config).Client()
return resource.Retry(2*time.Minute, func() *resource.RetryError {
ruleset, _, err := client.Rulesets.Get(d.Id())
if err != nil {
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}
d.Set("name", ruleset.Name)
d.Set("type", ruleset.Type)

// if ruleset is found set to ResourceData
if ruleset.Team != nil {
d.Set("team", flattenTeam(ruleset.Team))
}
d.Set("routing_keys", ruleset.RoutingKeys)

return nil
})
}

func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

Expand All @@ -122,37 +149,13 @@ func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) er
if retryErr != nil {
return retryErr
}

return resourcePagerDutyRulesetRead(d, meta)
return fetchPagerDutyRuleset(d, meta, genError)
}

func resourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

log.Printf("[INFO] Reading PagerDuty ruleset: %s", d.Id())
return fetchPagerDutyRuleset(d, meta, handleNotFoundError)

return resource.Retry(2*time.Minute, func() *resource.RetryError {
ruleset, _, err := client.Rulesets.Get(d.Id())
if err != nil {
errResp := handleNotFoundError(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}
d.Set("name", ruleset.Name)
d.Set("type", ruleset.Type)

// if ruleset is found set to ResourceData
if ruleset.Team != nil {
d.Set("team", flattenTeam(ruleset.Team))
}
d.Set("routing_keys", ruleset.RoutingKeys)

return nil
})
}
func resourcePagerDutyRulesetUpdate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()
Expand Down
51 changes: 27 additions & 24 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,29 @@ func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) {
return &service, nil
}

func fetchService(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error {
client, _ := meta.(*Config).Client()
return resource.Retry(2*time.Minute, func() *resource.RetryError {
service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{})
if err != nil {
log.Printf("[WARN] Service read error")
errResp := errCallback(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}

if err := flattenService(d, service); err != nil {
return resource.NonRetryableError(err)
}
return nil

})
}

func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

Expand All @@ -360,39 +383,19 @@ func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) er

log.Printf("[INFO] Creating PagerDuty service %s", service.Name)

createdService, _, err := client.Services.Create(service)
service, _, err = client.Services.Create(service)
if err != nil {
return err
}

d.SetId(createdService.ID)
d.SetId(service.ID)

return flattenService(d, createdService)
return fetchService(d, meta, genError)
}

func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error {
client, _ := meta.(*Config).Client()

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

return resource.Retry(2*time.Minute, func() *resource.RetryError {
service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{})
if err != nil {
log.Printf("[WARN] Service read error")
errResp := handleNotFoundError(err, d)
if errResp != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(errResp)
}

return nil
}

if err := flattenService(d, service); err != nil {
return resource.NonRetryableError(err)
}
return nil
})
return fetchService(d, meta, handleNotFoundError)
}

func resourcePagerDutyServiceUpdate(d *schema.ResourceData, meta interface{}) error {
Expand Down
Loading