@@ -2,7 +2,6 @@ package iaas_clients
22
33import (
44 "encoding/json"
5- "errors"
65 "fmt"
76 "net/url"
87 "os"
@@ -21,27 +20,34 @@ type VcenterClient struct {
2120}
2221
2322func NewVcenterClient (username string , password string , u string , caCertFile string , runner iaas_cli.CliRunner ) * VcenterClient {
24-
2523 encodedUser := url .QueryEscape (username )
2624 encodedPassword := url .QueryEscape (password )
2725 urlWithCredentials := fmt .Sprintf ("%s:%s@%s" , encodedUser , encodedPassword , u )
2826 urlWithRedactedPassword := fmt .Sprintf ("%s:REDACTED@%s" , encodedUser , u )
29- return & VcenterClient {Url : u , credentialUrl : urlWithCredentials , redactedUrl : urlWithRedactedPassword , caCertFile : caCertFile , Runner : runner }
27+
28+ return & VcenterClient {
29+ Url : u ,
30+ credentialUrl : urlWithCredentials ,
31+ redactedUrl : urlWithRedactedPassword ,
32+ caCertFile : caCertFile ,
33+ Runner : runner ,
34+ }
3035}
3136
3237func (c * VcenterClient ) ValidateUrl () error {
3338 args := []string {"about" , "-u" , c .Url }
34- errMsg := fmt . Sprintf ( "vcenter_client - unable to validate url: %s" , c . Url )
39+ errMsg := " unable to validate url"
3540 if c .caCertFile != "" {
3641 args = append (args , fmt .Sprintf ("-tls-ca-certs=%s" , c .caCertFile ))
37- errMsg = fmt . Sprintf ( "vcenter_client - invalid ca certs or url: %s" , c . Url )
42+ errMsg = " invalid ca certs or url"
3843 }
44+
3945 errCode := c .Runner .Run (args )
4046 if errCode != 0 {
41- return errors . New ( errMsg )
47+ return fmt . Errorf ( "vcenter_client - %s: %s" , errMsg , c . Url )
4248 }
43- return nil
4449
50+ return nil
4551}
4652
4753func (c * VcenterClient ) ValidateCredentials () error {
@@ -84,6 +90,7 @@ func (c *VcenterClient) ListDevices(vmInventoryPath string) ([]string, error) {
8490 devices = append (devices , r .FindString (entry ))
8591 }
8692 }
93+
8794 return devices , nil
8895}
8996func (c * VcenterClient ) RemoveDevice (vmInventoryPath string , deviceName string ) error {
@@ -92,16 +99,17 @@ func (c *VcenterClient) RemoveDevice(vmInventoryPath string, deviceName string)
9299 if errCode != 0 {
93100 return fmt .Errorf ("vcenter_client - %s could not be removed" , deviceName )
94101 }
102+
95103 return nil
96104}
97105
98106func (c * VcenterClient ) EjectCDRom (vmInventoryPath string , deviceName string ) error {
99-
100107 args := c .buildGovcCommand ("device.cdrom.eject" , "-vm" , vmInventoryPath , "-device" , deviceName )
101108 errCode := c .Runner .Run (args )
102109 if errCode != 0 {
103110 return fmt .Errorf ("vcenter_client - %s could not be ejected" , deviceName )
104111 }
112+
105113 return nil
106114}
107115
@@ -115,6 +123,7 @@ func (c *VcenterClient) ExportVM(vmInventoryPath string, destination string) err
115123 if errCode != 0 {
116124 return fmt .Errorf ("vcenter_client - %s could not be exported" , vmInventoryPath )
117125 }
126+
118127 return nil
119128}
120129
@@ -125,6 +134,7 @@ func (c *VcenterClient) UploadArtifact(vmInventoryPath, artifact, destination, u
125134 if errCode != 0 {
126135 return fmt .Errorf ("vcenter_client - %s could not be uploaded" , artifact )
127136 }
137+
128138 return nil
129139}
130140
@@ -136,6 +146,7 @@ func (c *VcenterClient) MakeDirectory(vmInventoryPath, path, username, password
136146 if errCode != 0 {
137147 return fmt .Errorf ("vcenter_client - directory `%s` could not be created" , path )
138148 }
149+
139150 return nil
140151}
141152
@@ -150,8 +161,8 @@ func (c *VcenterClient) Start(vmInventoryPath, username, password, command strin
150161 if exitCode != 0 {
151162 return "" , fmt .Errorf ("vcenter_client - '%s' returned exit code: %d" , command , exitCode )
152163 }
153- // We trim this suffix since govc outputs the pid with an '\n' in the output
154- return strings .TrimSuffix (pid , "\n " ), nil
164+
165+ return strings .TrimSuffix (pid , "\n " ), nil // trim since govc outputs the pid with an '\n' in the output
155166}
156167
157168type govcPS struct {
@@ -195,6 +206,7 @@ func (c *VcenterClient) buildGovcCommand(args ...string) []string {
195206 commonArgs = append (commonArgs , fmt .Sprintf ("-tls-ca-certs=%s" , c .caCertFile ))
196207 }
197208 args = append (args [:1 ], append (commonArgs , args [1 :]... )... )
209+
198210 return args
199211}
200212
@@ -204,6 +216,7 @@ func (c *VcenterClient) IsPoweredOff(vmInventoryPath string) (bool, error) {
204216 if exitCode != 0 {
205217 return false , fmt .Errorf ("vcenter_client - failed to get vm info, govc exit code: %d" , exitCode )
206218 }
219+
207220 if err != nil {
208221 return false , fmt .Errorf ("vcenter_client - failed to determine vm power state: %s" , err )
209222 }
0 commit comments