github.com/vmware/govmomi@v0.43.0/govc/host/vnic/change.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  	"errors"
    22  	"flag"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/vim25/mo"
    27  )
    28  
    29  type change struct {
    30  	*flags.HostSystemFlag
    31  
    32  	mtu int32
    33  }
    34  
    35  func init() {
    36  	cli.Register("host.vnic.change", &change{})
    37  }
    38  
    39  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    41  	cmd.HostSystemFlag.Register(ctx, f)
    42  
    43  	f.Var(flags.NewInt32(&cmd.mtu), "mtu", "vmk MTU")
    44  }
    45  
    46  func (cmd *change) Process(ctx context.Context) error {
    47  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    48  		return err
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func (cmd *change) Usage() string {
    55  	return "DEVICE"
    56  }
    57  
    58  func (cmd *change) Description() string {
    59  	return `Change a virtual nic DEVICE.
    60  
    61  Examples:
    62    govc host.vnic.change -host hostname -mtu 9000 vmk1`
    63  }
    64  
    65  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	if f.NArg() != 1 {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	device := f.Arg(0)
    71  
    72  	ns, err := cmd.HostNetworkSystem()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	var mns mo.HostNetworkSystem
    78  
    79  	err = ns.Properties(ctx, ns.Reference(), []string{"networkInfo"}, &mns)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	for _, nic := range mns.NetworkInfo.Vnic {
    85  		if nic.Device == device {
    86  			nic.Spec.Mtu = cmd.mtu
    87  			return ns.UpdateVirtualNic(ctx, device, nic.Spec)
    88  		}
    89  	}
    90  
    91  	return errors.New(device + " not found")
    92  }