Skip to content

Commit f486b57

Browse files
committed
stembuild: reorganize VcenterClient
- extract method to build VM auth parameter `-l ...` - rename internal struct for clarity - normalize spacing - move private members to end of file
1 parent 6514f8e commit f486b57

File tree

2 files changed

+448
-411
lines changed

2 files changed

+448
-411
lines changed

stembuild/iaas_cli/iaas_clients/vcenter_client.go

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (c *VcenterClient) ListDevices(vmInventoryPath string) ([]string, error) {
9393

9494
return devices, nil
9595
}
96+
9697
func (c *VcenterClient) RemoveDevice(vmInventoryPath string, deviceName string) error {
9798
args := c.buildGovcCommand("device.remove", "-vm", vmInventoryPath, deviceName)
9899
errCode := c.Runner.Run(args)
@@ -128,8 +129,7 @@ func (c *VcenterClient) ExportVM(vmInventoryPath string, destination string) err
128129
}
129130

130131
func (c *VcenterClient) UploadArtifact(vmInventoryPath, artifact, destination, username, password string) error {
131-
vmCredentials := fmt.Sprintf("%s:%s", username, password)
132-
args := c.buildGovcCommand("guest.upload", "-f", "-l", vmCredentials, "-vm", vmInventoryPath, artifact, destination)
132+
args := c.buildGovcCommand("guest.upload", "-f", "-l", vmCredentials(username, password), "-vm", vmInventoryPath, artifact, destination)
133133
errCode := c.Runner.Run(args)
134134
if errCode != 0 {
135135
return fmt.Errorf("vcenter_client - %s could not be uploaded", artifact)
@@ -139,9 +139,7 @@ func (c *VcenterClient) UploadArtifact(vmInventoryPath, artifact, destination, u
139139
}
140140

141141
func (c *VcenterClient) MakeDirectory(vmInventoryPath, path, username, password string) error {
142-
vmCredentials := fmt.Sprintf("%s:%s", username, password)
143-
144-
args := c.buildGovcCommand("guest.mkdir", "-l", vmCredentials, "-vm", vmInventoryPath, "-p", path)
142+
args := c.buildGovcCommand("guest.mkdir", "-l", vmCredentials(username, password), "-vm", vmInventoryPath, "-p", path)
145143
errCode := c.Runner.Run(args)
146144
if errCode != 0 {
147145
return fmt.Errorf("vcenter_client - directory `%s` could not be created", path)
@@ -150,10 +148,26 @@ func (c *VcenterClient) MakeDirectory(vmInventoryPath, path, username, password
150148
return nil
151149
}
152150

153-
func (c *VcenterClient) Start(vmInventoryPath, username, password, command string, args ...string) (string, error) {
154-
vmCredentials := fmt.Sprintf("%s:%s", username, password)
151+
func (c *VcenterClient) IsPoweredOff(vmInventoryPath string) (bool, error) {
152+
args := c.buildGovcCommand("vm.info", vmInventoryPath)
153+
out, exitCode, err := c.Runner.RunWithOutput(args)
154+
if exitCode != 0 {
155+
return false, fmt.Errorf("vcenter_client - failed to get vm info, govc exit code: %d", exitCode)
156+
}
157+
158+
if err != nil {
159+
return false, fmt.Errorf("vcenter_client - failed to determine vm power state: %s", err)
160+
}
161+
162+
if strings.Contains(out, "poweredOff") {
163+
return true, nil
164+
}
155165

156-
cmdArgs := c.buildGovcCommand(append([]string{"guest.start", "-l", vmCredentials, "-vm", vmInventoryPath, command}, args...)...)
166+
return false, nil
167+
}
168+
169+
func (c *VcenterClient) Start(vmInventoryPath, username, password, command string, args ...string) (string, error) {
170+
cmdArgs := c.buildGovcCommand(append([]string{"guest.start", "-l", vmCredentials(username, password), "-vm", vmInventoryPath, command}, args...)...)
157171
pid, exitCode, err := c.Runner.RunWithOutput(cmdArgs)
158172
if err != nil {
159173
return "", fmt.Errorf("vcenter_client - failed to run '%s': %s", command, err)
@@ -165,21 +179,8 @@ func (c *VcenterClient) Start(vmInventoryPath, username, password, command strin
165179
return strings.TrimSuffix(pid, "\n"), nil // trim since govc outputs the pid with an '\n' in the output
166180
}
167181

168-
type govcPS struct {
169-
ProcessInfo []struct {
170-
Name string
171-
Pid int
172-
Owner string
173-
CmdLine string
174-
StartTime string
175-
EndTime string
176-
ExitCode int
177-
}
178-
}
179-
180182
func (c *VcenterClient) WaitForExit(vmInventoryPath, username, password, pid string) (int, error) {
181-
vmCredentials := fmt.Sprintf("%s:%s", username, password)
182-
args := c.buildGovcCommand("guest.ps", "-l", vmCredentials, "-vm", vmInventoryPath, "-p", pid, "-X", "-json")
183+
args := c.buildGovcCommand("guest.ps", "-l", vmCredentials(username, password), "-vm", vmInventoryPath, "-p", pid, "-X", "-json")
183184
output, exitCode, err := c.Runner.RunWithOutput(args)
184185
if err != nil {
185186
return 0, fmt.Errorf("vcenter_client - failed to fetch exit code for PID %s: %s", pid, err)
@@ -188,16 +189,16 @@ func (c *VcenterClient) WaitForExit(vmInventoryPath, username, password, pid str
188189
return 0, fmt.Errorf("vcenter_client - fetching PID %s returned with exit code: %d", pid, exitCode)
189190
}
190191

191-
ps := govcPS{}
192-
err = json.Unmarshal([]byte(output), &ps)
192+
var processInfos govcProcessInfos
193+
err = json.Unmarshal([]byte(output), &processInfos)
193194
if err != nil {
194195
return 0, fmt.Errorf("vcenter_client - received bad JSON output for PID %s: %s", pid, output)
195196
}
196-
if len(ps.ProcessInfo) != 1 {
197+
if len(processInfos.ProcessInfo) != 1 {
197198
return 0, fmt.Errorf("vcenter_client - couldn't get exit code for PID %s", pid)
198199
}
199200

200-
return ps.ProcessInfo[0].ExitCode, nil
201+
return processInfos.ProcessInfo[0].ExitCode, nil
201202
}
202203

203204
func (c *VcenterClient) buildGovcCommand(args ...string) []string {
@@ -210,20 +211,18 @@ func (c *VcenterClient) buildGovcCommand(args ...string) []string {
210211
return args
211212
}
212213

213-
func (c *VcenterClient) IsPoweredOff(vmInventoryPath string) (bool, error) {
214-
args := c.buildGovcCommand("vm.info", vmInventoryPath)
215-
out, exitCode, err := c.Runner.RunWithOutput(args)
216-
if exitCode != 0 {
217-
return false, fmt.Errorf("vcenter_client - failed to get vm info, govc exit code: %d", exitCode)
218-
}
219-
220-
if err != nil {
221-
return false, fmt.Errorf("vcenter_client - failed to determine vm power state: %s", err)
222-
}
214+
func vmCredentials(username, password string) string {
215+
return fmt.Sprintf("%s:%s", username, password)
216+
}
223217

224-
if strings.Contains(out, "poweredOff") {
225-
return true, nil
218+
type govcProcessInfos struct {
219+
ProcessInfo []struct {
220+
Name string
221+
Pid int
222+
Owner string
223+
CmdLine string
224+
StartTime string
225+
EndTime string
226+
ExitCode int
226227
}
227-
228-
return false, nil
229228
}

0 commit comments

Comments
 (0)