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 if firewall exists before deleting #78

Merged
merged 1 commit into from
Sep 2, 2021
Merged
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
34 changes: 29 additions & 5 deletions civo/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func resourceFirewallCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)
networkID := ""

if attr, ok := d.GetOk("region"); ok {
apiClient.Region = attr.(string)
// overwrite the region if it's defined
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

if attr, ok := d.GetOk("network_id"); ok {
Expand Down Expand Up @@ -73,6 +74,11 @@ func resourceFirewallCreate(d *schema.ResourceData, m interface{}) error {
func resourceFirewallRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

// overwrite the region if it's defined
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

log.Printf("[INFO] retriving the firewall %s", d.Id())
resp, err := apiClient.FindFirewall(d.Id())
if err != nil {
Expand All @@ -94,6 +100,11 @@ func resourceFirewallRead(d *schema.ResourceData, m interface{}) error {
func resourceFirewallUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

// overwrite the region if it's defined
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

if d.HasChange("name") {
if d.Get("name").(string) != "" {
firewall := civogo.FirewallConfig{
Expand All @@ -114,10 +125,23 @@ func resourceFirewallUpdate(d *schema.ResourceData, m interface{}) error {
func resourceFirewallDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] deleting the firewall %s", d.Id())
_, err := apiClient.DeleteFirewall(d.Id())
// overwrite the region if it's defined
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

firewallID := d.Id()
log.Printf("[INFO] Checking if firewall %s exists", firewallID)
_, err := apiClient.FindFirewall(firewallID)
if err != nil {
log.Printf("[INFO] Unable to find firewall %s - probably it's been deleted", firewallID)
return nil
}

log.Printf("[INFO] deleting the firewall %s", firewallID)
_, err = apiClient.DeleteFirewall(firewallID)
if err != nil {
return fmt.Errorf("[ERR] an error occurred while tring to delete the firewall %s, %s", d.Id(), err)
return fmt.Errorf("[ERR] an error occurred while tring to delete the firewall %s, %s", firewallID, err)
}
return nil
}