Skip to content

Commit 7eeb4c5

Browse files
tvandintherytsarev
andauthored
Add support for empty input lists (#68)
* Add `FailOnEmpty` option to input * Add fail on empty option to error condition * Initialise empty results slice With this change, if no groups are queried, an empty list is returned instead of null. Signed-off-by: Tom van Dinther <[email protected]> * Add empty cases for ref resolution tests Also updates the mock to contain the same logic as added in the real implementation. Signed-off-by: Tom van Dinther <[email protected]> * Add test cases for `failOnEmpty` permutations Signed-off-by: Tom van Dinther <[email protected]> * Add FailOnEmpty option to function configuration options in README Signed-off-by: Tom van Dinther <[email protected]> --------- Signed-off-by: Tom van Dinther <[email protected]> Co-authored-by: Yury Tsarev <[email protected]>
1 parent 078b3f0 commit 7eeb4c5

File tree

4 files changed

+636
-34
lines changed

4 files changed

+636
-34
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ spec:
275275
| `servicePrincipalsRef` | string | Reference to resolve a list of service principal names from `spec`, `status` or `context` (e.g., `spec.servicePrincipalConfig.names`) |
276276
| `target` | string | Required. Where to store the query results. Can be `status.<field>` or `context.<field>` |
277277
| `skipQueryWhenTargetHasData` | bool | Optional. When true, will skip the query if the target already has data |
278-
| `identity.type | string | Optional. Type of identity credentials to use. Valid values: `AzureServicePrincipalCredentials`, `AzureWorkloadIdentityCredentials`. Default is `AzureServicePrincipalCredentials` |
278+
| `FailOnEmpty` | bool | Optional. When true, the function will fail if the `users`, `groups`, or `servicePrincipals` lists are empty, or if their respective reference fields are empty lists. |
279+
| `identity.type` | string | Optional. Type of identity credentials to use. Valid values: `AzureServicePrincipalCredentials`, `AzureWorkloadIdentityCredentials`. Default is `AzureServicePrincipalCredentials` |
279280

280281
## Result Targets
281282

fn.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,11 @@ func (g *GraphQuery) graphQuery(ctx context.Context, azureCreds map[string]strin
497497

498498
// validateUsers validates if the provided user principal names (emails) exist
499499
func (g *GraphQuery) validateUsers(ctx context.Context, client *msgraphsdk.GraphServiceClient, in *v1beta1.Input) (interface{}, error) {
500-
if len(in.Users) == 0 {
500+
if in.FailOnEmpty != nil && *in.FailOnEmpty && len(in.Users) == 0 {
501501
return nil, errors.New("no users provided for validation")
502502
}
503503

504-
var results []interface{}
504+
results := make([]interface{}, 0)
505505

506506
for _, userPrincipalName := range in.Users {
507507
if userPrincipalName == nil {
@@ -754,11 +754,11 @@ func (g *GraphQuery) getGroupMembers(ctx context.Context, client *msgraphsdk.Gra
754754

755755
// getGroupObjectIDs retrieves object IDs for the specified group names
756756
func (g *GraphQuery) getGroupObjectIDs(ctx context.Context, client *msgraphsdk.GraphServiceClient, in *v1beta1.Input) (interface{}, error) {
757-
if len(in.Groups) == 0 {
757+
if in.FailOnEmpty != nil && *in.FailOnEmpty && len(in.Groups) == 0 {
758758
return nil, errors.New("no group names provided")
759759
}
760760

761-
var results []interface{}
761+
results := make([]interface{}, 0)
762762

763763
for _, groupName := range in.Groups {
764764
if groupName == nil {
@@ -799,11 +799,11 @@ func (g *GraphQuery) getGroupObjectIDs(ctx context.Context, client *msgraphsdk.G
799799

800800
// getServicePrincipalDetails retrieves details about service principals by name
801801
func (g *GraphQuery) getServicePrincipalDetails(ctx context.Context, client *msgraphsdk.GraphServiceClient, in *v1beta1.Input) (interface{}, error) {
802-
if len(in.ServicePrincipals) == 0 {
802+
if in.FailOnEmpty != nil && *in.FailOnEmpty && len(in.ServicePrincipals) == 0 {
803803
return nil, errors.New("no service principal names provided")
804804
}
805805

806-
var results []interface{}
806+
results := make([]interface{}, 0)
807807

808808
for _, spName := range in.ServicePrincipals {
809809
if spName == nil {
@@ -1515,10 +1515,8 @@ func (f *Function) extractStringArrayFromMap(dataMap map[string]interface{}, fie
15151515
result = append(result, &strCopy)
15161516
}
15171517
}
1518-
if len(result) > 0 {
1519-
return result, nil
1520-
}
1518+
return result, nil
15211519
}
15221520

1523-
return nil, errors.Errorf("cannot resolve groupsRef: %s not a string array or empty", refKey)
1521+
return nil, errors.Errorf("cannot resolve groupsRef: %s not a string array", refKey)
15241522
}

0 commit comments

Comments
 (0)