github.com/vmware/govmomi@v0.51.0/cli/flags/resource_allocation_info.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package flags
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"strconv"
    11  	"strings"
    12  
    13  	"github.com/vmware/govmomi/vim25/types"
    14  )
    15  
    16  type sharesInfo types.SharesInfo
    17  
    18  func (s *sharesInfo) String() string {
    19  	return string(s.Level)
    20  }
    21  
    22  func (s *sharesInfo) Set(val string) error {
    23  	switch val {
    24  	case string(types.SharesLevelNormal), string(types.SharesLevelLow), string(types.SharesLevelHigh):
    25  		s.Level = types.SharesLevel(val)
    26  	default:
    27  		n, err := strconv.Atoi(val)
    28  		if err != nil {
    29  			return err
    30  		}
    31  
    32  		s.Level = types.SharesLevelCustom
    33  		s.Shares = int32(n)
    34  	}
    35  
    36  	return nil
    37  }
    38  
    39  type ResourceAllocationFlag struct {
    40  	cpu, mem              *types.ResourceAllocationInfo
    41  	ExpandableReservation bool
    42  }
    43  
    44  func NewResourceAllocationFlag(cpu, mem *types.ResourceAllocationInfo) *ResourceAllocationFlag {
    45  	return &ResourceAllocationFlag{cpu, mem, true}
    46  }
    47  
    48  func (r *ResourceAllocationFlag) Register(ctx context.Context, f *flag.FlagSet) {
    49  	opts := []struct {
    50  		name  string
    51  		units string
    52  		*types.ResourceAllocationInfo
    53  	}{
    54  		{"CPU", "MHz", r.cpu},
    55  		{"Memory", "MB", r.mem},
    56  	}
    57  
    58  	for _, opt := range opts {
    59  		prefix := strings.ToLower(opt.name)[:3]
    60  		shares := (*sharesInfo)(opt.Shares)
    61  
    62  		f.Var(NewOptionalInt64(&opt.Limit), prefix+".limit", opt.name+" limit in "+opt.units)
    63  		f.Var(NewOptionalInt64(&opt.Reservation), prefix+".reservation", opt.name+" reservation in "+opt.units)
    64  		if r.ExpandableReservation {
    65  			f.Var(NewOptionalBool(&opt.ExpandableReservation), prefix+".expandable", opt.name+" expandable reservation")
    66  		}
    67  		f.Var(shares, prefix+".shares", opt.name+" shares level or number")
    68  	}
    69  }
    70  
    71  func (s *ResourceAllocationFlag) Process(ctx context.Context) error {
    72  	return nil
    73  }