Skip to content

Commit 16bf676

Browse files
committed
driverutil: refactor splitPortProto to parse port as str
Signed-off-by: André Carvalho <andre.carvalho@corp.globo.com>
1 parent 7539b91 commit 16bf676

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

drivers/amazonec2/amazonec2.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,10 +1108,14 @@ func (d *Driver) configureSecurityGroupPermissions(group *ec2.SecurityGroup) ([]
11081108
if err != nil {
11091109
return nil, err
11101110
}
1111+
portNum, err := strconv.Atoi(port)
1112+
if err != nil {
1113+
return nil, fmt.Errorf("invalid port number %s: %s", port, err)
1114+
}
11111115
perms = append(perms, &ec2.IpPermission{
11121116
IpProtocol: aws.String(protocol),
1113-
FromPort: aws.Int64(int64(port)),
1114-
ToPort: aws.Int64(int64(port)),
1117+
FromPort: aws.Int64(int64(portNum)),
1118+
ToPort: aws.Int64(int64(portNum)),
11151119
IpRanges: []*ec2.IpRange{{CidrIp: aws.String(ipRange)}},
11161120
})
11171121
}

drivers/azure/util.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io/ioutil"
66
"net"
77
"net/url"
8-
"strconv"
98
"strings"
109

1110
"github.com/Azure/azure-sdk-for-go/arm/network"
@@ -128,11 +127,10 @@ func (d *Driver) getSecurityRules(extraPorts []string) (*[]network.SecurityRule,
128127
// extra port numbers requested by user
129128
basePri := 1000
130129
for i, p := range extraPorts {
131-
n, protocol, err := driverutil.SplitPortProto(p)
130+
port, protocol, err := driverutil.SplitPortProto(p)
132131
if err != nil {
133132
return nil, err
134133
}
135-
port := strconv.Itoa(n)
136134
proto, err := parseSecurityRuleProtocol(protocol)
137135
if err != nil {
138136
return nil, err

drivers/driverutil/util.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
package driverutil
22

3-
import (
4-
"fmt"
5-
"strconv"
6-
"strings"
7-
)
3+
import "strings"
84

95
// SplitPortProto splits a string in the format port/protocol, defaulting
106
// protocol to "tcp" if not provided.
11-
func SplitPortProto(raw string) (port int, protocol string, err error) {
7+
func SplitPortProto(raw string) (port string, protocol string, err error) {
128
parts := strings.Split(raw, "/")
139
if len(parts) == 1 {
1410
protocol = "tcp"
1511
} else {
1612
protocol = parts[1]
1713
}
18-
port, err = strconv.Atoi(parts[0])
19-
if err != nil {
20-
return 0, "", fmt.Errorf("invalid port number %s: %s", parts[0], err)
21-
}
14+
port = parts[0]
2215
return port, protocol, nil
2316
}

drivers/driverutil/util_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99
func TestSplitPortProtocol(t *testing.T) {
1010
tests := []struct {
1111
raw string
12-
expectedPort int
12+
expectedPort string
1313
expectedProto string
1414
expectedErr bool
1515
}{
16-
{"8080/tcp", 8080, "tcp", false},
17-
{"90/udp", 90, "udp", false},
18-
{"80", 80, "tcp", false},
19-
{"abc", 0, "", true},
16+
{"8080/tcp", "8080", "tcp", false},
17+
{"90/udp", "90", "udp", false},
18+
{"80", "80", "tcp", false},
2019
}
2120

2221
for _, tc := range tests {

0 commit comments

Comments
 (0)