-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_sls.go
284 lines (256 loc) · 8.65 KB
/
diff_sls.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
*
* MIT License
*
* (C) Copyright 2022-2023 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package sls
import (
"fmt"
"reflect"
sls_client "github.com/Cray-HPE/cani/pkg/sls-client"
)
// Hardware present in A that is missing from B
// Set subtract operation
func HardwareSubtract(a, b sls_client.SlsState) ([]sls_client.Hardware, error) {
var missingHardware []sls_client.Hardware
// Build up a lookup map for the hardware in set B
bHardwareMap := map[string]sls_client.Hardware{}
for _, hardware := range b.Hardware {
// Verify new hardware
if _, present := bHardwareMap[hardware.Xname]; present {
return nil, fmt.Errorf("found duplicate xname %v in set B", hardware.Xname)
}
bHardwareMap[hardware.Xname] = hardware
}
// Iterate over Set A to identify hardware not present in set B
for _, hardware := range a.Hardware {
if _, present := bHardwareMap[hardware.Xname]; present {
continue
}
missingHardware = append(missingHardware, hardware)
}
// Sort the slice to make it look nice, and have a deterministic order
HardwareSort(missingHardware)
return missingHardware, nil
}
type HardwarePair struct {
Xname string
HardwareA sls_client.Hardware
HardwareB sls_client.Hardware
}
// Identify hardware
// Note when comparing hardware network information like IP address and subnets are not considered.
// TODO make striping of networking information toggable
func HardwareUnion(a, b sls_client.SlsState) (identicalHardware []sls_client.Hardware, differingContents []HardwarePair, err error) {
// Build up a lookup map for the hardware in set B
bHardwareMap := map[string]sls_client.Hardware{}
for _, hardware := range b.Hardware {
// Verify new hardware
if _, present := bHardwareMap[hardware.Xname]; present {
return nil, nil, fmt.Errorf("found duplicate xname %v in set B", hardware.Xname)
}
bHardwareMap[hardware.Xname] = hardware
}
// Iterate over Set A to identify hardware present in set B
for _, hardwareA := range a.Hardware {
hardwareB, present := bHardwareMap[hardwareA.Xname]
if !present {
continue
}
hardwarePair := HardwarePair{
Xname: hardwareA.Xname,
HardwareA: hardwareA,
HardwareB: hardwareB,
}
// See if the hardware class between the 2 hardware objects is different
if hardwareA.Class != hardwareB.Class {
differingContents = append(differingContents, hardwarePair)
// Don't bother checking differences in extra properties as there are already differences.
continue
}
// Next check to see if the extra properties between the two hardware objects
// We are ignoring fields like IPv4 fields and Model during the comparison
// as that is something that we don't know when generating from the CCJ or CSI
extraPropertiesA, err := hardwareA.DecodeExtraProperties()
if err != nil {
return nil, nil, fmt.Errorf("failed to decode extra properties on (%s): %w", hardwareA.Xname, err)
}
extraPropertiesA = stripIpInformationFromHardware(extraPropertiesA)
extraPropertiesB, err := hardwareB.DecodeExtraProperties()
if err != nil {
return nil, nil, fmt.Errorf("failed to decode extra properties on (%s): %w", hardwareB.Xname, err)
}
extraPropertiesB = stripIpInformationFromHardware(extraPropertiesB)
if !reflect.DeepEqual(extraPropertiesA, extraPropertiesB) {
differingContents = append(differingContents, hardwarePair)
continue
}
// If we made it here, then these 2 hardware objects must be identical
identicalHardware = append(identicalHardware, hardwareA)
}
// Sort the slices to make it look nice, and have a deterministic order
HardwareSort(identicalHardware)
HardwarePairSort(differingContents)
return
}
func stripIpInformationFromHardware(extraPropertiesRaw interface{}) interface{} {
// Helper command to build up the switch
// grep -R "type HardwareExtraProperties" -R ./pkg/sls-client/ | grep -v HardwareExtraPropertiesCabinetNetworks | awk '{print $2 ":" }' | sort | sed -e 's/^/case sls_client./'
switch ep := extraPropertiesRaw.(type) {
case sls_client.HardwareExtraPropertiesBmcNic:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesCabPduNic:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesCabPduPwrConnector:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesCabinet:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.Model = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesCduMgmtSwitch:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesChassis:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesChassisBmc:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesCompmod:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesCompmodPowerConnector:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesHsnConnector:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesMgmtHlSwitch:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
ep.IP4addr = ""
ep.IP6addr = ""
ep.Model = "" // Not guaranteed that the system was installed with information about the switch model.
return ep
case sls_client.HardwareExtraPropertiesMgmtSwitch:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
ep.IP4addr = ""
ep.IP6addr = ""
ep.Model = "" // Not guaranteed that the system was installed with information about the switch model.
return ep
case sls_client.HardwareExtraPropertiesMgmtSwitchConnector:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
ep.CaniLastModified = ""
return ep
case sls_client.HardwareExtraPropertiesNcard:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesNode:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesNodeHsnNic:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesNodeNic:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesRtrBmc:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesRtrBmcNic:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesRtrmod:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
case sls_client.HardwareExtraPropertiesSystem:
ep.CaniId = ""
ep.CaniSlsSchemaVersion = ""
ep.CaniLastModified = ""
ep.CaniStatus = nil
return ep
}
return extraPropertiesRaw
}