@@ -617,17 +617,6 @@ func (s *Server) GetProfileStatusById(
617617 }, nil
618618}
619619
620- func extractEntitySelector (entity * minderv1.EntityTypedId ) * uuid.NullUUID {
621- if entity == nil {
622- return nil
623- }
624- var selector uuid.NullUUID
625- if err := selector .Scan (entity .GetId ()); err != nil {
626- return nil
627- }
628- return & selector
629- }
630-
631620func (s * Server ) processProfileStatusByName (
632621 ctx context.Context ,
633622 profileName string ,
@@ -638,21 +627,29 @@ func (s *Server) processProfileStatusByName(
638627) (* minderv1.GetProfileStatusByNameResponse , error ) {
639628 var ruleEvaluationStatuses []* minderv1.RuleEvaluationStatus
640629
641- selector , ruleType , ruleName , err := extractFiltersFromNameRequest (req )
642- if err != nil {
630+ // Telemetry logging
631+ entityCtx := engcontext .EntityFromContext (ctx )
632+ logger .BusinessRecord (ctx ).Project = entityCtx .Project .ID
633+ logger .BusinessRecord (ctx ).Profile = logger.Profile {Name : profileName , ID : profileID }
634+
635+ if err := validateEntityType (req .GetEntity ()); err != nil {
643636 return nil , err
644637 }
638+ maybeEntityID , err := maybeNullUUID (req .GetEntity ().GetId ())
639+ if err != nil {
640+ return nil , util .UserVisibleError (codes .InvalidArgument , "Unable to parse entity id: %q" , req .GetEntity ().GetId ())
641+ }
642+ maybeEntityName := maybeNullString (req .GetEntity ().GetName ())
643+ ruleType := maybeNullString (req .GetRuleType ())
644+ ruleName := maybeNullString (req .GetRuleName ())
645645
646- if selector != nil || req .GetAll () {
647- var entityID uuid.NullUUID
648- if selector != nil {
649- entityID = * selector
650- }
646+ if req .GetAll () || maybeEntityID .Valid || maybeEntityName .Valid {
651647 dbRuleEvaluationStatuses , err := s .store .ListRuleEvaluationsByProfileId (ctx , db.ListRuleEvaluationsByProfileIdParams {
652648 ProfileID : profileID ,
653- EntityID : entityID ,
654- RuleTypeName : * ruleType ,
655- RuleName : * ruleName ,
649+ EntityID : maybeEntityID ,
650+ EntityName : maybeEntityName ,
651+ RuleTypeName : ruleType ,
652+ RuleName : ruleName ,
656653 })
657654 if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
658655 return nil , status .Errorf (codes .Unknown , "failed to list rule evaluation status: %s" , err )
@@ -663,11 +660,6 @@ func (s *Server) processProfileStatusByName(
663660 )
664661 }
665662
666- // Telemetry logging
667- entityCtx := engcontext .EntityFromContext (ctx )
668- logger .BusinessRecord (ctx ).Project = entityCtx .Project .ID
669- logger .BusinessRecord (ctx ).Profile = logger.Profile {Name : profileName , ID : profileID }
670-
671663 return & minderv1.GetProfileStatusByNameResponse {
672664 ProfileStatus : & minderv1.ProfileStatus {
673665 ProfileId : profileID .String (),
@@ -689,22 +681,30 @@ func (s *Server) processProfileStatusById(
689681) (* minderv1.GetProfileStatusByIdResponse , error ) {
690682 var ruleEvaluationStatuses []* minderv1.RuleEvaluationStatus
691683
692- selector , ruleType , ruleName , err := extractFiltersFromIdRequest (req )
693- if err != nil {
684+ // Telemetry logging
685+ entityCtx := engcontext .EntityFromContext (ctx )
686+ logger .BusinessRecord (ctx ).Project = entityCtx .Project .ID
687+ logger .BusinessRecord (ctx ).Profile = logger.Profile {Name : profileName , ID : profileID }
688+
689+ // selector, ruleType, ruleName, err := extractFiltersFromIdRequest(req)
690+ if err := validateEntityType (req .GetEntity ()); err != nil {
694691 return nil , err
695692 }
693+ maybeEntityID , err := maybeNullUUID (req .GetEntity ().GetId ())
694+ if err != nil {
695+ return nil , util .UserVisibleError (codes .InvalidArgument , "Unable to parse entity id: %q" , req .GetEntity ().GetId ())
696+ }
697+ maybeEntityName := maybeNullString (req .GetEntity ().GetName ())
698+ ruleType := maybeNullString (req .GetRuleType ())
699+ ruleName := maybeNullString (req .GetRuleName ())
696700
697- // Only fetch rule evaluations if selector is present or all is requested
698- if selector != nil || req .GetAll () {
699- var entityID uuid.NullUUID
700- if selector != nil {
701- entityID = * selector
702- }
701+ if req .GetAll () || maybeEntityID .Valid || maybeEntityName .Valid {
703702 dbRuleEvaluationStatuses , err := s .store .ListRuleEvaluationsByProfileId (ctx , db.ListRuleEvaluationsByProfileIdParams {
704703 ProfileID : profileID ,
705- EntityID : entityID ,
706- RuleTypeName : * ruleType ,
707- RuleName : * ruleName ,
704+ EntityID : maybeEntityID ,
705+ EntityName : maybeEntityName ,
706+ RuleTypeName : ruleType ,
707+ RuleName : ruleName ,
708708 })
709709 if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
710710 return nil , status .Errorf (codes .Unknown , "failed to list rule evaluation status: %s" , err )
@@ -715,11 +715,6 @@ func (s *Server) processProfileStatusById(
715715 )
716716 }
717717
718- // Telemetry logging
719- entityCtx := engcontext .EntityFromContext (ctx )
720- logger .BusinessRecord (ctx ).Project = entityCtx .Project .ID
721- logger .BusinessRecord (ctx ).Profile = logger.Profile {Name : profileName , ID : profileID }
722-
723718 return & minderv1.GetProfileStatusByIdResponse {
724719 ProfileStatus : & minderv1.ProfileStatus {
725720 ProfileId : profileID .String (),
@@ -731,61 +726,32 @@ func (s *Server) processProfileStatusById(
731726 }, nil
732727}
733728
734- func extractFiltersFromNameRequest (
735- req * minderv1.GetProfileStatusByNameRequest ) (
736- * uuid.NullUUID , * sql.NullString , * sql.NullString , error ) {
737- if e := req .GetEntity (); e != nil {
729+ func validateEntityType (e * minderv1.EntityTypedId ) error {
730+ if e != nil {
738731 if ! e .GetType ().IsValid () {
739- return nil , nil , nil , util .UserVisibleError (codes .InvalidArgument ,
732+ return util .UserVisibleError (codes .InvalidArgument ,
740733 "invalid entity type %s, please use one of %s" ,
741734 e .GetType (), entities .KnownTypesCSV ())
742735 }
743736 }
744-
745- selector := extractEntitySelector (req .GetEntity ())
746-
747- ruleType := & sql.NullString {
748- String : req .GetRuleType (),
749- Valid : req .GetRuleType () != "" ,
750- }
751- if ! ruleType .Valid {
752- //nolint:staticcheck // ignore SA1019: Deprecated field supported for backward compatibility
753- ruleType = & sql.NullString {
754- String : req .GetRule (),
755- Valid : req .GetRule () != "" ,
756- }
757- }
758-
759- ruleName := & sql.NullString {
760- String : req .GetRuleName (),
761- Valid : req .GetRuleName () != "" ,
762- }
763-
764- return selector , ruleType , ruleName , nil
737+ return nil
765738}
766739
767- func extractFiltersFromIdRequest (
768- req * minderv1.GetProfileStatusByIdRequest ) (
769- * uuid.NullUUID , * sql.NullString , * sql.NullString , error ) {
770- if e := req .GetEntity (); e != nil {
771- if ! e .GetType ().IsValid () {
772- return nil , nil , nil , util .UserVisibleError (codes .InvalidArgument ,
773- "invalid entity type %s, please use one of %s" ,
774- e .GetType (), entities .KnownTypesCSV ())
775- }
776- }
777-
778- selector := extractEntitySelector (req .GetEntity ())
779-
780- ruleType := & sql.NullString {
781- String : req .GetRuleType (),
782- Valid : req .GetRuleType () != "" ,
740+ func maybeNullString (s string ) sql.NullString {
741+ return sql.NullString {
742+ String : s ,
743+ Valid : s != "" ,
783744 }
745+ }
784746
785- ruleName := & sql.NullString {
786- String : req .GetRuleName (),
787- Valid : req .GetRuleName () != "" ,
747+ func maybeNullUUID (s string ) (uuid.NullUUID , error ) {
748+ var id uuid.UUID
749+ var err error
750+ if s != "" {
751+ id , err = uuid .Parse (s )
788752 }
789-
790- return selector , ruleType , ruleName , nil
753+ return uuid.NullUUID {
754+ UUID : id ,
755+ Valid : s != "" ,
756+ }, err
791757}
0 commit comments