diff --git a/vultr/resource_firewall_rule.go b/vultr/resource_firewall_rule.go index f246c9f2..7b7200fa 100644 --- a/vultr/resource_firewall_rule.go +++ b/vultr/resource_firewall_rule.go @@ -173,6 +173,9 @@ func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error } func splitFirewallRule(portRange string) (int, int, error) { + if len(portRange) == 0 { + return 0, 0, nil + } ports := strings.Split(portRange, "-") from, err := strconv.Atoi(strings.TrimSpace(ports[0])) if err != nil { diff --git a/vultr/resource_firewall_rule_test.go b/vultr/resource_firewall_rule_test.go new file mode 100644 index 00000000..a48c0a48 --- /dev/null +++ b/vultr/resource_firewall_rule_test.go @@ -0,0 +1,77 @@ +package vultr + +import ( + "testing" +) + +func TestSplitFirewallRule(t *testing.T) { + cases := []struct { + portRange string + from int + to int + err bool + }{ + { + portRange: "", + from: 0, + to: 0, + err: false, + }, + { + portRange: ":", + from: 0, + to: 0, + err: true, + }, + { + portRange: "-", + from: 0, + to: 0, + err: true, + }, + { + portRange: "22", + from: 22, + to: 22, + err: false, + }, + { + portRange: "foo", + from: 0, + to: 0, + err: true, + }, + { + portRange: "22:23", + from: 0, + to: 0, + err: true, + }, + { + portRange: "22 - 23", + from: 22, + to: 23, + err: false, + }, + { + portRange: "80-81", + from: 80, + to: 81, + err: false, + }, + } + + for i, c := range cases { + from, to, err := splitFirewallRule(c.portRange) + if (err != nil) != c.err { + no := "no" + if c.err { + no = "an" + } + t.Errorf("test case %d: expected %s error, got %v", i, no, err) + } + if from != c.from || to != c.to { + t.Errorf("test case %d: expected range %d:%d, got %d:%d", i, c.from, c.to, from, to) + } + } +}