From e9e7472a3f4796050b799dca309bc46cc614edda Mon Sep 17 00:00:00 2001 From: Uzair Ali <72073401+uzaxirr@users.noreply.github.com> Date: Mon, 24 Jun 2024 11:08:42 +0530 Subject: [PATCH] Check for Resource Already Exist Error before retry --- civo/instances/resource_instance.go | 30 ++++++++++++++------- civo/network/resource_network.go | 42 ++++++++++++++++++----------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/civo/instances/resource_instance.go b/civo/instances/resource_instance.go index 3aa5241..1d3add2 100644 --- a/civo/instances/resource_instance.go +++ b/civo/instances/resource_instance.go @@ -2,6 +2,7 @@ package instances import ( "context" + "errors" "log" "strings" "time" @@ -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"}, diff --git a/civo/network/resource_network.go b/civo/network/resource_network.go index 83be75a..a0be5c2 100644 --- a/civo/network/resource_network.go +++ b/civo/network/resource_network.go @@ -3,6 +3,7 @@ package network import ( "context" "fmt" + "errors" "log" "time" @@ -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) { + 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("failed firewall") } - return resourceNetworkRead(ctx, d, m) }