github.com/vmware/govmomi@v0.37.1/govc/flags/resource_allocation_info.go (about)

     1  /*
     2  Copyright (c) 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 flags
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"strconv"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type sharesInfo types.SharesInfo
    29  
    30  func (s *sharesInfo) String() string {
    31  	return string(s.Level)
    32  }
    33  
    34  func (s *sharesInfo) Set(val string) error {
    35  	switch val {
    36  	case string(types.SharesLevelNormal), string(types.SharesLevelLow), string(types.SharesLevelHigh):
    37  		s.Level = types.SharesLevel(val)
    38  	default:
    39  		n, err := strconv.Atoi(val)
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		s.Level = types.SharesLevelCustom
    45  		s.Shares = int32(n)
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  type ResourceAllocationFlag struct {
    52  	cpu, mem              *types.ResourceAllocationInfo
    53  	ExpandableReservation bool
    54  }
    55  
    56  func NewResourceAllocationFlag(cpu, mem *types.ResourceAllocationInfo) *ResourceAllocationFlag {
    57  	return &ResourceAllocationFlag{cpu, mem, true}
    58  }
    59  
    60  func (r *ResourceAllocationFlag) Register(ctx context.Context, f *flag.FlagSet) {
    61  	opts := []struct {
    62  		name  string
    63  		units string
    64  		*types.ResourceAllocationInfo
    65  	}{
    66  		{"CPU", "MHz", r.cpu},
    67  		{"Memory", "MB", r.mem},
    68  	}
    69  
    70  	for _, opt := range opts {
    71  		prefix := strings.ToLower(opt.name)[:3]
    72  		shares := (*sharesInfo)(opt.Shares)
    73  
    74  		f.Var(NewOptionalInt64(&opt.Limit), prefix+".limit", opt.name+" limit in "+opt.units)
    75  		f.Var(NewOptionalInt64(&opt.Reservation), prefix+".reservation", opt.name+" reservation in "+opt.units)
    76  		if r.ExpandableReservation {
    77  			f.Var(NewOptionalBool(&opt.ExpandableReservation), prefix+".expandable", opt.name+" expandable reservation")
    78  		}
    79  		f.Var(shares, prefix+".shares", opt.name+" shares level or number")
    80  	}
    81  }
    82  
    83  func (s *ResourceAllocationFlag) Process(ctx context.Context) error {
    84  	return nil
    85  }