github.com/vmware/govmomi@v0.51.0/cli/device/clock/add.go (about)

     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 floppy
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/vim25/types"
    15  )
    16  
    17  type add struct {
    18  	*flags.VirtualMachineFlag
    19  }
    20  
    21  func init() {
    22  	cli.Register("device.clock.add", &add{})
    23  }
    24  
    25  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    26  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    27  	cmd.VirtualMachineFlag.Register(ctx, f)
    28  }
    29  
    30  func (cmd *add) Description() string {
    31  	return `Add precision clock device to VM.
    32  
    33  Examples:
    34    govc device.clock.add -vm $vm
    35    govc device.info clock-*`
    36  }
    37  
    38  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    39  	vm, err := cmd.VirtualMachine()
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	if vm == nil {
    45  		return flag.ErrHelp
    46  	}
    47  
    48  	d := &types.VirtualPrecisionClock{
    49  		VirtualDevice: types.VirtualDevice{
    50  			Backing: &types.VirtualPrecisionClockSystemClockBackingInfo{
    51  				Protocol: string(types.HostDateTimeInfoProtocolPtp), // TODO: ntp option
    52  			},
    53  		},
    54  	}
    55  
    56  	err = vm.AddDevice(ctx, d)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// output name of device we just created
    62  	devices, err := vm.Device(ctx)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	devices = devices.SelectByType(d)
    68  
    69  	name := devices.Name(devices[len(devices)-1])
    70  
    71  	fmt.Println(name)
    72  
    73  	return nil
    74  }