Skip to content

Commit dcce804

Browse files
committed
Supporting multiple ports per host in JSON (fixes #11)
1 parent ac85afb commit dcce804

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ See [test-data/input.json](test-data/input.json) for full example. In the **host
4646
},
4747
"region-b": {
4848
"selenium-cloud-b-[1:40].example.com": {
49-
"port": 4445,
49+
"ports": "444[5:8]",
5050
"count": 2
5151
}
5252
}

cmd/data.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
type Host struct {
44
Port int `json:"port"`
5+
Ports string `json:"ports"`
56
Count int `json:"count"`
67
Username string `json:"username"`
78
Password string `json:"password"`

cmd/generate.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,20 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region {
126126
for hostPattern, host := range region {
127127
hostNames := parseHostPattern(hostPattern)
128128
for _, hostName := range hostNames {
129-
h := ggr.Host{
130-
Name: hostName,
131-
Port: host.Port,
132-
Count: host.Count,
133-
Username: host.Username,
134-
Password: host.Password,
129+
ports := getPorts(host.Port, host.Ports)
130+
for _, port := range ports {
131+
h := ggr.Host{
132+
Name: hostName,
133+
Port: port,
134+
Count: host.Count,
135+
Username: host.Username,
136+
Password: host.Password,
137+
}
138+
if host.VNC != "" {
139+
h.VNC = preProcessVNC(hostName, port, host.VNC)
140+
}
141+
xmlHosts = append(xmlHosts, h)
135142
}
136-
if host.VNC != "" {
137-
h.VNC = preProcessVNC(hostName, host.Port, host.VNC)
138-
}
139-
xmlHosts = append(xmlHosts, h)
140143
}
141144
}
142145
xmlRegions = append(xmlRegions, ggr.Region{
@@ -147,6 +150,19 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region {
147150
return xmlRegions
148151
}
149152

153+
func getPorts(port int, ports string) []int {
154+
if ports != "" {
155+
var ret []int
156+
for _, maybePort := range parseHostPattern(ports) {
157+
if port, err := strconv.Atoi(maybePort); err == nil {
158+
ret = append(ret, port)
159+
}
160+
}
161+
return ret
162+
}
163+
return []int{port}
164+
}
165+
150166
func preProcessVNC(hostName string, port int, vnc string) string {
151167
const selenoid = "selenoid"
152168
const hostPattern = "$hostName"

cmd/generate_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,11 @@ func TestPreProcessVNC(t *testing.T) {
112112
AssertThat(t, preProcessVNC("selenoid-host.example.com", 4444, "selenoid"), EqualTo{"ws://selenoid-host.example.com:4444/vnc"})
113113
AssertThat(t, preProcessVNC("vnc-host.example.com", 5900, "vnc://$hostName:5900"), EqualTo{"vnc://vnc-host.example.com:5900"})
114114
}
115+
116+
func TestGetPorts(t *testing.T) {
117+
AssertThat(t, getPorts(4444, ""), EqualTo{[]int{4444}})
118+
AssertThat(t, getPorts(4444, "4445"), EqualTo{[]int{4445}})
119+
AssertThat(t, getPorts(4444, "444[5:8]"), EqualTo{[]int{4445, 4446, 4447, 4448}})
120+
AssertThat(t, getPorts(4444, "44[5:8]4"), EqualTo{[]int{4454, 4464, 4474, 4484}})
121+
AssertThat(t, len(getPorts(4444, "NaN")), EqualTo{0})
122+
}

0 commit comments

Comments
 (0)