github.com/vmware/govmomi@v0.43.0/govc/dvs/portgroup/spec.go (about) 1 /* 2 Copyright (c) 2016-2024 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 := types.DistributedVirtualPortgroupPortgroupType("").Strings() 50 51 vlanModes := []string{ 52 string(VLAN), 53 string(TrunkVLAN), 54 } 55 56 f.StringVar(&spec.Type, "type", ptypes[0], 57 fmt.Sprintf("Portgroup type (%s)", strings.Join(ptypes, "|"))) 58 f.Var(flags.NewInt32(&spec.NumPorts), "nports", "Number of ports") 59 f.Var(flags.NewInt32(&vlanId), "vlan", "VLAN ID") 60 f.StringVar(&vlanRange, "vlan-range", "0-4094", "VLAN Ranges with comma delimited") 61 f.StringVar(&vlanMode, "vlan-mode", "vlan", fmt.Sprintf("vlan mode (%s)", strings.Join(vlanModes, "|"))) 62 f.Var(flags.NewOptionalBool(&spec.AutoExpand), "auto-expand", "Ignore the limit on the number of ports") 63 } 64 65 func getRange(vlanRange string) []types.NumericRange { 66 nRanges := make([]types.NumericRange, 0) 67 strRanges := strings.Split(vlanRange, ",") 68 for _, v := range strRanges { 69 vlans := strings.Split(v, "-") 70 if len(vlans) != 2 { 71 panic(fmt.Sprintf("range %s does not follow format with vlanId-vlanId", v)) 72 } 73 start, err := strconv.Atoi(vlans[0]) 74 if err != nil { 75 panic(fmt.Sprintf("range %s does not follow format with vlanId-vlanId error: %v", v, err)) 76 } 77 end, err := strconv.Atoi(vlans[1]) 78 if err != nil { 79 panic(fmt.Sprintf("range %s does not follow format with vlanId-vlanId error: %v", v, err)) 80 } 81 nRanges = append(nRanges, types.NumericRange{ 82 Start: int32(start), 83 End: int32(end), 84 }) 85 } 86 return nRanges 87 } 88 89 func (spec *DVPortgroupConfigSpec) Spec() types.DVPortgroupConfigSpec { 90 config := new(types.VMwareDVSPortSetting) 91 switch VLANMode(vlanMode) { 92 case VLAN: 93 spec := new(types.VmwareDistributedVirtualSwitchVlanIdSpec) 94 spec.VlanId = vlanId 95 vlanSpec = spec 96 case TrunkVLAN: 97 spec := new(types.VmwareDistributedVirtualSwitchTrunkVlanSpec) 98 spec.VlanId = getRange(vlanRange) 99 vlanSpec = spec 100 } 101 config.Vlan = vlanSpec 102 spec.DefaultPortConfig = config 103 return spec.DVPortgroupConfigSpec 104 }