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

Check for Resource Already Exist Error before retry #243

Merged
merged 3 commits into from
Jul 9, 2024
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only error we want to verify? There are multiple errors are present, What will happen to those?

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
42 changes: 26 additions & 16 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"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -130,29 +131,38 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

return diag.Errorf("[ERR] %s", err)
}
d.SetId(network.ID)

// Create a default firewall for the network
log.Printf("[INFO] Creating default firewall for the network %s", d.Get("label").(string))
err = createDefaultFirewall(apiClient, network.ID, network.Label)
// 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 err
return diag.Errorf("[ERR] failed to create a new network after multiple attempts: %s", err)
}
}

return nil
}, 10*time.Second, 2*time.Minute)

d.SetId(network.ID)
// Create a default firewall for the network
log.Printf("[INFO] Creating default firewall for the network %s", d.Get("label").(string))
err = createDefaultFirewall(apiClient, network.ID, network.Label)
if err != nil {
return diag.Errorf("[ERR] failed to create a new network after multiple attempts: %s", err)
return diag.Errorf("[ERR] failed to create a new firewall for the network %s: %s", d.Get("label").(string), err)
}

return resourceNetworkRead(ctx, d, m)
}

Expand Down
Loading