@@ -879,6 +879,16 @@ func (f *Function) validateAndPrepareInput(_ context.Context, req *fnv1.RunFunct
879879 return false
880880 }
881881
882+ // Process references based on query type
883+ if ! f .processReferences (req , in , rsp ) {
884+ return false
885+ }
886+
887+ return true
888+ }
889+
890+ // processReferences handles resolving references like groupRef and groupsRef
891+ func (f * Function ) processReferences (req * fnv1.RunFunctionRequest , in * v1beta1.Input , rsp * fnv1.RunFunctionResponse ) bool {
882892 // Process groupRef if it exists for GroupMembership query type
883893 if in .QueryType == "GroupMembership" && in .GroupRef != nil && * in .GroupRef != "" {
884894 groupName , err := f .resolveGroupRef (req , in .GroupRef )
@@ -890,6 +900,17 @@ func (f *Function) validateAndPrepareInput(_ context.Context, req *fnv1.RunFunct
890900 f .log .Info ("Resolved GroupRef to group" , "group" , groupName , "groupRef" , * in .GroupRef )
891901 }
892902
903+ // Process groupsRef if it exists for GroupObjectIDs query type
904+ if in .QueryType == "GroupObjectIDs" && in .GroupsRef != nil && * in .GroupsRef != "" {
905+ groupNames , err := f .resolveGroupsRef (req , in .GroupsRef )
906+ if err != nil {
907+ response .Fatal (rsp , err )
908+ return false
909+ }
910+ in .Groups = groupNames
911+ f .log .Info ("Resolved GroupsRef to groups" , "groupCount" , len (groupNames ), "groupsRef" , * in .GroupsRef )
912+ }
913+
893914 return true
894915}
895916
@@ -996,3 +1017,77 @@ func (f *Function) resolveFromContext(req *fnv1.RunFunctionRequest, refKey strin
9961017 }
9971018 return value , nil
9981019}
1020+
1021+ // resolveGroupsRef resolves a list of group names from a reference in status or context.
1022+ func (f * Function ) resolveGroupsRef (req * fnv1.RunFunctionRequest , groupsRef * string ) ([]* string , error ) {
1023+ if groupsRef == nil || * groupsRef == "" {
1024+ return nil , errors .New ("empty groupsRef provided" )
1025+ }
1026+
1027+ refKey := * groupsRef
1028+
1029+ // Use proper switch statement instead of if-else chain
1030+ switch {
1031+ case strings .HasPrefix (refKey , "status." ):
1032+ return f .resolveGroupsFromStatus (req , refKey )
1033+ case strings .HasPrefix (refKey , "context." ):
1034+ return f .resolveGroupsFromContext (req , refKey )
1035+ default :
1036+ return nil , errors .Errorf ("unsupported groupsRef format: %s" , refKey )
1037+ }
1038+ }
1039+
1040+ // resolveGroupsFromStatus resolves a list of group names from XR status
1041+ func (f * Function ) resolveGroupsFromStatus (req * fnv1.RunFunctionRequest , refKey string ) ([]* string , error ) {
1042+ xrStatus , _ , err := f .getXRAndStatus (req )
1043+ if err != nil {
1044+ return nil , errors .Wrap (err , "cannot get XR status" )
1045+ }
1046+
1047+ statusField := strings .TrimPrefix (refKey , "status." )
1048+ return f .extractStringArrayFromMap (xrStatus , statusField , refKey )
1049+ }
1050+
1051+ // resolveGroupsFromContext resolves a list of group names from function context
1052+ func (f * Function ) resolveGroupsFromContext (req * fnv1.RunFunctionRequest , refKey string ) ([]* string , error ) {
1053+ contextMap := req .GetContext ().AsMap ()
1054+ contextField := strings .TrimPrefix (refKey , "context." )
1055+ return f .extractStringArrayFromMap (contextMap , contextField , refKey )
1056+ }
1057+
1058+ // extractStringArrayFromMap extracts a string array from a map using nested key
1059+ func (f * Function ) extractStringArrayFromMap (dataMap map [string ]interface {}, field , refKey string ) ([]* string , error ) {
1060+ parts , err := ParseNestedKey (field )
1061+ if err != nil {
1062+ return nil , errors .Wrap (err , "invalid field key" )
1063+ }
1064+
1065+ currentValue := interface {}(dataMap )
1066+ for _ , k := range parts {
1067+ if nestedMap , ok := currentValue .(map [string ]interface {}); ok {
1068+ if nextValue , exists := nestedMap [k ]; exists {
1069+ currentValue = nextValue
1070+ } else {
1071+ return nil , errors .Errorf ("cannot resolve groupsRef: %s not found" , refKey )
1072+ }
1073+ } else {
1074+ return nil , errors .Errorf ("cannot resolve groupsRef: %s not a map" , refKey )
1075+ }
1076+ }
1077+
1078+ // The current value should be a slice of strings
1079+ if strArray , ok := currentValue .([]interface {}); ok {
1080+ result := make ([]* string , 0 , len (strArray ))
1081+ for _ , val := range strArray {
1082+ if strVal , ok := val .(string ); ok {
1083+ strCopy := strVal // Create a new string to avoid pointing to a loop variable
1084+ result = append (result , & strCopy )
1085+ }
1086+ }
1087+ if len (result ) > 0 {
1088+ return result , nil
1089+ }
1090+ }
1091+
1092+ return nil , errors .Errorf ("cannot resolve groupsRef: %s not a string array or empty" , refKey )
1093+ }
0 commit comments