-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.go
93 lines (79 loc) · 2.32 KB
/
utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package devicemanager
import (
"errors"
"fmt"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/device"
psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
)
// UnknownDeviceError is returned when an operation is attempted on an unknown
// device.
type UnknownDeviceError struct {
Err error
Name string
Vendor string
Type string
IDs []string
}
// NewUnknownDeviceError returns a new UnknownDeviceError for the given device.
func NewUnknownDeviceError(err error, name, vendor, devType string, ids []string) *UnknownDeviceError {
return &UnknownDeviceError{
Err: err,
Name: name,
Vendor: vendor, Type: devType,
IDs: ids,
}
}
// Error returns an error formatting that reveals which unknown devices were
// requested
func (u *UnknownDeviceError) Error() string {
return fmt.Sprintf("operation on unknown device(s) \"%s/%s/%s\" (%v): %v",
u.Vendor, u.Type, u.Name, u.IDs, u.Err)
}
// UnknownDeviceErrFromAllocated is a helper that returns an UnknownDeviceError
// populating it via the AllocatedDeviceResource struct.
func UnknownDeviceErrFromAllocated(err string, d *structs.AllocatedDeviceResource) *UnknownDeviceError {
return NewUnknownDeviceError(errors.New(err), d.Name, d.Vendor, d.Type, d.DeviceIDs)
}
// convertDeviceGroup converts a device group to a structs NodeDeviceResource
func convertDeviceGroup(d *device.DeviceGroup) *structs.NodeDeviceResource {
if d == nil {
return nil
}
return &structs.NodeDeviceResource{
Vendor: d.Vendor,
Type: d.Type,
Name: d.Name,
Instances: convertDevices(d.Devices),
Attributes: psstructs.CopyMapStringAttribute(d.Attributes),
}
}
func convertDevices(devs []*device.Device) []*structs.NodeDevice {
if devs == nil {
return nil
}
out := make([]*structs.NodeDevice, len(devs))
for i, dev := range devs {
out[i] = convertDevice(dev)
}
return out
}
func convertDevice(dev *device.Device) *structs.NodeDevice {
if dev == nil {
return nil
}
return &structs.NodeDevice{
ID: dev.ID,
Healthy: dev.Healthy,
HealthDescription: dev.HealthDesc,
Locality: convertHwLocality(dev.HwLocality),
}
}
func convertHwLocality(l *device.DeviceLocality) *structs.NodeDeviceLocality {
if l == nil {
return nil
}
return &structs.NodeDeviceLocality{
PciBusID: l.PciBusID,
}
}