-
Notifications
You must be signed in to change notification settings - Fork 56
/
update.go
463 lines (438 loc) · 19.4 KB
/
update.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//(C) Copyright [2020] Hewlett Packard Enterprise Development LP
//
//Licensed under the Apache License, Version 2.0 (the "License"); you may
//not use this file except in compliance with the License. You may obtain
//a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
//License for the specific language governing permissions and limitations
// under the License.
package system
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"
aggregatorproto "github.com/ODIM-Project/ODIM/lib-utilities/proto/aggregator"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/ODIM/svc-aggregation/agmodel"
"github.com/ODIM-Project/ODIM/svc-aggregation/agresponse"
)
// UpdateAggregationSource defines the interface for updation of added Aggregation Source
func (e *ExternalInterface) UpdateAggregationSource(ctx context.Context, req *aggregatorproto.AggregatorRequest) response.RPC {
// validate the aggregation source if it's present in odim
var resp response.RPC
aggregationSource, dbErr := agmodel.GetAggregationSourceInfo(req.URL)
if dbErr != nil {
l.LogWithFields(ctx).Error("Unable to get AggregationSource : " + dbErr.Error())
errorMessage := dbErr.Error()
if errors.DBKeyNotFound == dbErr.ErrNo() {
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errorMessage, []interface{}{"AggregationSource", req.URL}, nil)
}
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errorMessage, nil, nil)
}
// parse the request
var updateRequest map[string]interface{}
err := json.Unmarshal(req.RequestBody, &updateRequest)
if err != nil {
errMsg := "Unable to parse the add request" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
if len(updateRequest) <= 0 {
param := "HostName UserName Password "
errMsg := "field " + param + " Missing"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.PropertyMissing, errMsg, []interface{}{param}, nil)
}
var param string
for key, value := range updateRequest {
if value.(string) == "" {
param = param + key + " "
}
}
if param != "" {
errMsg := "field " + param + " Missing"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.PropertyMissing, errMsg, []interface{}{param}, nil)
}
if _, ok := updateRequest["UserName"]; !ok {
updateRequest["UserName"] = aggregationSource.UserName
}
var hostNameUpdated bool
if _, ok := updateRequest["HostName"]; !ok {
updateRequest["HostName"] = aggregationSource.HostName
} else {
err := validateManagerAddress(updateRequest["HostName"].(string))
if err != nil {
l.LogWithFields(ctx).Error(err.Error())
return common.GeneralError(http.StatusBadRequest, response.PropertyValueFormatError, err.Error(), []interface{}{updateRequest["HostName"].(string), "HostName"}, nil)
}
if updateRequest["HostName"].(string) != aggregationSource.HostName {
hostNameUpdated = true
}
}
if _, ok := updateRequest["Password"]; !ok {
decryptedPasswordByte, err := e.DecryptPassword(aggregationSource.Password)
if err != nil {
errMsg := "Unable to decrypt device password: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
updateRequest["Password"] = decryptedPasswordByte
} else {
bytePassword := []byte(updateRequest["Password"].(string))
updateRequest["Password"] = bytePassword
}
if hostNameUpdated {
// check if the Requested Updated BMCAddress is already present
ipAddr := updateRequest["HostName"].(string)
indexList, err := agmodel.GetString("BMCAddress", ipAddr)
if err != nil {
errMsg := fmt.Sprintf("Unable to collect the active request details from DB: %v", err.Error())
l.LogWithFields(ctx).Println(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
if len(indexList) > 0 {
errMsg := fmt.Sprintf("Manager address already exist %v", ipAddr)
return common.GeneralError(http.StatusConflict, response.ResourceAlreadyExists, errMsg, []interface{}{"ComputerSystem", "HostName", ipAddr}, nil)
}
}
var data = strings.Split(req.URL, "/redfish/v1/AggregationService/AggregationSources/")
links := aggregationSource.Links.(map[string]interface{})
resp = e.updateAggregationSourceWithConnectionMethod(ctx, req.URL, links["ConnectionMethod"].(map[string]interface{}), updateRequest, hostNameUpdated)
if resp.StatusMessage != "" {
return resp
}
// Update the aggregation source info
aggregationSource.HostName = updateRequest["HostName"].(string)
aggregationSource.UserName = updateRequest["UserName"].(string)
aggregationSource.Password = updateRequest["Password"].([]byte)
dbErr = agmodel.UpdateAggregtionSource(aggregationSource, req.URL)
if dbErr != nil {
errMsg := "error while trying to update aggregation source info: " + dbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
commonResponse := response.Response{
OdataType: common.AggregationSourceType,
OdataID: req.URL,
OdataContext: "/redfish/v1/$metadata#AggregationSource.AggregationSource",
ID: data[1],
Name: "Aggregation Source",
}
commonResponse.CreateGenericResponse(response.Success)
commonResponse.Message = ""
commonResponse.MessageID = ""
commonResponse.Severity = ""
resp.Body = agresponse.AggregationSourceResponse{
Response: commonResponse,
HostName: updateRequest["HostName"].(string),
UserName: updateRequest["UserName"].(string),
Links: aggregationSource.Links,
}
resp.StatusCode = http.StatusOK
resp.StatusMessage = response.Success
return resp
}
func (e *ExternalInterface) updateAggregationSourceWithConnectionMethod(ctx context.Context, url string, connectionMethodLink, updateRequest map[string]interface{}, hostNameUpdated bool) response.RPC {
connectionMethodOdataID := connectionMethodLink["@odata.id"].(string)
connectionMethod, err := e.GetConnectionMethod(connectionMethodOdataID)
if err != nil {
l.LogWithFields(ctx).Error("Unable to get connectionmethod : " + err.Error())
errorMessage := err.Error()
if errors.DBKeyNotFound == err.ErrNo() {
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, err.Error(), []interface{}{"ConnectionMethod", connectionMethodOdataID}, nil)
}
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errorMessage, nil, nil)
}
cmVariants := getConnectionMethodVariants(connectionMethod.ConnectionMethodVariant)
var data = strings.Split(url, "/redfish/v1/AggregationService/AggregationSources/")
uuid := url[strings.LastIndexByte(url, '/')+1:]
uuidData := strings.SplitN(uuid, ".", 2)
target, terr := agmodel.GetTarget(uuidData[0])
if terr != nil || target == nil {
return e.updateManagerAggregationSource(ctx, data[1], cmVariants.PluginID, updateRequest, hostNameUpdated)
}
return e.updateBMCAggregationSource(ctx, uuidData[0], cmVariants.PluginID, updateRequest, hostNameUpdated)
}
func (e *ExternalInterface) updateManagerAggregationSource(ctx context.Context, aggregationSourceID, pluginID string, updateRequest map[string]interface{}, hostNameUpdated bool) response.RPC {
plugin, errs := agmodel.GetPluginData(pluginID)
if errs != nil {
errMsg := errs.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errMsg, []interface{}{"plugin", pluginID}, nil)
}
ipData := strings.Split(updateRequest["HostName"].(string), ":")
plugin.IP = ipData[0]
plugin.Port = ipData[1]
plugin.Username = updateRequest["UserName"].(string)
plugin.Password = updateRequest["Password"].([]byte)
var pluginContactRequest getResourceRequest
pluginContactRequest.ContactClient = e.ContactClient
pluginContactRequest.GetPluginStatus = e.GetPluginStatus
pluginContactRequest.Plugin = plugin
pluginContactRequest.StatusPoll = true
if strings.EqualFold(plugin.PreferredAuthType, "XAuthToken") {
pluginContactRequest.HTTPMethodType = http.MethodPost
pluginContactRequest.DeviceInfo = map[string]interface{}{
"Username": plugin.Username,
"Password": string(plugin.Password),
}
pluginContactRequest.OID = "/ODIM/v1/Sessions"
_, token, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while creating the session: ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
pluginContactRequest.Token = token
} else {
pluginContactRequest.LoginCredentials = map[string]string{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
}
// Verfiying the plugin Status
pluginContactRequest.HTTPMethodType = http.MethodGet
pluginContactRequest.OID = "/ODIM/v1/Status"
body, _, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while getting the details "+pluginContactRequest.OID+": ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
var managerUUID = plugin.ManagerUUID
var managersMap map[string]interface{}
// Getting all managers info from plugin
pluginContactRequest.OID = "/ODIM/v1/Managers"
body, _, getResponse, err = contactPlugin(ctx, pluginContactRequest, "error while getting the details "+pluginContactRequest.OID+": ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
// Extract all managers info and loop over each members
err = json.Unmarshal([]byte(body), &managersMap)
if err != nil {
errMsg := "Unable to parse the managers resposne" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
if hostNameUpdated {
managerMembers := managersMap["Members"]
// Getting the individual managers response
for _, object := range managerMembers.([]interface{}) {
pluginContactRequest.OID = object.(map[string]interface{})["@odata.id"].(string)
body, _, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while getting the details "+pluginContactRequest.OID+": ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
var managerData map[string]interface{}
err = json.Unmarshal([]byte(body), &managerData)
if err != nil {
errMsg := "Unable to parse the managers response" + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
if uuid, ok := managerData["UUID"]; ok {
managerUUID = uuid.(string)
}
}
if managerUUID != plugin.ManagerUUID {
errMsg := "Manager UUID " + managerUUID + "is not matching plugin UUID: " + plugin.ManagerUUID
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.ResourceInUse, errMsg, nil, nil)
}
}
// encrypt plugin password
ciphertext, err := e.EncryptPassword(plugin.Password)
if err != nil {
errMsg := "Encryption failed: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
plugin.Password = ciphertext
plugin.ManagerUUID = managerUUID
updateRequest["Password"] = ciphertext
dbErr := agmodel.UpdatePluginData(plugin, pluginID)
if dbErr != nil {
errMsg := "Unable to update plugin info: " + dbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
return response.RPC{
StatusCode: http.StatusOK,
}
}
func (e *ExternalInterface) updateBMCAggregationSource(ctx context.Context, aggregationSourceID, pluginID string, updateRequest map[string]interface{}, hostNameUpdated bool) response.RPC {
// Get the plugin from db
plugin, errs := agmodel.GetPluginData(pluginID)
if errs != nil {
errMsg := errs.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errMsg, []interface{}{"plugin", pluginID}, nil)
}
var pluginContactRequest getResourceRequest
pluginContactRequest.ContactClient = e.ContactClient
pluginContactRequest.GetPluginStatus = e.GetPluginStatus
pluginContactRequest.Plugin = plugin
pluginContactRequest.StatusPoll = true
if strings.EqualFold(plugin.PreferredAuthType, "XAuthToken") {
var err error
pluginContactRequest.HTTPMethodType = http.MethodPost
pluginContactRequest.DeviceInfo = map[string]interface{}{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
pluginContactRequest.OID = "/ODIM/v1/Sessions"
_, token, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while logging in to plugin: ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
pluginContactRequest.Token = token
} else {
pluginContactRequest.LoginCredentials = map[string]string{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
}
// validate the device credentials
var saveSystem = agmodel.SaveSystem{
ManagerAddress: updateRequest["HostName"].(string),
UserName: updateRequest["UserName"].(string),
Password: updateRequest["Password"].([]byte),
}
pluginContactRequest.DeviceInfo = saveSystem
pluginContactRequest.OID = "/ODIM/v1/validate"
pluginContactRequest.HTTPMethodType = http.MethodPost
body, _, getResponse, err := contactPlugin(ctx, pluginContactRequest, "error while trying to authenticate the compute server: ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
var commonError errors.CommonError
err = json.Unmarshal(body, &commonError)
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
if hostNameUpdated {
// Get All systems
pluginContactRequest.OID = "/redfish/v1/Systems"
pluginContactRequest.HTTPMethodType = http.MethodGet
body, _, getResponse, err = contactPlugin(ctx, pluginContactRequest, "error while trying to get system collection details: ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
var systemsMap map[string]interface{}
err = json.Unmarshal([]byte(body), &systemsMap)
if err != nil {
errMsg := "Unable to unmarshal systems collection: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
systemMembers := systemsMap["Members"]
for _, object := range systemMembers.([]interface{}) {
oDataID := object.(map[string]interface{})["@odata.id"].(string)
pluginContactRequest.OID = oDataID
body, _, getResponse, err = contactPlugin(ctx, pluginContactRequest, "error while trying to get system details: ")
if err != nil {
errMsg := err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, nil)
}
var computeSystem map[string]interface{}
err = json.Unmarshal(body, &computeSystem)
if err != nil {
errMsg := "Unable to unmarshal computer system: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
computeSystemID := computeSystem["Id"].(string)
computeSystemUUID := computeSystem["UUID"].(string)
oidKey := keyFormation(oDataID, computeSystemID, aggregationSourceID)
l.LogWithFields(ctx).Info("Computer SystemUUID" + computeSystemUUID)
indexList, err := agmodel.GetString("UUID", computeSystemUUID)
if err != nil {
errMsg := "Unable to get computer system index: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
l.LogWithFields(ctx).Info("Index List" + strings.Join(indexList, "::"))
if len(indexList) <= 0 {
errMsg := "UUID of the added bmc is not matching with given HostName"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.ResourceInUse, errMsg, nil, nil)
}
var isPresent bool
for _, systemID := range indexList {
if systemID == oidKey {
isPresent = true
}
}
if !isPresent {
errMsg := "UUID of the added bmc is not matching with given HostName"
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.ResourceInUse, errMsg, nil, nil)
}
// updating the index of BMC address
err = agmodel.UpdateIndex(map[string]interface{}{}, oidKey, computeSystemUUID, updateRequest["HostName"].(string))
if err != nil {
errMsg := "error while trying updating index values: " + err.Error()
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
}
}
// update the system
saveSystem.PluginID = pluginID
saveSystem.DeviceUUID = aggregationSourceID
// encrypt the device password
ciphertext, err := e.EncryptPassword([]byte(saveSystem.Password))
if err != nil {
errMsg := "Unable to encrypt device password: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
saveSystem.Password = ciphertext
updateRequest["Password"] = ciphertext
dbErr := agmodel.UpdateSystemData(saveSystem, aggregationSourceID)
if dbErr != nil {
errMsg := "Unable to update system info: " + dbErr.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
}
return response.RPC{
StatusCode: http.StatusOK,
}
}
func validateManagerAddress(managerAddress string) error {
// if the manager address is of the form <IP/FQDN>:<port>
// will split address to obtain only IP/FQDN. If obtained
// value is empty, then will use the actual address passed
addr, _, _ := net.SplitHostPort(managerAddress)
if addr == "" {
addr = managerAddress
}
if _, err := net.ResolveIPAddr("ip", addr); err != nil {
return fmt.Errorf("error: failed to resolve ManagerAddress: %v", err)
}
return nil
}