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