github.com/vmware/govmomi@v0.43.0/govc/pool/change.go (about)

     1  /*
     2  Copyright (c) 2015-2017 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 pool
    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.DatacenterFlag
    30  	*ResourceConfigSpecFlag
    31  
    32  	name string
    33  }
    34  
    35  func init() {
    36  	cli.Register("pool.change", &change{})
    37  }
    38  
    39  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    41  	cmd.DatacenterFlag.Register(ctx, f)
    42  	cmd.ResourceConfigSpecFlag = &ResourceConfigSpecFlag{
    43  		ResourceConfigSpec: types.ResourceConfigSpec{
    44  			CpuAllocation: types.ResourceAllocationInfo{
    45  				Shares: new(types.SharesInfo),
    46  			},
    47  			MemoryAllocation: types.ResourceAllocationInfo{
    48  				Shares: new(types.SharesInfo),
    49  			},
    50  		},
    51  	}
    52  	cmd.ResourceConfigSpecFlag.Register(ctx, f)
    53  
    54  	f.StringVar(&cmd.name, "name", "", "Resource pool name")
    55  }
    56  
    57  func (cmd *change) Process(ctx context.Context) error {
    58  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    59  		return err
    60  	}
    61  	if err := cmd.ResourceConfigSpecFlag.Process(ctx); err != nil {
    62  		return err
    63  	}
    64  	return nil
    65  }
    66  
    67  func (cmd *change) Usage() string {
    68  	return "POOL..."
    69  }
    70  
    71  func (cmd *change) Description() string {
    72  	return "Change the configuration of one or more resource POOLs.\n" + poolNameHelp
    73  }
    74  
    75  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    76  	if f.NArg() == 0 {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	finder, err := cmd.Finder()
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	for _, ra := range []*types.ResourceAllocationInfo{&cmd.CpuAllocation, &cmd.MemoryAllocation} {
    86  		if ra.Shares.Level == "" {
    87  			ra.Shares = nil
    88  		}
    89  	}
    90  
    91  	for _, arg := range f.Args() {
    92  		pools, err := finder.ResourcePoolListAll(ctx, arg)
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		for _, pool := range pools {
    98  			err := pool.UpdateConfig(ctx, cmd.name, &cmd.ResourceConfigSpec)
    99  			if err != nil {
   100  				return err
   101  			}
   102  		}
   103  	}
   104  
   105  	return nil
   106  }