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: Reset ASG ID if not found #24

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Changes from 1 commit
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
26 changes: 15 additions & 11 deletions src/terraform-provider-paperspace/resource_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import (
"github.com/paperspace/paperspace-go"
)

func resetIfNotFound(d *schema.ResourceData, err error) error {
paperspaceError, ok := err.(paperspace.PaperspaceError)
if ok {
if paperspaceError.Status == 404 {
d.SetId("")
return nil
}
}

return err
}

func resourceAutoscalingGroupCreate(d *schema.ResourceData, m interface{}) error {
var autoscalingGroup paperspace.AutoscalingGroup

Expand Down Expand Up @@ -52,15 +64,7 @@ func resourceAutoscalingGroupRead(d *schema.ResourceData, m interface{}) error {

autoscalingGroup, err := paperspaceClient.GetAutoscalingGroup(d.Id(), paperspace.AutoscalingGroupGetParams{})
if err != nil {
paperspaceError, ok := err.(paperspace.PaperspaceError)
if ok {
if paperspaceError.Status == 404 {
d.SetId("")
return nil
}
}

return err
return resetIfNotFound(d, err)
}

d.Set("name", autoscalingGroup.Name)
Expand Down Expand Up @@ -90,7 +94,7 @@ func resourceAutoscalingGroupUpdate(d *schema.ResourceData, m interface{}) error

err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
if err := paperspaceClient.UpdateAutoscalingGroup(d.Id(), autoscalingGroupUpdateParams); err != nil {
return resource.RetryableError(err)
return resource.RetryableError(resetIfNotFound(d, err))
}

return resource.NonRetryableError(nil)
Expand All @@ -114,7 +118,7 @@ func resourceAutoscalingGroupDelete(d *schema.ResourceData, m interface{}) error

return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
if err := paperspaceClient.DeleteAutoscalingGroup(d.Id(), paperspace.AutoscalingGroupDeleteParams{}); err != nil {
return resource.RetryableError(err)
return resource.RetryableError(resetIfNotFound(d, err))
}

return resource.NonRetryableError(nil)
Expand Down