36
36
zlog = logging .GetLogger (name )
37
37
)
38
38
39
+ const (
40
+ serialNumField = "Serialnum"
41
+ serialNumNotAvailable = "N/A"
42
+ )
43
+
39
44
type InventoryClientService struct {
40
45
invClient * invclient.OnboardingInventoryClient
41
46
invClientAPI * invclient.OnboardingInventoryClient
@@ -276,6 +281,16 @@ func (s *NonInteractiveOnboardingService) handleDefaultState(
276
281
})
277
282
}
278
283
284
+ func serialNumberValidationError (err error ) bool {
285
+ var validationErr pb.OnboardNodeStreamRequestValidationError
286
+ if errors .As (err , & validationErr ) {
287
+ if validationErr .Field () == serialNumField {
288
+ return true
289
+ }
290
+ }
291
+ return false
292
+ }
293
+
279
294
//nolint:cyclop,funlen // reason: function is long due to necessary logic; cyclomatic complexity is high due to necessary handling
280
295
func (s * NonInteractiveOnboardingService ) getHostResource (req * pb.OnboardNodeStreamRequest ) (* computev1.HostResource , error ) {
281
296
var hostResource * computev1.HostResource
@@ -289,10 +304,8 @@ func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStr
289
304
if errUUID != nil {
290
305
if inv_errors .IsNotFound (errUUID ) {
291
306
zlog .Debug ().Msgf ("Node doesn't exist for UUID: %v" , req .Uuid )
292
- zlog .Error ().Err (errUUID ).Msgf ("Node doesn't exist for UUID" )
293
307
} else {
294
308
zlog .Debug ().Msgf ("Error retrieving host resource by UUID: %v" , req .Uuid )
295
- zlog .Error ().Err (errUUID ).Msgf ("Error retrieving host resource by UUID" )
296
309
return nil , inv_errors .Errorfc (codes .Internal , "Error retrieving host resource by UUID" )
297
310
}
298
311
} else {
@@ -319,10 +332,8 @@ func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStr
319
332
if errSN != nil {
320
333
if inv_errors .IsNotFound (errSN ) {
321
334
zlog .Debug ().Msgf ("Node doesn't exist for serial number: %v" , req .Serialnum )
322
- zlog .Error ().Err (errSN ).Msgf ("Node doesn't exist for serial number" )
323
335
} else {
324
336
zlog .Debug ().Msgf ("Error retrieving host resource by serial number: %v" , req .Serialnum )
325
- zlog .Error ().Err (errSN ).Msgf ("Error retrieving host resource by serial number" )
326
337
return nil , inv_errors .Errorfc (codes .Internal , "Error retrieving host resource by serial number" )
327
338
}
328
339
} else {
@@ -339,10 +350,15 @@ func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStr
339
350
zlog .Error ().Err (errUpdate ).Msgf ("failed to updated the host resource uuid: %v" , errUpdate )
340
351
return nil , inv_errors .Errorfc (codes .Internal , "failed to updated the host resource uuid" )
341
352
}
342
- zlog .Debug ().Msgf ("Proceeding with registration for Serial Number %v with no UUID in inventory" , req .Serialnum )
353
+ zlog .Debug ().Msgf ("Proceeding with registration for Serial Number %v with no UUID in inventory" ,
354
+ req .Serialnum )
343
355
return hostResource , nil
344
356
}
345
357
}
358
+ } else if uuidMatch {
359
+ // If UUID is found and serial number is empty, proceed with UUID-based provisioning
360
+ zlog .Debug ().Msgf ("Proceeding with registration for UUID %v with empty Serial Number" , req .Uuid )
361
+ return hostResource , nil
346
362
}
347
363
348
364
// Handle mismatches between the two resources
@@ -441,9 +457,16 @@ func (s *NonInteractiveOnboardingService) OnboardNodeStream(
441
457
442
458
// Validate the stream request using the generated Validate method
443
459
if reqValidateerr := req .Validate (); reqValidateerr != nil {
444
- return sendStreamErrorResponse (stream , codes .InvalidArgument , reqValidateerr .Error ())
460
+ // Check if the error is related to serial number validation
461
+ if serialNumberValidationError (reqValidateerr ) {
462
+ // Log the validation error and proceed with UUID-based provisioning
463
+ zlog .Debug ().Msgf ("Ignoring serial number validation error: %v" , reqValidateerr )
464
+ req .Serialnum = serialNumNotAvailable // Set serial number to Not Available
465
+ } else {
466
+ // For other validation errors, send an InvalidArgument error response
467
+ return sendStreamErrorResponse (stream , codes .InvalidArgument , reqValidateerr .Error ())
468
+ }
445
469
}
446
-
447
470
// Retrieves the host resource based on UUID or Serial Number.
448
471
hostInv , err = s .getHostResource (req )
449
472
if err != nil {
@@ -517,14 +540,48 @@ func (s *NonInteractiveOnboardingService) OnboardNodeStream(
517
540
}
518
541
}
519
542
543
+ func serialNumberValidationErrorIO (err error ) bool {
544
+ type validationError interface {
545
+ Field () string
546
+ Cause () error
547
+ }
548
+
549
+ for currentErr := err ; currentErr != nil ; {
550
+ if e , ok := currentErr .(validationError ); ok {
551
+ // Check if the field is "Serialnum"
552
+ if e .Field () == serialNumField {
553
+ return true
554
+ }
555
+ // Move to the next error
556
+ currentErr = e .Cause ()
557
+ } else {
558
+ return false
559
+ }
560
+ }
561
+ return false
562
+ }
563
+
520
564
//nolint:funlen,cyclop // reason: function is long due to necessary logic; cyclomatic complexity is high due to necessary handling
521
565
func (s * InteractiveOnboardingService ) CreateNodes (ctx context.Context , req * pb.CreateNodesRequest ) (
522
566
* pb.CreateNodesResponse , error ,
523
567
) {
524
568
zlog .Info ().Msgf ("CreateNodes" )
525
- if validationErr := req .Validate (); validationErr != nil {
526
- zlog .InfraSec ().InfraErr (validationErr ).Msgf ("Request does not match the expected regex pattern %v" , validationErr )
527
- return nil , validationErr
569
+
570
+ // Validate the request using the generated Validate method
571
+ if reqValidateerr := req .Validate (); reqValidateerr != nil {
572
+ // Check if the error is related to serial number validation
573
+ if serialNumberValidationErrorIO (reqValidateerr ) {
574
+ // Ignore serail number validation error
575
+ zlog .Debug ().Msgf ("Ignoring serial number validation error: %v" , reqValidateerr )
576
+ for _ , nodeData := range req .GetPayload () {
577
+ for _ , hwData := range nodeData .GetHwdata () {
578
+ hwData .Serialnum = serialNumNotAvailable // Set serial number to Not Available
579
+ }
580
+ }
581
+ } else {
582
+ // For other validation errors, return the error
583
+ return nil , reqValidateerr
584
+ }
528
585
}
529
586
530
587
if s .authEnabled {
0 commit comments