Skip to content

Commit

Permalink
Check for Resource Already Exist Error before retry
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Jun 24, 2024
1 parent 8a47734 commit 846f87b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
30 changes: 21 additions & 9 deletions civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package instances

import (
"context"
"errors"
"log"
"strings"
"time"
Expand Down Expand Up @@ -278,19 +279,30 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
config.Tags = tags

log.Printf("[INFO] creating the instance %s", d.Get("hostname").(string))
err := utils.RetryUntilSuccessOrTimeout(func() error {
instance, err := apiClient.CreateInstance(config)
if err != nil {
return err
}
d.SetId(instance.ID)
return nil
}, 10*time.Second, 2*time.Minute)

instance, err := apiClient.CreateInstance(config)
if err != nil {
return diag.Errorf("[ERR] failed to create instance after multiple attempts: %s", err)
if errors.Is(err, civogo.DatabaseInstanceDuplicateNameError) {
return diag.Errorf("[ERR] instance with the name %s already exists", config.Hostname)
}

err := utils.RetryUntilSuccessOrTimeout(func() error {
log.Printf("[INFO] Attempting to create the instance %s", config.Hostname)
instance, err := apiClient.CreateInstance(config)
if err != nil {
return err
}
d.SetId(instance.ID)
return nil
}, 10*time.Second, 2*time.Minute)

if err != nil {
return diag.Errorf("[ERR] failed to create instance after multiple attempts: %s", err)
}
}

d.SetId(instance.ID)

createStateConf := &resource.StateChangeConf{
Pending: []string{"BUILDING"},
Target: []string{"ACTIVE"},
Expand Down
34 changes: 23 additions & 11 deletions civo/network/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network

import (
"context"
"errors"
"log"
"time"

Expand Down Expand Up @@ -129,21 +130,32 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interf
configs.VLanConfig = &vlanConfig
}

// Retry the network creation using the utility function
err := utils.RetryUntilSuccessOrTimeout(func() error {
log.Printf("[INFO] Attempting to create the network %s", d.Get("label").(string))
network, err := apiClient.CreateNetwork(configs)
if err != nil {
return err
log.Printf("[INFO] Attempting to create the network %s", d.Get("label").(string))
network, err := apiClient.CreateNetwork(configs)
if err != nil {

// Check for the resource already exists error
if errors.Is(err, civogo.DatabaseNetworkExistsError) {
return diag.Errorf("[ERR] %s", err)
}
d.SetId(network.ID)
return nil
}, 10*time.Second, 2*time.Minute)

if err != nil {
return diag.Errorf("[ERR] failed to create a new network after multiple attempts: %s", err)
// Retry the network creation using the utility function for other errors
err := utils.RetryUntilSuccessOrTimeout(func() error {
log.Printf("[INFO] Attempting to create the network %s", d.Get("label").(string))
network, err := apiClient.CreateNetwork(configs)
if err != nil {
return err
}
d.SetId(network.ID)
return nil
}, 10*time.Second, 2*time.Minute)

if err != nil {
return diag.Errorf("[ERR] failed to create a new network after multiple attempts: %s", err)
}
}

d.SetId(network.ID)
return resourceNetworkRead(ctx, d, m)
}

Expand Down

0 comments on commit 846f87b

Please sign in to comment.