@@ -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 )
0 commit comments