Skip to content

Commit

Permalink
Validation to set create_default_rules to false when any of the firew…
Browse files Browse the repository at this point in the history
…all rule is specified (#315)

* Validation to set create_default_rules to false when any of the fw rules are specified.
  • Loading branch information
uzaxirr authored Aug 9, 2024
1 parent 7249f1e commit 6b1264a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions civo/firewall/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,35 @@ func ResourceFirewall() *schema.Resource {
DeleteContext: resourceFirewallDelete,
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {

ingressRules := diff.Get("ingress_rule")
egressRules := diff.Get("egress_rule")
if diff.HasChange("create_default_rules") {
createDefaultRules := diff.Get("create_default_rules").(bool)
ingressRules := diff.Get("ingress_rule")
egressRules := diff.Get("egress_rule")

if createDefaultRules && (ingressRules.(*schema.Set).Len() > 0 || egressRules.(*schema.Set).Len() > 0) {
return fmt.Errorf("create_default_rules can't be true when ingress_rule or egress_rule is specified")
}
}

ingressRules := diff.Get("ingress_rule")
for _, v := range ingressRules.(*schema.Set).List() {
ingress := v.(map[string]interface{})
protocol := ingress["protocol"]

port := ingress["port_range"]
if protocol != "icmp" && port == "" {
return fmt.Errorf("`ports` of ingress rules is required if protocol is `tcp` or `udp`")
return fmt.Errorf("ports of ingress rules is required if protocol is tcp or udp")
}
}

egressRules := diff.Get("egress_rule")
for _, v := range egressRules.(*schema.Set).List() {
egress := v.(map[string]interface{})
protocol := egress["protocol"]

port := egress["port_range"]
if protocol != "icmp" && port == "" {
return fmt.Errorf("`ports` of egress rules is required if protocol is `tcp` or `udp`")
return fmt.Errorf("ports of egress rules is required if protocol is tcp or udp")
}
}

Expand Down

0 comments on commit 6b1264a

Please sign in to comment.