github.com/vmware/govmomi@v0.51.0/cli/dvs/portgroup/add.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 portgroup
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/object"
    15  	"github.com/vmware/govmomi/vim25/types"
    16  )
    17  
    18  type add struct {
    19  	*flags.DatacenterFlag
    20  
    21  	DVPortgroupConfigSpec
    22  
    23  	path string
    24  }
    25  
    26  func init() {
    27  	cli.Register("dvs.portgroup.add", &add{})
    28  }
    29  
    30  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    31  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    32  	cmd.DatacenterFlag.Register(ctx, f)
    33  
    34  	f.StringVar(&cmd.path, "dvs", "", "DVS path")
    35  
    36  	cmd.DVPortgroupConfigSpec.NumPorts = 128 // default
    37  
    38  	cmd.DVPortgroupConfigSpec.Register(ctx, f)
    39  }
    40  
    41  func (cmd *add) Description() string {
    42  	return `Add portgroup to DVS.
    43  
    44  The '-type' options are defined by the dvs.DistributedVirtualPortgroup.PortgroupType API.
    45  The UI labels '-type' as "Port binding" with the following choices:
    46      "Static binding":  earlyBinding
    47      "Dynanic binding": lateBinding
    48      "No binding":      ephemeral
    49  
    50  The '-auto-expand' option is labeled in the UI as "Port allocation".
    51  The default value is false, behaves as the UI labeled "Fixed" choice.
    52  When given '-auto-expand=true', behaves as the UI labeled "Elastic" choice.
    53  
    54  Examples:
    55    govc dvs.create DSwitch
    56    govc dvs.portgroup.add -dvs DSwitch -type earlyBinding -nports 16 ExternalNetwork
    57    govc dvs.portgroup.add -dvs DSwitch -type ephemeral InternalNetwork
    58    govc object.destroy network/InternalNetwork # remove the portgroup`
    59  }
    60  
    61  func (cmd *add) Process(ctx context.Context) error {
    62  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    63  		return err
    64  	}
    65  	return nil
    66  }
    67  
    68  func (cmd *add) Usage() string {
    69  	return "NAME"
    70  }
    71  
    72  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    73  	if f.NArg() == 0 {
    74  		return flag.ErrHelp
    75  	}
    76  
    77  	name := f.Arg(0)
    78  
    79  	finder, err := cmd.Finder()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	net, err := finder.Network(ctx, cmd.path)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	dvs, ok := net.(*object.DistributedVirtualSwitch)
    90  	if !ok {
    91  		return fmt.Errorf("%s (%T) is not of type %T", cmd.path, net, dvs)
    92  	}
    93  
    94  	cmd.DVPortgroupConfigSpec.Name = name
    95  
    96  	task, err := dvs.AddPortgroup(ctx, []types.DVPortgroupConfigSpec{cmd.Spec()})
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	logger := cmd.ProgressLogger(fmt.Sprintf("adding %s portgroup to dvs %s... ", name, dvs.InventoryPath))
   102  	defer logger.Wait()
   103  
   104  	_, err = task.WaitForResult(ctx, logger)
   105  	return err
   106  }