Skip to content

Commit

Permalink
Added the option to recreate resource if not found
Browse files Browse the repository at this point in the history
in the cloud
  • Loading branch information
alejandrojnm committed Jun 7, 2020
1 parent a239402 commit 256b2e3
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion civo/resource_dns_domain_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func resourceDNSDomainNameRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retriving the domain %s", d.Get("name").(string))
resp, err := apiClient.GetDNSDomain(d.Get("name").(string))
if err != nil {
if resp != nil {
if resp == nil {
d.SetId("")
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion civo/resource_dns_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ func resourceDNSDomainRecordRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retriving the domain record %s", d.Get("name").(string))
resp, err := apiClient.GetDNSRecord(d.Get("domain_id").(string), d.Id())
if err != nil {
if resp != nil {
if resp == nil {
d.SetId("")
return nil
}

return fmt.Errorf("[WARN] domain record (%s) not found", d.Id())
}

Expand Down
3 changes: 2 additions & 1 deletion civo/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ func resourceFirewallRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retriving the firewall %s", d.Id())
resp, err := apiClient.FindFirewall(d.Id())
if err != nil {
if resp != nil {
if resp == nil {
d.SetId("")
return nil
}

return fmt.Errorf("[ERR] error retrieving firewall %s, %s", d.Id(), err)
}

Expand Down
2 changes: 1 addition & 1 deletion civo/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func resourceFirewallRuleRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retriving the firewall rule %s", d.Id())
resp, err := apiClient.FindFirewallRule(d.Get("firewall_id").(string), d.Id())
if err != nil {
if resp != nil {
if resp == nil {
d.SetId("")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {

log.Printf("[INFO] retriving the instance %s", d.Id())
resp, err := apiClient.GetInstance(d.Id())
if err != nil {
if err == nil {
d.SetId("")
// check if the instance no longer exists.
return fmt.Errorf("[ERR] instance (%s) not found", d.Id())
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func resourceLoadBalancerRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retrieving the load balancer %s", d.Id())
resp, err := apiClient.FindLoadBalancer(d.Id())
if err != nil {
if resp != nil {
if resp == nil {
d.SetId("")
return nil
}
Expand Down
8 changes: 7 additions & 1 deletion civo/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package civo

import (
"fmt"
"log"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)

// The resource network represent a network inside the cloud
Expand Down Expand Up @@ -71,6 +72,11 @@ func resourceNetworkRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retriving the network %s", d.Id())
resp, err := apiClient.ListNetworks()
if err != nil {
if resp == nil {
d.SetId("")
return nil
}

return fmt.Errorf("[ERR] failed to list all network %s", err)
}

Expand Down
5 changes: 5 additions & 0 deletions civo/resource_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func resourceSnapshotRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retrieving the snapshot %s", d.Get("name").(string))
resp, err := apiClient.FindSnapshot(d.Id())
if err != nil {
if resp == nil {
d.SetId("")
return nil
}

return fmt.Errorf("[ERR] failed retrieving the snapshot: %s", err)
}

Expand Down
5 changes: 3 additions & 2 deletions civo/resource_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package civo

import (
"fmt"
"log"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)

// Ssh resource, with this we can create and manage all Snapshot
Expand Down Expand Up @@ -63,7 +64,7 @@ func resourceSSHKeyRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retrieving the new ssh key %s", d.Get("name").(string))
sshKey, err := apiClient.FindSSHKey(d.Id())
if err != nil {
if sshKey != nil {
if sshKey == nil {
d.SetId("")
return nil
}
Expand Down
7 changes: 6 additions & 1 deletion civo/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package civo

import (
"fmt"
"log"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
)

// Template resource, with this we can create and manage all template
Expand Down Expand Up @@ -144,6 +145,10 @@ func resourceTemplateRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retrieving the template %s", d.Get("code").(string))
resp, err := apiClient.GetTemplateByCode(d.Get("code").(string))
if err != nil {
if resp == nil {
d.SetId("")
return nil
}
return fmt.Errorf("[ERR] failed to retrieving the template: %s", err)
}

Expand Down
9 changes: 7 additions & 2 deletions civo/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"time"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

// Volume resource, with this we can create and manage all volume
Expand Down Expand Up @@ -74,6 +75,10 @@ func resourceVolumeRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retrieving the volume %s", d.Id())
resp, err := apiClient.FindVolume(d.Id())
if err != nil {
if resp == nil {
d.SetId("")
return nil
}
return fmt.Errorf("[ERR] failed retrieving the volume: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion civo/resource_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func resourceVolumeAttachmentRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] retrieving the volume %s", volumeID)
resp, err := apiClient.FindVolume(volumeID)
if err != nil {
if resp != nil {
if resp == nil {
d.SetId("")
return nil
}
Expand Down

0 comments on commit 256b2e3

Please sign in to comment.