github.com/vmware/govmomi@v0.51.0/cli/host/vnic/service.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 vnic
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"strings"
    12  
    13  	"github.com/vmware/govmomi/cli"
    14  	"github.com/vmware/govmomi/cli/flags"
    15  	"github.com/vmware/govmomi/vim25/types"
    16  )
    17  
    18  type service struct {
    19  	*flags.HostSystemFlag
    20  
    21  	Enable bool
    22  }
    23  
    24  func init() {
    25  	cli.Register("host.vnic.service", &service{})
    26  }
    27  
    28  func (cmd *service) Register(ctx context.Context, f *flag.FlagSet) {
    29  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    30  	cmd.HostSystemFlag.Register(ctx, f)
    31  
    32  	f.BoolVar(&cmd.Enable, "enable", true, "Enable service")
    33  }
    34  
    35  func (cmd *service) Process(ctx context.Context) error {
    36  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    37  		return err
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  func (cmd *service) Usage() string {
    44  	return "SERVICE DEVICE"
    45  }
    46  
    47  func (cmd *service) Description() string {
    48  	nicTypes := types.HostVirtualNicManagerNicType("").Strings()
    49  
    50  	return fmt.Sprintf(`
    51  Enable or disable service on a virtual nic device.
    52  
    53  Where SERVICE is one of: %s
    54  Where DEVICE is one of: %s
    55  
    56  Examples:
    57    govc host.vnic.service -host hostname -enable vsan vmk0
    58    govc host.vnic.service -host hostname -enable=false vmotion vmk1`,
    59  		strings.Join(nicTypes, "|"),
    60  		strings.Join([]string{"vmk0", "vmk1", "..."}, "|"))
    61  }
    62  
    63  func (cmd *service) Run(ctx context.Context, f *flag.FlagSet) error {
    64  	if f.NArg() != 2 {
    65  		return flag.ErrHelp
    66  	}
    67  
    68  	service := f.Arg(0)
    69  	device := f.Arg(1)
    70  
    71  	host, err := cmd.HostSystem()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	m, err := host.ConfigManager().VirtualNicManager(ctx)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	var method func(context.Context, string, string) error
    82  
    83  	if cmd.Enable {
    84  		method = m.SelectVnic
    85  	} else {
    86  		method = m.DeselectVnic
    87  	}
    88  
    89  	if method == nil {
    90  		return flag.ErrHelp
    91  	}
    92  
    93  	return method(ctx, service, device)
    94  }