github.com/vmware/govmomi@v0.37.2/govc/dvs/portgroup/change.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/mo"
    28  )
    29  
    30  type change struct {
    31  	*flags.DatacenterFlag
    32  
    33  	DVPortgroupConfigSpec
    34  }
    35  
    36  func init() {
    37  	cli.Register("dvs.portgroup.change", &change{})
    38  }
    39  
    40  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    42  	cmd.DatacenterFlag.Register(ctx, f)
    43  
    44  	cmd.DVPortgroupConfigSpec.Register(ctx, f)
    45  }
    46  
    47  func (cmd *change) Description() string {
    48  	return `Change DVS portgroup configuration.
    49  
    50  Examples:
    51    govc dvs.portgroup.change -nports 26 ExternalNetwork
    52    govc dvs.portgroup.change -vlan 3214 ExternalNetwork`
    53  }
    54  
    55  func (cmd *change) Process(ctx context.Context) error {
    56  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    57  		return err
    58  	}
    59  	return nil
    60  }
    61  
    62  func (cmd *change) Usage() string {
    63  	return "PATH"
    64  }
    65  
    66  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    67  	if f.NArg() != 1 {
    68  		return flag.ErrHelp
    69  	}
    70  
    71  	path := f.Arg(0)
    72  
    73  	finder, err := cmd.Finder()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	net, err := finder.Network(ctx, path)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	pg, ok := net.(*object.DistributedVirtualPortgroup)
    84  	if !ok {
    85  		return fmt.Errorf("%s (%T) is not of type %T", path, net, pg)
    86  	}
    87  
    88  	var s mo.DistributedVirtualPortgroup
    89  	err = pg.Properties(ctx, pg.Reference(), []string{"config.configVersion"}, &s)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	spec := cmd.Spec()
    95  	spec.ConfigVersion = s.Config.ConfigVersion
    96  
    97  	task, err := pg.Reconfigure(ctx, spec)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	logger := cmd.ProgressLogger(fmt.Sprintf("changing %s portgroup configuration %s... ", pg.Name(), pg.InventoryPath))
   103  	defer logger.Wait()
   104  
   105  	_, err = task.WaitForResult(ctx, logger)
   106  	return err
   107  }