github.com/vmware/govmomi@v0.51.0/vim25/types/hardware_version.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 types
     6  
     7  import (
     8  	"fmt"
     9  	"regexp"
    10  	"strconv"
    11  )
    12  
    13  // HardwareVersion is a VMX hardware version.
    14  type HardwareVersion uint8
    15  
    16  const (
    17  	invalidHardwareVersion HardwareVersion = 0
    18  )
    19  
    20  const (
    21  	VMX3 HardwareVersion = iota + 3
    22  	VMX4
    23  
    24  	vmx5 // invalid
    25  
    26  	VMX6
    27  	VMX7
    28  	VMX8
    29  	VMX9
    30  	VMX10
    31  	VMX11
    32  	VMX12
    33  	VMX13
    34  	VMX14
    35  	VMX15
    36  	VMX16
    37  	VMX17
    38  	VMX18
    39  	VMX19
    40  	VMX20
    41  	VMX21
    42  )
    43  
    44  const (
    45  	// MinValidHardwareVersion is the minimum, valid hardware version supported
    46  	// by VMware hypervisors in the wild.
    47  	MinValidHardwareVersion = VMX3
    48  
    49  	// MaxValidHardwareVersion is the maximum, valid hardware version supported
    50  	// by VMware hypervisors in the wild.
    51  	MaxValidHardwareVersion = VMX21
    52  )
    53  
    54  // IsSupported returns true if the hardware version is known to and supported by
    55  // GoVmomi's generated types.
    56  func (hv HardwareVersion) IsSupported() bool {
    57  	return hv.IsValid() &&
    58  		hv != vmx5 &&
    59  		hv >= MinValidHardwareVersion &&
    60  		hv <= MaxValidHardwareVersion
    61  }
    62  
    63  // IsValid returns true if the hardware version is not valid.
    64  // Unlike IsSupported, this function returns true as long as the hardware
    65  // version is greater than 0.
    66  // For example, the result of parsing "abc" or "vmx-abc" is an invalid hardware
    67  // version, whereas the result of parsing "vmx-99" is valid, just not supported.
    68  func (hv HardwareVersion) IsValid() bool {
    69  	return hv != invalidHardwareVersion
    70  }
    71  
    72  func (hv HardwareVersion) String() string {
    73  	if hv.IsValid() {
    74  		return fmt.Sprintf("vmx-%d", hv)
    75  	}
    76  	return ""
    77  }
    78  
    79  func (hv HardwareVersion) MarshalText() ([]byte, error) {
    80  	return []byte(hv.String()), nil
    81  }
    82  
    83  func (hv *HardwareVersion) UnmarshalText(text []byte) error {
    84  	v, err := ParseHardwareVersion(string(text))
    85  	if err != nil {
    86  		return err
    87  	}
    88  	*hv = v
    89  	return nil
    90  }
    91  
    92  var (
    93  	vmxRe        = regexp.MustCompile(`(?i)^vmx-(\d+)$`)
    94  	vmxNumOnlyRe = regexp.MustCompile(`^(\d+)$`)
    95  )
    96  
    97  // MustParseHardwareVersion parses the provided string into a hardware version.
    98  func MustParseHardwareVersion(s string) HardwareVersion {
    99  	v, err := ParseHardwareVersion(s)
   100  	if err != nil {
   101  		panic(err)
   102  	}
   103  	return v
   104  }
   105  
   106  // ParseHardwareVersion parses the provided string into a hardware version.
   107  // Supported formats include vmx-123 or 123. Please note that the parser will
   108  // only return an error if the supplied version does not match the supported
   109  // formats.
   110  // Once parsed, use the function IsSupported to determine if the hardware
   111  // version falls into the range of versions known to GoVmomi.
   112  func ParseHardwareVersion(s string) (HardwareVersion, error) {
   113  	if m := vmxRe.FindStringSubmatch(s); len(m) > 0 {
   114  		u, err := strconv.ParseUint(m[1], 10, 8)
   115  		if err != nil {
   116  			return invalidHardwareVersion, fmt.Errorf(
   117  				"failed to parse %s from %q as uint8: %w", m[1], s, err)
   118  		}
   119  		return HardwareVersion(u), nil
   120  	} else if m := vmxNumOnlyRe.FindStringSubmatch(s); len(m) > 0 {
   121  		u, err := strconv.ParseUint(m[1], 10, 8)
   122  		if err != nil {
   123  			return invalidHardwareVersion, fmt.Errorf(
   124  				"failed to parse %s as uint8: %w", m[1], err)
   125  		}
   126  		return HardwareVersion(u), nil
   127  	}
   128  	return invalidHardwareVersion, fmt.Errorf("invalid version: %q", s)
   129  }
   130  
   131  var hardwareVersions []HardwareVersion
   132  
   133  func init() {
   134  	for i := MinValidHardwareVersion; i <= MaxValidHardwareVersion; i++ {
   135  		if i.IsSupported() {
   136  			hardwareVersions = append(hardwareVersions, i)
   137  		}
   138  	}
   139  }
   140  
   141  // GetHardwareVersions returns a list of hardware versions.
   142  func GetHardwareVersions() []HardwareVersion {
   143  	dst := make([]HardwareVersion, len(hardwareVersions))
   144  	copy(dst, hardwareVersions)
   145  	return dst
   146  }