github.com/vmware/govmomi@v0.43.0/govc/host/portgroup/change.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  
    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 change struct {
    29  	*flags.ClientFlag
    30  	*flags.HostSystemFlag
    31  
    32  	types.HostPortGroupSpec
    33  	types.HostNetworkSecurityPolicy
    34  }
    35  
    36  func init() {
    37  	cli.Register("host.portgroup.change", &change{})
    38  }
    39  
    40  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    42  	cmd.ClientFlag.Register(ctx, f)
    43  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    44  	cmd.HostSystemFlag.Register(ctx, f)
    45  
    46  	cmd.VlanId = -1
    47  	f.Var(flags.NewInt32(&cmd.VlanId), "vlan-id", "VLAN ID")
    48  	f.StringVar(&cmd.Name, "name", "", "Portgroup name")
    49  	f.StringVar(&cmd.VswitchName, "vswitch-name", "", "vSwitch name")
    50  
    51  	f.Var(flags.NewOptionalBool(&cmd.AllowPromiscuous), "allow-promiscuous", "Allow promiscuous mode")
    52  	f.Var(flags.NewOptionalBool(&cmd.ForgedTransmits), "forged-transmits", "Allow forged transmits")
    53  	f.Var(flags.NewOptionalBool(&cmd.MacChanges), "mac-changes", "Allow MAC changes")
    54  }
    55  
    56  func (cmd *change) Description() string {
    57  	return `Change configuration of HOST portgroup NAME.
    58  
    59  Examples:
    60    govc host.portgroup.change -allow-promiscuous -forged-transmits -mac-changes "VM Network"
    61    govc host.portgroup.change -vswitch-name vSwitch1 "Management Network"`
    62  }
    63  
    64  func (cmd *change) Usage() string {
    65  	return "NAME"
    66  }
    67  
    68  func (cmd *change) Process(ctx context.Context) error {
    69  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    70  		return err
    71  	}
    72  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    73  		return err
    74  	}
    75  	return nil
    76  }
    77  
    78  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    79  	if f.NArg() != 1 {
    80  		return flag.ErrHelp
    81  	}
    82  
    83  	pg, err := networkInfoPortgroup(ctx, cmd.ClientFlag, cmd.HostSystemFlag)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	ns, err := cmd.HostNetworkSystem()
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	name := f.Arg(0)
    94  	var current *types.HostPortGroupSpec
    95  
    96  	for _, g := range pg {
    97  		if g.Spec.Name == name {
    98  			current = &g.Spec
    99  			break
   100  		}
   101  	}
   102  
   103  	if current != nil {
   104  		if cmd.Name == "" {
   105  			cmd.Name = current.Name
   106  		}
   107  		if cmd.VswitchName == "" {
   108  			cmd.VswitchName = current.VswitchName
   109  		}
   110  		if cmd.VlanId < 0 {
   111  			cmd.VlanId = current.VlanId
   112  		}
   113  	}
   114  
   115  	cmd.HostPortGroupSpec.Policy.Security = &cmd.HostNetworkSecurityPolicy
   116  
   117  	return ns.UpdatePortGroup(ctx, name, cmd.HostPortGroupSpec)
   118  }