github.com/vmware/govmomi@v0.37.2/govc/dvs/portgroup/spec.go (about)

     1  /*
     2  Copyright (c) 2016 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 portgroup
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"strconv"
    24  	"strings"
    25  
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type VLANMode string
    31  
    32  const (
    33  	VLAN      = VLANMode("vlan")
    34  	TrunkVLAN = VLANMode("trunking")
    35  )
    36  
    37  type DVPortgroupConfigSpec struct {
    38  	types.DVPortgroupConfigSpec
    39  }
    40  
    41  var (
    42  	vlanMode  string
    43  	vlanId    int32
    44  	vlanRange string
    45  	vlanSpec  types.BaseVmwareDistributedVirtualSwitchVlanSpec
    46  )
    47  
    48  func (spec *DVPortgroupConfigSpec) Register(ctx context.Context, f *flag.FlagSet) {
    49  	ptypes := []string{
    50  		string(types.DistributedVirtualPortgroupPortgroupTypeEarlyBinding),
    51  		string(types.DistributedVirtualPortgroupPortgroupTypeLateBinding),
    52  		string(types.DistributedVirtualPortgroupPortgroupTypeEphemeral),
    53  	}
    54  
    55  	vlanModes := []string{
    56  		string(VLAN),
    57  		string(TrunkVLAN),
    58  	}
    59  
    60  	f.StringVar(&spec.Type, "type", ptypes[0],
    61  		fmt.Sprintf("Portgroup type (%s)", strings.Join(ptypes, "|")))
    62  	f.Var(flags.NewInt32(&spec.NumPorts), "nports", "Number of ports")
    63  	f.Var(flags.NewInt32(&vlanId), "vlan", "VLAN ID")
    64  	f.StringVar(&vlanRange, "vlan-range", "0-4094", "VLAN Ranges with comma delimited")
    65  	f.StringVar(&vlanMode, "vlan-mode", "vlan", fmt.Sprintf("vlan mode (%s)", strings.Join(vlanModes, "|")))
    66  	f.Var(flags.NewOptionalBool(&spec.AutoExpand), "auto-expand", "Ignore the limit on the number of ports")
    67  }
    68  
    69  func getRange(vlanRange string) []types.NumericRange {
    70  	nRanges := make([]types.NumericRange, 0)
    71  	strRanges := strings.Split(vlanRange, ",")
    72  	for _, v := range strRanges {
    73  		vlans := strings.Split(v, "-")
    74  		if len(vlans) != 2 {
    75  			panic(fmt.Sprintf("range %s does not follow format with vlanId-vlanId", v))
    76  		}
    77  		start, err := strconv.Atoi(vlans[0])
    78  		if err != nil {
    79  			panic(fmt.Sprintf("range %s does not follow format with vlanId-vlanId error: %v", v, err))
    80  		}
    81  		end, err := strconv.Atoi(vlans[1])
    82  		if err != nil {
    83  			panic(fmt.Sprintf("range %s does not follow format with vlanId-vlanId error: %v", v, err))
    84  		}
    85  		nRanges = append(nRanges, types.NumericRange{
    86  			Start: int32(start),
    87  			End:   int32(end),
    88  		})
    89  	}
    90  	return nRanges
    91  }
    92  
    93  func (spec *DVPortgroupConfigSpec) Spec() types.DVPortgroupConfigSpec {
    94  	config := new(types.VMwareDVSPortSetting)
    95  	switch VLANMode(vlanMode) {
    96  	case VLAN:
    97  		spec := new(types.VmwareDistributedVirtualSwitchVlanIdSpec)
    98  		spec.VlanId = vlanId
    99  		vlanSpec = spec
   100  	case TrunkVLAN:
   101  		spec := new(types.VmwareDistributedVirtualSwitchTrunkVlanSpec)
   102  		spec.VlanId = getRange(vlanRange)
   103  		vlanSpec = spec
   104  	}
   105  	config.Vlan = vlanSpec
   106  	spec.DefaultPortConfig = config
   107  	return spec.DVPortgroupConfigSpec
   108  }