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