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

     1  /*
     2  Copyright (c) 2015 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 := []string{
    61  		string(types.HostVirtualNicManagerNicTypeVmotion),
    62  		string(types.HostVirtualNicManagerNicTypeFaultToleranceLogging),
    63  		string(types.HostVirtualNicManagerNicTypeVSphereReplication),
    64  		string(types.HostVirtualNicManagerNicTypeVSphereReplicationNFC),
    65  		string(types.HostVirtualNicManagerNicTypeManagement),
    66  		string(types.HostVirtualNicManagerNicTypeVsan),
    67  		string(types.HostVirtualNicManagerNicTypeVSphereProvisioning),
    68  	}
    69  
    70  	return fmt.Sprintf(`
    71  Enable or disable service on a virtual nic device.
    72  
    73  Where SERVICE is one of: %s
    74  Where DEVICE is one of: %s
    75  
    76  Examples:
    77    govc host.vnic.service -host hostname -enable vsan vmk0
    78    govc host.vnic.service -host hostname -enable=false vmotion vmk1`,
    79  		strings.Join(nicTypes, "|"),
    80  		strings.Join([]string{"vmk0", "vmk1", "..."}, "|"))
    81  }
    82  
    83  func (cmd *service) Run(ctx context.Context, f *flag.FlagSet) error {
    84  	if f.NArg() != 2 {
    85  		return flag.ErrHelp
    86  	}
    87  
    88  	service := f.Arg(0)
    89  	device := f.Arg(1)
    90  
    91  	host, err := cmd.HostSystem()
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	m, err := host.ConfigManager().VirtualNicManager(ctx)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	var method func(context.Context, string, string) error
   102  
   103  	if cmd.Enable {
   104  		method = m.SelectVnic
   105  	} else {
   106  		method = m.DeselectVnic
   107  	}
   108  
   109  	if method == nil {
   110  		return flag.ErrHelp
   111  	}
   112  
   113  	return method(ctx, service, device)
   114  }