github.com/vmware/govmomi@v0.43.0/govc/host/vswitch/add.go (about)

     1  /*
     2  Copyright (c) 2014-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 vswitch
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type add struct {
    29  	*flags.HostSystemFlag
    30  
    31  	nic  string
    32  	spec types.HostVirtualSwitchSpec
    33  }
    34  
    35  func init() {
    36  	cli.Register("host.vswitch.add", &add{})
    37  }
    38  
    39  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    41  	cmd.HostSystemFlag.Register(ctx, f)
    42  
    43  	cmd.spec.NumPorts = 128 // default
    44  	f.Var(flags.NewInt32(&cmd.spec.NumPorts), "ports", "Number of ports")
    45  	f.Var(flags.NewInt32(&cmd.spec.Mtu), "mtu", "MTU")
    46  	f.StringVar(&cmd.nic, "nic", "", "Bridge nic device")
    47  }
    48  
    49  func (cmd *add) Process(ctx context.Context) error {
    50  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }
    55  
    56  func (cmd *add) Usage() string {
    57  	return "NAME"
    58  }
    59  
    60  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    61  	ns, err := cmd.HostNetworkSystem()
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if cmd.nic != "" {
    67  		cmd.spec.Bridge = &types.HostVirtualSwitchBondBridge{
    68  			NicDevice: []string{cmd.nic},
    69  		}
    70  	}
    71  
    72  	return ns.AddVirtualSwitch(ctx, f.Arg(0), &cmd.spec)
    73  }