Skip to content

Commit

Permalink
Fix the error in firewall, fixed spelling mistake in the errors and a…
Browse files Browse the repository at this point in the history
…dded new Pending state to the cluster (#159)
  • Loading branch information
alejandrojnm committed Dec 15, 2022
1 parent 34fa1b2 commit 37a99d6
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 32 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 @@ -115,7 +115,7 @@ func resourceDNSDomainNameDelete(_ context.Context, d *schema.ResourceData, m in
log.Printf("[INFO] Deleting the domain %s", d.Get("name").(string))
_, err = apiClient.DeleteDNSDomain(resp)
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the domain %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete the domain %s", d.Id())
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_dns_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func resourceDNSDomainRecordDelete(_ context.Context, d *schema.ResourceData, m
log.Printf("[INFO] deleting the domain record %s", d.Get("name").(string))
_, err = apiClient.DeleteDNSRecord(resp)
if err != nil {
return diag.Errorf("[WARN] an error occurred while tring to delete the domain record %s", d.Id())
return diag.Errorf("[WARN] an error occurred while trying to delete the domain record %s", d.Id())
}

return nil
Expand Down
37 changes: 28 additions & 9 deletions civo/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"
"log"
"time"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -126,7 +128,7 @@ func resourceFirewallCreate(ctx context.Context, d *schema.ResourceData, m inter

firewallConfig, err := firewallRequestBuild(d, apiClient)
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to build the firewall request, %s", err)
return diag.Errorf("[ERR] an error occurred while trying to build the firewall request, %s", err)
}

firewall, err := apiClient.NewFirewall(firewallConfig)
Expand Down Expand Up @@ -195,7 +197,7 @@ func resourceFirewallUpdate(ctx context.Context, d *schema.ResourceData, m inter
log.Printf("[INFO] updating the firewall name, %s", d.Id())
_, err := apiClient.RenameFirewall(d.Id(), &firewall)
if err != nil {
return diag.Errorf("[WARN] an error occurred while tring to rename the firewall %s, %s", d.Id(), err)
return diag.Errorf("[WARN] an error occurred while trying to rename the firewall %s, %s", d.Id(), err)
}
}
}
Expand All @@ -215,7 +217,7 @@ func resourceFirewallUpdate(ctx context.Context, d *schema.ResourceData, m inter
// call the api to get the current rules
allRules, err := apiClient.ListFirewallRules(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to list the firewall rules, %s", err)
return diag.Errorf("[ERR] an error occurred while trying to list the firewall rules, %s", err)
}

// remove the rules that are not in terraform
Expand All @@ -225,15 +227,15 @@ func resourceFirewallUpdate(ctx context.Context, d *schema.ResourceData, m inter
log.Printf("[INFO] removing the ingress rule %s", rule.ID)
_, err := apiClient.DeleteFirewallRule(d.Id(), rule.ID)
if err != nil {
return diag.Errorf("[WARN] an error occurred while tring to delete the ingress rule %s, %s", rule.ID, err)
return diag.Errorf("[WARN] an error occurred while trying to delete the ingress rule %s, %s", rule.ID, err)
}
}
} else {
if !egressRulesContains(egressRules, rule) {
log.Printf("[INFO] removing the egress rule %s", rule.ID)
_, err := apiClient.DeleteFirewallRule(d.Id(), rule.ID)
if err != nil {
return diag.Errorf("[WARN] an error occurred while tring to delete the egress rule %s, %s", rule.ID, err)
return diag.Errorf("[WARN] an error occurred while trying to delete the egress rule %s, %s", rule.ID, err)
}
}
}
Expand All @@ -246,7 +248,7 @@ func resourceFirewallUpdate(ctx context.Context, d *schema.ResourceData, m inter
fwRule := firewallUpdateBuild(ingressRule, apiClient.Region, "ingress", d)
resp, err := apiClient.NewFirewallRule(fwRule)
if err != nil {
return diag.Errorf("[WARN] an error occurred while tring to create the ingress rule %s, %s", fwRule, err)
return diag.Errorf("[WARN] an error occurred while trying to create the ingress rule %s, %s", fwRule, err)
}
log.Printf("[INFO] creating a new ingress rule %s", resp.ID)
}
Expand All @@ -260,7 +262,7 @@ func resourceFirewallUpdate(ctx context.Context, d *schema.ResourceData, m inter
fwRule := firewallUpdateBuild(egressRule, apiClient.Region, "egress", d)
resp, err := apiClient.NewFirewallRule(fwRule)
if err != nil {
return diag.Errorf("[WARN] an error occurred while tring to create the egress rule %s, %s", fwRule, err)
return diag.Errorf("[WARN] an error occurred while trying to create the egress rule %s, %s", fwRule, err)
}
log.Printf("[INFO] creating a new egress rule %s", resp.ID)
}
Expand Down Expand Up @@ -289,10 +291,27 @@ func resourceFirewallDelete(_ context.Context, d *schema.ResourceData, m interfa
}

log.Printf("[INFO] deleting the firewall %s", firewallID)
_, err = apiClient.DeleteFirewall(firewallID)

deleteStateConf := &resource.StateChangeConf{
Pending: []string{"failed"},
Target: []string{"success"},
Refresh: func() (interface{}, string, error) {
resp, err := apiClient.DeleteFirewall(firewallID)
if err != nil {
return 0, "", err
}
return resp, string(resp.Result), nil
},
Timeout: 60 * time.Minute,
Delay: 3 * time.Second,
MinTimeout: 3 * time.Second,
NotFoundChecks: 10,
}
_, err = deleteStateConf.WaitForStateContext(context.Background())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the firewall %s, %s", firewallID, err)
return diag.Errorf("error waiting for firewall (%s) to be deleted: %s", firewallID, err)
}

return nil
}

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 @@ -202,7 +202,7 @@ func resourceFirewallRuleDelete(_ context.Context, d *schema.ResourceData, m int
log.Printf("[INFO] retriving the firewall rule %s", d.Id())
_, err := apiClient.DeleteFirewallRule(d.Get("firewall_id").(string), d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete firewall rule %s - %v", d.Id(), err)
return diag.Errorf("[ERR] an error occurred while trying to delete firewall rule %s - %v", d.Id(), err)
}
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 @@ -468,7 +468,7 @@ func resourceInstanceDelete(_ context.Context, d *schema.ResourceData, m interfa
log.Printf("[INFO] deleting the instance %s", d.Id())
_, err := apiClient.DeleteInstance(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete instance %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete instance %s", d.Id())
}
return nil
}
10 changes: 5 additions & 5 deletions civo/resource_instance_reserved_ip_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func resourceInstanceReservedIPCreate(ctx context.Context, d *schema.ResourceDat
// We check if the instance is valid and if it is not we return an error
instance, err := apiClient.GetInstance(d.Get("instance_id").(string))
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to get instance %s", d.Get("instance_id").(string))
return diag.Errorf("[ERR] an error occurred while trying to get instance %s", d.Get("instance_id").(string))
}

// We check if the reserved ip is valid and if it is not we return an error
reservedIP, err := apiClient.FindIP(d.Get("reserved_ip_id").(string))
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to get reserved ip %s", d.Get("reserved_ip_id").(string))
return diag.Errorf("[ERR] an error occurred while trying to get reserved ip %s", d.Get("reserved_ip_id").(string))
}

if reservedIP.AssignedTo.ID != "" {
Expand All @@ -75,7 +75,7 @@ func resourceInstanceReservedIPCreate(ctx context.Context, d *schema.ResourceDat

_, err = apiClient.AssignIP(reservedIP.ID, instance.ID, "instance", apiClient.Region)
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to assign reserved ip %s to instance %s", d.Get("reserved_ip_id").(string), d.Get("instance_id").(string))
return diag.Errorf("[ERR] an error occurred while trying to assign reserved ip %s to instance %s", d.Get("reserved_ip_id").(string), d.Get("instance_id").(string))
}

d.SetId(resource.UniqueId())
Expand Down Expand Up @@ -122,7 +122,7 @@ func resourceInstanceReservedIPRead(_ context.Context, d *schema.ResourceData, m
// We check if the reserved ip is valid and if it is not we return an error
reservedIP, err := apiClient.FindIP(reservedID)
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to get reserved ip %s", reservedID)
return diag.Errorf("[ERR] an error occurred while trying to get reserved ip %s", reservedID)
}

if reservedIP.AssignedTo.ID != instanceID {
Expand All @@ -148,7 +148,7 @@ func resourceInstanceReservedIPDelete(ctx context.Context, d *schema.ResourceDat
log.Printf("[INFO] unassign the ip (%s) from the instance", reservedIP)
_, err := apiClient.UnassignIP(reservedIP, apiClient.Region)
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to unassign the ip %s: %s", reservedIP, err)
return diag.Errorf("[ERR] an error occurred while trying to unassign the ip %s: %s", reservedIP, err)
}

createStateConf := &resource.StateChangeConf{
Expand Down
4 changes: 2 additions & 2 deletions civo/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func resourceKubernetesClusterCreate(ctx context.Context, d *schema.ResourceData
d.SetId(resp.ID)

createStateConf := &resource.StateChangeConf{
Pending: []string{"BUILDING", "AVAILABLE", "UPGRADING"},
Pending: []string{"BUILDING", "AVAILABLE", "UPGRADING", "SCALING"},
Target: []string{"ACTIVE"},
Refresh: func() (interface{}, string, error) {
resp, err := apiClient.GetKubernetesCluster(d.Id())
Expand Down Expand Up @@ -466,7 +466,7 @@ func resourceKubernetesClusterDelete(_ context.Context, d *schema.ResourceData,
log.Printf("[INFO] deleting the kubernetes cluster %s", d.Id())
_, err := apiClient.DeleteKubernetesCluster(d.Id())
if err != nil {
return diag.Errorf("[INFO] an error occurred while tring to delete the kubernetes cluster %s", err)
return diag.Errorf("[INFO] an error occurred while trying to delete the kubernetes cluster %s", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_kubernetes_cluster_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func resourceKubernetesClusterNodePoolDelete(ctx context.Context, d *schema.Reso
log.Printf("[INFO] deleting the kubernetes cluster %s", d.Id())
_, err = apiClient.UpdateKubernetesCluster(getKubernetesCluster.ID, config)
if err != nil {
return diag.Errorf("[INFO] an error occurred while tring to delete the kubernetes cluster pool %s", err)
return diag.Errorf("[INFO] an error occurred while trying to delete the kubernetes cluster pool %s", err)
}

err = waitForKubernetesNodePoolDelete(apiClient, d)
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_loadbalancer.go.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func resourceLoadBalancerDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] deleting the load balancer %s", d.Id())
_, err := apiClient.DeleteLoadBalancer(d.Id())
if err != nil {
return fmt.Errorf("[ERR] an error occurred while tring to delete load balancer %s", d.Id())
return fmt.Errorf("[ERR] an error occurred while trying to delete load balancer %s", d.Id())
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func resourceNetworkDelete(_ context.Context, d *schema.ResourceData, m interfac
log.Printf("[INFO] deleting the network %s", d.Id())
_, err := apiClient.DeleteNetwork(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the network %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete the network %s", d.Id())
}
return nil
}
2 changes: 1 addition & 1 deletion civo/resource_object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func resourceObjectStoreDelete(ctx context.Context, d *schema.ResourceData, m in
log.Printf("[INFO] deleting the Object Store %s", d.Id())
_, err := apiClient.DeleteObjectStore(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the Object Store %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete the Object Store %s", d.Id())
}
return nil
}
2 changes: 1 addition & 1 deletion civo/resource_object_store_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func resourceObjectStoreCredentialDelete(ctx context.Context, d *schema.Resource
log.Printf("[INFO] deleting the Object Store Credential %s", d.Id())
_, err := apiClient.DeleteObjectStoreCredential(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the Object Store Credential %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete the Object Store Credential %s", d.Id())
}
return nil
}
2 changes: 1 addition & 1 deletion civo/resource_reserved_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func resourceReservedIPDelete(_ context.Context, d *schema.ResourceData, m inter
log.Printf("[INFO] deleting the ip resource %s", d.Id())
_, err := apiClient.DeleteIP(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the ip resource %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete the ip resource %s", d.Id())
}
return nil
}
2 changes: 1 addition & 1 deletion civo/resource_snapshot.go.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func resourceSnapshotDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] deleting the snapshot %s", d.Id())
_, err := apiClient.DeleteSnapshot(d.Id())
if err != nil {
return fmt.Errorf("[ERR] an error occurred while tring to delete the snapshot %s", d.Id())
return fmt.Errorf("[ERR] an error occurred while trying to delete the snapshot %s", d.Id())
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions civo/resource_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func resourceSSHKeyUpdate(ctx context.Context, d *schema.ResourceData, m interfa
log.Printf("[INFO] updating the ssh key %s", d.Get("name").(string))
_, err := apiClient.UpdateSSHKey(d.Get("name").(string), d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to rename the ssh key %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to rename the ssh key %s", d.Id())
}
}
}
Expand All @@ -104,7 +104,7 @@ func resourceSSHKeyDelete(_ context.Context, d *schema.ResourceData, m interface
log.Printf("[INFO] deleting the ssh key %s", d.Id())
_, err := apiClient.DeleteSSHKey(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the ssh key %s", d.Id())
return diag.Errorf("[ERR] an error occurred while trying to delete the ssh key %s", d.Id())
}
return nil
}
4 changes: 2 additions & 2 deletions civo/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func resourceVolumeUpdate(ctx context.Context, d *schema.ResourceData, m interfa
_, err = apiClient.AttachVolume(d.Id(), resp.InstanceID)
if err != nil {
return fmt.Errorf("[ERR] an error occurred while tring to attach the volume %s", d.Id())
return fmt.Errorf("[ERR] an error occurred while trying to attach the volume %s", d.Id())
}
} else {
Expand Down Expand Up @@ -209,7 +209,7 @@ func resourceVolumeDelete(_ context.Context, d *schema.ResourceData, m interface
log.Printf("[INFO] deleting the volume %s", d.Id())
_, err := apiClient.DeleteVolume(d.Id())
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to delete the volume %s", err)
return diag.Errorf("[ERR] an error occurred while trying to delete the volume %s", err)
}
return nil
}
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 @@ -141,7 +141,7 @@ func resourceVolumeAttachmentDelete(_ context.Context, d *schema.ResourceData, m
log.Printf("[INFO] Detaching the volume %s", d.Id())
_, err := apiClient.DetachVolume(volumeID)
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to detach the volume %s", err)
return diag.Errorf("[ERR] an error occurred while trying to detach the volume %s", err)
}
return nil
}

0 comments on commit 37a99d6

Please sign in to comment.