-
Notifications
You must be signed in to change notification settings - Fork 291
/
addons.go
64 lines (51 loc) · 1.66 KB
/
addons.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// (C) Copyright 2022-2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package ctl
import (
"strconv"
"strings"
"github.com/pkg/errors"
)
func unmarshalState(data []byte, toName map[int32]string, toValue map[string]int32) (int32, error) {
stateStr := strings.Trim(strings.ToUpper(string(data)), "\"")
if si, err := strconv.ParseInt(stateStr, 0, 32); err == nil {
state := int32(si)
if _, ok := toName[state]; !ok {
return 0, errors.Errorf("invalid state name lookup %q", stateStr)
}
return state, nil
}
// Try converting the string to an int32, to handle the conversion from control-plane native type.
if si, ok := toValue[stateStr]; ok {
return int32(si), nil
}
return 0, errors.Errorf("invalid state value lookup %q", stateStr)
}
func (nds *NvmeDevState) UnmarshalJSON(data []byte) error {
state, err := unmarshalState(data, NvmeDevState_name, NvmeDevState_value)
if err != nil {
return err
}
*nds = NvmeDevState(state)
return nil
}
func (vls *LedState) UnmarshalJSON(data []byte) error {
state, err := unmarshalState(data, LedState_name, LedState_value)
if err != nil {
return err
}
*vls = LedState(state)
return nil
}
// IsScannable returns true if NVMe device state indicates controller details are accessible.
func (nc *NvmeController) IsScannable() bool {
return nc.DevState == NvmeDevState_NORMAL || nc.DevState == NvmeDevState_EVICTED ||
nc.DevState == NvmeDevState_NEW
}
// CanSupplyHealthStats returns true if NVMe device state indicates health stats are accessible.
func (nc *NvmeController) CanSupplyHealthStats() bool {
return nc.DevState == NvmeDevState_NORMAL || nc.DevState == NvmeDevState_EVICTED
}