Skip to content

Commit

Permalink
govc: add 'device.clock.add' command
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed May 6, 2022
1 parent ac1eba3 commit fc17df0
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
16 changes: 16 additions & 0 deletions govc/USAGE.md
Expand Up @@ -86,6 +86,7 @@ but appear via `govc $cmd -h`:
- [device.cdrom.add](#devicecdromadd)
- [device.cdrom.eject](#devicecdromeject)
- [device.cdrom.insert](#devicecdrominsert)
- [device.clock.add](#deviceclockadd)
- [device.connect](#deviceconnect)
- [device.disconnect](#devicedisconnect)
- [device.floppy.add](#devicefloppyadd)
Expand Down Expand Up @@ -1275,6 +1276,21 @@ Options:
-vm= Virtual machine [GOVC_VM]
```

## device.clock.add

```
Usage: govc device.clock.add [OPTIONS]
Add precision clock device to VM.
Examples:
govc device.clock.add -vm $vm
govc device.info clock-*
Options:
-vm= Virtual machine [GOVC_VM]
```

## device.connect

```
Expand Down
86 changes: 86 additions & 0 deletions govc/device/clock/add.go
@@ -0,0 +1,86 @@
/*
Copyright (c) 2022 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package floppy

import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/types"
)

type add struct {
*flags.VirtualMachineFlag
}

func init() {
cli.Register("device.clock.add", &add{})
}

func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
cmd.VirtualMachineFlag.Register(ctx, f)
}

func (cmd *add) Description() string {
return `Add precision clock device to VM.
Examples:
govc device.clock.add -vm $vm
govc device.info clock-*`
}

func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
vm, err := cmd.VirtualMachine()
if err != nil {
return err
}

if vm == nil {
return flag.ErrHelp
}

d := &types.VirtualPrecisionClock{
VirtualDevice: types.VirtualDevice{
Backing: &types.VirtualPrecisionClockSystemClockBackingInfo{
Protocol: string(types.HostDateTimeInfoProtocolPtp), // TODO: ntp option
},
},
}

err = vm.AddDevice(ctx, d)
if err != nil {
return err
}

// output name of device we just created
devices, err := vm.Device(ctx)
if err != nil {
return err
}

devices = devices.SelectByType(d)

name := devices.Name(devices[len(devices)-1])

fmt.Println(name)

return nil
}
8 changes: 8 additions & 0 deletions govc/device/info.go
Expand Up @@ -243,6 +243,14 @@ func (r *infoResult) Write(w io.Writer) error {
fmt.Fprintf(tw, " Service URI:\t%s\n", b.ServiceURI)
fmt.Fprintf(tw, " Proxy URI:\t%s\n", b.ProxyURI)
}
case *types.VirtualPrecisionClock:
if b, ok := md.Backing.(*types.VirtualPrecisionClockSystemClockBackingInfo); ok {
proto := b.Protocol
if proto == "" {
proto = string(types.HostDateTimeInfoProtocolPtp)
}
fmt.Fprintf(tw, " Protocol:\t%s\n", proto)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions govc/main.go
Expand Up @@ -34,6 +34,7 @@ import (
_ "github.com/vmware/govmomi/govc/datastore/vsan"
_ "github.com/vmware/govmomi/govc/device"
_ "github.com/vmware/govmomi/govc/device/cdrom"
_ "github.com/vmware/govmomi/govc/device/clock"
_ "github.com/vmware/govmomi/govc/device/floppy"
_ "github.com/vmware/govmomi/govc/device/pci"
_ "github.com/vmware/govmomi/govc/device/scsi"
Expand Down
16 changes: 16 additions & 0 deletions govc/test/device.bats
Expand Up @@ -279,6 +279,22 @@ load test_helper
[ $result -eq 1 ]
}

@test "device.clock" {
vcsim_env

vm=$(new_empty_vm)

result=$(govc device.ls -vm "$vm" | grep clock | wc -l)
[ "$result" -eq 0 ]

run govc device.clock.add -vm "$vm"
assert_success
id=$output

result=$(govc device.ls -vm "$vm" | grep "$id" | wc -l)
[ "$result" -eq 1 ]
}

@test "device.scsi slots" {
vcsim_env

Expand Down
2 changes: 2 additions & 0 deletions object/virtual_device_list.go
Expand Up @@ -894,6 +894,8 @@ func (l VirtualDeviceList) Type(device types.BaseVirtualDevice) string {
return "lsilogic-sas"
case *types.VirtualNVMEController:
return "nvme"
case *types.VirtualPrecisionClock:
return "clock"
default:
return l.deviceName(device)
}
Expand Down

0 comments on commit fc17df0

Please sign in to comment.