-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
devices.go
155 lines (138 loc) · 3.44 KB
/
devices.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//go:build linux && cgo && !agent
package cluster
import (
"fmt"
)
// Code generation directives.
//
//go:generate -command mapper incus-generate db mapper -t devices.mapper.go
//go:generate mapper reset -i -b "//go:build linux && cgo && !agent"
//
//go:generate mapper stmt -e device objects
//go:generate mapper stmt -e device create struct=Device
//go:generate mapper stmt -e device delete
//
//go:generate mapper method -i -e device GetMany
//go:generate mapper method -i -e device Create struct=Device
//go:generate mapper method -i -e device Update struct=Device
//go:generate mapper method -i -e device DeleteMany
// DeviceType represents the types of supported devices.
type DeviceType int
// Device is a reference struct representing another entity's device.
type Device struct {
ID int
ReferenceID int
Name string
Type DeviceType
Config map[string]string
}
// DeviceFilter specifies potential query parameter fields.
type DeviceFilter struct {
Name *string
Type *DeviceType
Config *ConfigFilter
}
// Supported device types.
const (
TypeNone = DeviceType(0)
TypeNIC = DeviceType(1)
TypeDisk = DeviceType(2)
TypeUnixChar = DeviceType(3)
TypeUnixBlock = DeviceType(4)
TypeUSB = DeviceType(5)
TypeGPU = DeviceType(6)
TypeInfiniband = DeviceType(7)
TypeProxy = DeviceType(8)
TypeUnixHotplug = DeviceType(9)
TypeTPM = DeviceType(10)
TypePCI = DeviceType(11)
)
func (t DeviceType) String() string {
switch t {
case TypeNone:
return "none"
case TypeNIC:
return "nic"
case TypeDisk:
return "disk"
case TypeUnixChar:
return "unix-char"
case TypeUnixBlock:
return "unix-block"
case TypeUSB:
return "usb"
case TypeGPU:
return "gpu"
case TypeInfiniband:
return "infiniband"
case TypeProxy:
return "proxy"
case TypeUnixHotplug:
return "unix-hotplug"
case TypeTPM:
return "tpm"
case TypePCI:
return "pci"
}
return ""
}
// NewDeviceType determines the device type from the given string, if supported.
func NewDeviceType(t string) (DeviceType, error) {
switch t {
case "none":
return TypeNone, nil
case "nic":
return TypeNIC, nil
case "disk":
return TypeDisk, nil
case "unix-char":
return TypeUnixChar, nil
case "unix-block":
return TypeUnixBlock, nil
case "usb":
return TypeUSB, nil
case "gpu":
return TypeGPU, nil
case "infiniband":
return TypeInfiniband, nil
case "proxy":
return TypeProxy, nil
case "unix-hotplug":
return TypeUnixHotplug, nil
case "tpm":
return TypeTPM, nil
case "pci":
return TypePCI, nil
default:
return -1, fmt.Errorf("Invalid device type %s", t)
}
}
// DevicesToAPI takes a map of devices and converts them to API format.
func DevicesToAPI(devices map[string]Device) map[string]map[string]string {
config := map[string]map[string]string{}
for _, d := range devices {
if d.Config == nil {
d.Config = map[string]string{}
}
config[d.Name] = d.Config
config[d.Name]["type"] = d.Type.String()
}
return config
}
// APIToDevices takes an API format devices map and converts it to a map of db.Device.
func APIToDevices(apiDevices map[string]map[string]string) (map[string]Device, error) {
devices := map[string]Device{}
for name, config := range apiDevices {
newType, err := NewDeviceType(config["type"])
if err != nil {
return nil, err
}
device := Device{
Name: name,
Type: newType,
Config: config,
}
devices[name] = device
}
return devices, nil
}