From a065f49db3fa48e0dbd09de1e52d672d0bb4b687 Mon Sep 17 00:00:00 2001 From: Michal Fiedorowicz Date: Fri, 2 Feb 2024 20:50:29 +0000 Subject: [PATCH] feat: OBS-381 - extract push request validation into func Signed-off-by: Michal Fiedorowicz --- diode-server/distributor/component.go | 67 ++++++++++++++------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/diode-server/distributor/component.go b/diode-server/distributor/component.go index 866fc5ce..b222673e 100644 --- a/diode-server/distributor/component.go +++ b/diode-server/distributor/component.go @@ -91,9 +91,8 @@ func (c *Component) Stop() error { // Push handles a push request func (c *Component) Push(ctx context.Context, in *pb.PushRequest) (*pb.PushResponse, error) { - reqID := in.GetId() - if reqID == "" { - return nil, fmt.Errorf("id is empty") + if err := validatePushRequest(in); err != nil { + return nil, err } reqStream := in.GetStream() @@ -101,30 +100,6 @@ func (c *Component) Push(ctx context.Context, in *pb.PushRequest) (*pb.PushRespo reqStream = DefaultRequestStream } - producerAppName := in.GetProducerAppName() - if producerAppName == "" { - return nil, fmt.Errorf("producer app name is empty") - } - - producerAppVersion := in.GetProducerAppVersion() - if producerAppVersion == "" { - return nil, fmt.Errorf("producer app version is empty") - } - - sdkName := in.GetSdkName() - if sdkName == "" { - return nil, fmt.Errorf("sdk name is empty") - } - - sdkVersion := in.GetSdkVersion() - if sdkVersion == "" { - return nil, fmt.Errorf("sdk version is empty") - } - - if len(in.GetData()) < 1 { - return nil, fmt.Errorf("data is empty") - } - errs := make([]string, 0) for i, v := range in.GetData() { @@ -139,12 +114,12 @@ func (c *Component) Push(ctx context.Context, in *pb.PushRequest) (*pb.PushRespo continue } msg := map[string]interface{}{ - "id": reqID, + "id": in.GetId(), "stream": reqStream, - "producer_app_name": producerAppName, - "producer_app_version": producerAppVersion, - "sdk_name": sdkName, - "sdk_version": sdkVersion, + "producer_app_name": in.GetProducerAppName(), + "producer_app_version": in.GetProducerAppVersion(), + "sdk_name": in.GetSdkName(), + "sdk_version": in.GetSdkVersion(), "data": encodedEntity, "ts": v.GetTimestamp().String(), "ingestion_ts": time.Now().UnixNano(), @@ -159,3 +134,31 @@ func (c *Component) Push(ctx context.Context, in *pb.PushRequest) (*pb.PushRespo return &pb.PushResponse{Errors: errs}, nil } + +func validatePushRequest(in *pb.PushRequest) error { + if in.GetId() == "" { + return fmt.Errorf("id is empty") + } + + if in.GetProducerAppName() == "" { + return fmt.Errorf("producer app name is empty") + } + + if in.GetProducerAppVersion() == "" { + return fmt.Errorf("producer app version is empty") + } + + if in.GetSdkName() == "" { + return fmt.Errorf("sdk name is empty") + } + + if in.GetSdkVersion() == "" { + return fmt.Errorf("sdk version is empty") + } + + if len(in.GetData()) < 1 { + return fmt.Errorf("data is empty") + } + + return nil +}