github.com/vmware/govmomi@v0.37.1/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 VMX3 HardwareVersion = iota + 3 30 VMX4 31 32 vmx5 // invalid 33 34 VMX6 35 VMX7 36 VMX8 37 VMX9 38 VMX10 39 VMX11 40 VMX12 41 VMX13 42 VMX14 43 VMX15 44 VMX16 45 VMX17 46 VMX18 47 VMX19 48 VMX20 49 VMX21 50 ) 51 52 const ( 53 // MinValidHardwareVersion is the minimum, valid hardware version supported 54 // by VMware hypervisors in the wild. 55 MinValidHardwareVersion = VMX3 56 57 // MaxValidHardwareVersion is the maximum, valid hardware version supported 58 // by VMware hypervisors in the wild. 59 MaxValidHardwareVersion = VMX21 60 ) 61 62 func (hv HardwareVersion) IsValid() bool { 63 return hv != vmx5 && 64 hv >= MinValidHardwareVersion && 65 hv <= MaxValidHardwareVersion 66 } 67 68 func (hv HardwareVersion) String() string { 69 if hv.IsValid() { 70 return fmt.Sprintf("vmx-%d", hv) 71 } 72 return "" 73 } 74 75 func (hv HardwareVersion) MarshalText() ([]byte, error) { 76 return []byte(hv.String()), nil 77 } 78 79 func (hv *HardwareVersion) UnmarshalText(text []byte) error { 80 v, err := ParseHardwareVersion(string(text)) 81 if err != nil { 82 return err 83 } 84 *hv = v 85 return nil 86 } 87 88 var vmxRe = regexp.MustCompile(`(?i)^vmx-(\d+)$`) 89 90 // MustParseHardwareVersion parses the provided string into a hardware version. 91 func MustParseHardwareVersion(s string) HardwareVersion { 92 v, err := ParseHardwareVersion(s) 93 if err != nil { 94 panic(err) 95 } 96 return v 97 } 98 99 // ParseHardwareVersion parses the provided string into a hardware version. 100 func ParseHardwareVersion(s string) (HardwareVersion, error) { 101 var u uint64 102 if m := vmxRe.FindStringSubmatch(s); len(m) > 0 { 103 u, _ = strconv.ParseUint(m[1], 10, 8) 104 } else { 105 u, _ = strconv.ParseUint(s, 10, 8) 106 } 107 v := HardwareVersion(u) 108 if !v.IsValid() { 109 return 0, fmt.Errorf("invalid version: %q", s) 110 } 111 return v, nil 112 } 113 114 var hardwareVersions []HardwareVersion 115 116 func init() { 117 for i := MinValidHardwareVersion; i <= MaxValidHardwareVersion; i++ { 118 if i.IsValid() { 119 hardwareVersions = append(hardwareVersions, i) 120 } 121 } 122 } 123 124 // GetHardwareVersions returns a list of hardware versions. 125 func GetHardwareVersions() []HardwareVersion { 126 dst := make([]HardwareVersion, len(hardwareVersions)) 127 copy(dst, hardwareVersions) 128 return dst 129 }