Skip to content

Commit 814bd96

Browse files
ipsita-npgyvuppalx
andauthored
[onboarding-manager<->device discovery]Node Onboarding without SN (#133)
Co-authored-by: yvuppalx <[email protected]>
1 parent 4bd745b commit 814bd96

File tree

2 files changed

+265
-197
lines changed

2 files changed

+265
-197
lines changed

onboarding-manager/internal/handlers/southbound/grpcserver/grpc_server.go

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ var (
3636
zlog = logging.GetLogger(name)
3737
)
3838

39+
const (
40+
serialNumField = "Serialnum"
41+
serialNumNotAvailable = "N/A"
42+
)
43+
3944
type InventoryClientService struct {
4045
invClient *invclient.OnboardingInventoryClient
4146
invClientAPI *invclient.OnboardingInventoryClient
@@ -276,6 +281,16 @@ func (s *NonInteractiveOnboardingService) handleDefaultState(
276281
})
277282
}
278283

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+
279294
//nolint:cyclop,funlen // reason: function is long due to necessary logic; cyclomatic complexity is high due to necessary handling
280295
func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStreamRequest) (*computev1.HostResource, error) {
281296
var hostResource *computev1.HostResource
@@ -289,10 +304,8 @@ func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStr
289304
if errUUID != nil {
290305
if inv_errors.IsNotFound(errUUID) {
291306
zlog.Debug().Msgf("Node doesn't exist for UUID: %v", req.Uuid)
292-
zlog.Error().Err(errUUID).Msgf("Node doesn't exist for UUID")
293307
} else {
294308
zlog.Debug().Msgf("Error retrieving host resource by UUID: %v", req.Uuid)
295-
zlog.Error().Err(errUUID).Msgf("Error retrieving host resource by UUID")
296309
return nil, inv_errors.Errorfc(codes.Internal, "Error retrieving host resource by UUID")
297310
}
298311
} else {
@@ -319,10 +332,8 @@ func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStr
319332
if errSN != nil {
320333
if inv_errors.IsNotFound(errSN) {
321334
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")
323335
} else {
324336
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")
326337
return nil, inv_errors.Errorfc(codes.Internal, "Error retrieving host resource by serial number")
327338
}
328339
} else {
@@ -339,10 +350,15 @@ func (s *NonInteractiveOnboardingService) getHostResource(req *pb.OnboardNodeStr
339350
zlog.Error().Err(errUpdate).Msgf("failed to updated the host resource uuid: %v", errUpdate)
340351
return nil, inv_errors.Errorfc(codes.Internal, "failed to updated the host resource uuid")
341352
}
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)
343355
return hostResource, nil
344356
}
345357
}
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
346362
}
347363

348364
// Handle mismatches between the two resources
@@ -441,9 +457,16 @@ func (s *NonInteractiveOnboardingService) OnboardNodeStream(
441457

442458
// Validate the stream request using the generated Validate method
443459
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+
}
445469
}
446-
447470
// Retrieves the host resource based on UUID or Serial Number.
448471
hostInv, err = s.getHostResource(req)
449472
if err != nil {
@@ -517,14 +540,48 @@ func (s *NonInteractiveOnboardingService) OnboardNodeStream(
517540
}
518541
}
519542

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+
520564
//nolint:funlen,cyclop // reason: function is long due to necessary logic; cyclomatic complexity is high due to necessary handling
521565
func (s *InteractiveOnboardingService) CreateNodes(ctx context.Context, req *pb.CreateNodesRequest) (
522566
*pb.CreateNodesResponse, error,
523567
) {
524568
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+
}
528585
}
529586

530587
if s.authEnabled {

0 commit comments

Comments
 (0)