github.com/vmware/govmomi@v0.43.0/govc/host/vnic/service.go (about)

     1  /*
     2  Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vnic
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type service struct {
    31  	*flags.HostSystemFlag
    32  
    33  	Enable bool
    34  }
    35  
    36  func init() {
    37  	cli.Register("host.vnic.service", &service{})
    38  }
    39  
    40  func (cmd *service) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    42  	cmd.HostSystemFlag.Register(ctx, f)
    43  
    44  	f.BoolVar(&cmd.Enable, "enable", true, "Enable service")
    45  }
    46  
    47  func (cmd *service) Process(ctx context.Context) error {
    48  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    49  		return err
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func (cmd *service) Usage() string {
    56  	return "SERVICE DEVICE"
    57  }
    58  
    59  func (cmd *service) Description() string {
    60  	nicTypes := types.HostVirtualNicManagerNicType("").Strings()
    61  
    62  	return fmt.Sprintf(`
    63  Enable or disable service on a virtual nic device.
    64  
    65  Where SERVICE is one of: %s
    66  Where DEVICE is one of: %s
    67  
    68  Examples:
    69    govc host.vnic.service -host hostname -enable vsan vmk0
    70    govc host.vnic.service -host hostname -enable=false vmotion vmk1`,
    71  		strings.Join(nicTypes, "|"),
    72  		strings.Join([]string{"vmk0", "vmk1", "..."}, "|"))
    73  }
    74  
    75  func (cmd *service) Run(ctx context.Context, f *flag.FlagSet) error {
    76  	if f.NArg() != 2 {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	service := f.Arg(0)
    81  	device := f.Arg(1)
    82  
    83  	host, err := cmd.HostSystem()
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	m, err := host.ConfigManager().VirtualNicManager(ctx)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	var method func(context.Context, string, string) error
    94  
    95  	if cmd.Enable {
    96  		method = m.SelectVnic
    97  	} else {
    98  		method = m.DeselectVnic
    99  	}
   100  
   101  	if method == nil {
   102  		return flag.ErrHelp
   103  	}
   104  
   105  	return method(ctx, service, device)
   106  }