Skip to content

Commit ec3d962

Browse files
committed
utilize new connectiontype enum
1 parent 42a900c commit ec3d962

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

provisioner/communication.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
1717
// declare communication args
1818
var args []string
1919

20-
// determine communication string by packer connection type
21-
connectionType, ok := provisioner.generatedData["ConnType"].(string)
22-
if !ok {
20+
// determine communication type enum by packer data
21+
connectionType, err := connectionType(provisioner.generatedData["ConnType"].(string)).New()
22+
if err != nil {
23+
// do not log the enum returned error because it is only a symptom of the packer data issue
2324
ui.Error("packer is unable to resolve the communicator connection type")
2425
return nil, errors.New("unknown communicator connection type")
2526
}
@@ -28,7 +29,7 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
2829

2930
// determine communication based on connection type
3031
switch connectionType {
31-
case "ssh":
32+
case ssh:
3233
// assign user and host address
3334
user, httpAddr, err := provisioner.determineUserAddr(connectionType, ui)
3435
if err != nil {
@@ -84,7 +85,7 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
8485

8586
return nil, errors.New("unsupported ssh auth type")
8687
}
87-
case "winrm":
88+
case winrm:
8889
// assign user and host address
8990
user, httpAddr, err := provisioner.determineUserAddr(connectionType, ui)
9091
if err != nil {
@@ -116,7 +117,7 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
116117

117118
// append args with winrm connection backend information (user, password, host, port)
118119
args = append(args, connectionBackend)
119-
case "docker", "podman", "lxc":
120+
case docker, podman, lxc:
120121
// determine instanceid
121122
instanceID, ok := provisioner.generatedData["ID"].(string)
122123
if !ok || len(instanceID) == 0 {
@@ -127,6 +128,7 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
127128
// append args with container connection backend information (instanceid)
128129
args = append(args, fmt.Sprintf("--hosts=%s://%s", connectionType, instanceID))
129130
default:
131+
// should be unreachable due to earlier enum validation, but here for safety
130132
ui.Errorf("communication backend with machine image is not supported, and was resolved to '%s'", connectionType)
131133
return nil, errors.New("unsupported communication type")
132134
}
@@ -137,23 +139,23 @@ func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string,
137139
}
138140

139141
// determine and return user and host address
140-
func (provisioner *Provisioner) determineUserAddr(connectionType string, ui packer.Ui) (string, string, error) {
142+
func (provisioner *Provisioner) determineUserAddr(connType connectionType, ui packer.Ui) (string, string, error) {
141143
// ssh and winrm provisioner generated data maps
142-
genDataMap := map[string]map[string]string{
143-
"ssh": {
144+
genDataMap := map[connectionType]map[string]string{
145+
ssh: {
144146
"user": "SSHUsername",
145147
"host": "SSHHost",
146148
"port": "SSHPort",
147149
},
148-
"winrm": {
150+
winrm: {
149151
"user": "WinRMUser",
150152
"host": "WinRMHost",
151153
"port": "WinRMPort",
152154
},
153155
}
154156

155157
// determine user based on connection protocol
156-
user, ok := provisioner.generatedData[genDataMap[connectionType]["user"]].(string)
158+
user, ok := provisioner.generatedData[genDataMap[connType]["user"]].(string)
157159
if !ok || len(user) == 0 {
158160
// fallback to general user (usually packer)
159161
user, ok = provisioner.generatedData["User"].(string)
@@ -165,7 +167,7 @@ func (provisioner *Provisioner) determineUserAddr(connectionType string, ui pack
165167
}
166168

167169
// determine host address and port based on connection protocol
168-
ipaddress, ok := provisioner.generatedData[genDataMap[connectionType]["host"]].(string)
170+
ipaddress, ok := provisioner.generatedData[genDataMap[connType]["host"]].(string)
169171
if !ok || len(ipaddress) == 0 {
170172
// fallback to general host information
171173
ipaddress, ok = provisioner.generatedData["Host"].(string)
@@ -177,7 +179,7 @@ func (provisioner *Provisioner) determineUserAddr(connectionType string, ui pack
177179
}
178180

179181
// valid ip address so now determine port
180-
port, ok := provisioner.generatedData[genDataMap[connectionType]["port"]].(int)
182+
port, ok := provisioner.generatedData[genDataMap[connType]["port"]].(int)
181183
if !ok || port == 0 {
182184
// fallback to general port
183185
port, ok = provisioner.generatedData["Port"].(int)

provisioner/communication_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) {
156156
}
157157

158158
_, err = provisioner.determineCommunication(ui)
159-
if err == nil || err.Error() != "unsupported communication type" {
159+
if err == nil || err.Error() != "unknown communicator connection type" {
160160
test.Error("determineCommunication function did not fail on unsupported connection type")
161161
}
162162
}
@@ -173,7 +173,7 @@ func TestDetermineUserAddr(test *testing.T) {
173173
"Port": 22,
174174
}
175175

176-
user, httpAddr, err := provisioner.determineUserAddr("ssh", ui)
176+
user, httpAddr, err := provisioner.determineUserAddr(ssh, ui)
177177
if err != nil {
178178
test.Error("determineUserAddr failed to determine user and address")
179179
test.Error(err)
@@ -189,17 +189,17 @@ func TestDetermineUserAddr(test *testing.T) {
189189
}
190190

191191
delete(provisioner.generatedData, "Port")
192-
if _, _, err = provisioner.determineUserAddr("ssh", ui); err == nil || err.Error() != "unknown host port" {
192+
if _, _, err = provisioner.determineUserAddr(ssh, ui); err == nil || err.Error() != "unknown host port" {
193193
test.Error("determineCommunication did not fail on unknown port")
194194
}
195195

196196
delete(provisioner.generatedData, "Host")
197-
if _, _, err = provisioner.determineUserAddr("ssh", ui); err == nil || err.Error() != "unknown host address" {
197+
if _, _, err = provisioner.determineUserAddr(ssh, ui); err == nil || err.Error() != "unknown host address" {
198198
test.Error("determineCommunication did not fail on unknown host")
199199
}
200200

201201
delete(provisioner.generatedData, "User")
202-
if _, _, err = provisioner.determineUserAddr("ssh", ui); err == nil || err.Error() != "unknown remote user" {
202+
if _, _, err = provisioner.determineUserAddr(ssh, ui); err == nil || err.Error() != "unknown remote user" {
203203
test.Error("determineCommunication did not fail on unknown user")
204204
}
205205
}

0 commit comments

Comments
 (0)