Skip to content

Commit

Permalink
Create Default firewall for network when not specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Jun 19, 2024
1 parent b13b827 commit 7292132
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 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"
"fmt"
"log"
"time"

Expand Down Expand Up @@ -134,6 +135,15 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interf
return err
}
d.SetId(network.ID)
// Check if a default firewall needs to be created
if _, ok := d.GetOk("firewall_id"); !ok {
log.Printf("[INFO] Creating default firewall for the network %s", d.Get("label").(string))
err := createDefaultFirewall(apiClient, network.ID)
if err != nil {
return err
}
}

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

Expand Down Expand Up @@ -256,3 +266,21 @@ func expandStringList(input interface{}) []string {
}
return result
}

// createDefaultFirewall function to create a default firewall
func createDefaultFirewall(apiClient *civogo.Client, networkID string) error {

firewallConfig := civogo.FirewallConfig{
Name: fmt.Sprintf("default-firewall-%s", networkID),
NetworkID: networkID,
Region: apiClient.Region,
}

// Create the default firewall
_, err := apiClient.NewFirewall(&firewallConfig)
if err != nil {
return err
}

return nil
}

0 comments on commit 7292132

Please sign in to comment.