Skip to content

Commit 2a3fd10

Browse files
authored
govc: add device.sata.add (#3936)
- Added 'device.sata.add' command to add SATA controllers to virtual machines. - Registered 'device.sata.add' in the 'govc' CLI entry point. - Updated documentation and test suite to cover the new command. Signed-off-by: Ryan Johnson <ryan.johnson@broadcom.com>
1 parent 4957c81 commit 2a3fd10

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

cli/device/sata/add.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package sata
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
12+
"github.com/vmware/govmomi/cli"
13+
"github.com/vmware/govmomi/cli/flags"
14+
)
15+
16+
type add struct {
17+
*flags.VirtualMachineFlag
18+
}
19+
20+
func init() {
21+
cli.Register("device.sata.add", &add{})
22+
}
23+
24+
func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
25+
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
26+
cmd.VirtualMachineFlag.Register(ctx, f)
27+
}
28+
29+
func (cmd *add) Description() string {
30+
return `Add SATA controller to VM.
31+
32+
Examples:
33+
govc device.sata.add -vm $vm
34+
govc device.info -vm $vm sata-*`
35+
}
36+
37+
func (cmd *add) Process(ctx context.Context) error {
38+
return cmd.VirtualMachineFlag.Process(ctx)
39+
}
40+
41+
func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
42+
vm, err := cmd.VirtualMachine()
43+
if err != nil {
44+
return err
45+
}
46+
47+
if vm == nil {
48+
return flag.ErrHelp
49+
}
50+
51+
devices, err := vm.Device(ctx)
52+
if err != nil {
53+
return err
54+
}
55+
56+
d, err := devices.CreateSATAController()
57+
if err != nil {
58+
return err
59+
}
60+
61+
err = vm.AddDevice(ctx, d)
62+
if err != nil {
63+
return err
64+
}
65+
66+
devices, err = vm.Device(ctx)
67+
if err != nil {
68+
return err
69+
}
70+
71+
devices = devices.SelectByType(d)
72+
73+
name := devices.Name(devices[len(devices)-1])
74+
75+
fmt.Println(name)
76+
77+
return nil
78+
}

govc/USAGE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ but appear via `govc $cmd -h`:
118118
- [device.pci.ls](#devicepcils)
119119
- [device.pci.remove](#devicepciremove)
120120
- [device.remove](#deviceremove)
121+
- [device.sata.add](#devicesataadd)
121122
- [device.scsi.add](#devicescsiadd)
122123
- [device.serial.add](#deviceserialadd)
123124
- [device.serial.connect](#deviceserialconnect)
@@ -2004,6 +2005,21 @@ Options:
20042005
-vm= Virtual machine [GOVC_VM]
20052006
```
20062007

2008+
## device.sata.add
2009+
2010+
```
2011+
Usage: govc device.sata.add [OPTIONS]
2012+
2013+
Add SATA controller to VM.
2014+
2015+
Examples:
2016+
govc device.sata.add -vm $vm
2017+
govc device.info -vm $vm sata-*
2018+
2019+
Options:
2020+
-vm= Virtual machine [GOVC_VM]
2021+
```
2022+
20072023
## device.scsi.add
20082024

20092025
```

govc/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
_ "github.com/vmware/govmomi/cli/device/floppy"
3232
_ "github.com/vmware/govmomi/cli/device/model"
3333
_ "github.com/vmware/govmomi/cli/device/pci"
34+
_ "github.com/vmware/govmomi/cli/device/sata"
3435
_ "github.com/vmware/govmomi/cli/device/scsi"
3536
_ "github.com/vmware/govmomi/cli/device/serial"
3637
_ "github.com/vmware/govmomi/cli/device/usb"

govc/test/device.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ load test_helper
227227
assert_failure "govc: device '$id' not found"
228228
}
229229

230+
@test "device.sata" {
231+
vcsim_env
232+
233+
vm=$(new_empty_vm)
234+
235+
run govc device.sata.add -vm $vm
236+
assert_success
237+
id=$output
238+
239+
result=$(govc device.ls -vm $vm | grep $id | wc -l)
240+
[ $result -eq 1 ]
241+
}
242+
230243
@test "device.scsi" {
231244
vcsim_env
232245

0 commit comments

Comments
 (0)