diff --git a/fn.go b/fn.go index e584700..ddb5150 100644 --- a/fn.go +++ b/fn.go @@ -17,6 +17,7 @@ import ( "github.com/microsoftgraph/msgraph-sdk-go/users" "github.com/upbound/function-msgraph/input/v1beta1" "google.golang.org/protobuf/types/known/structpb" + "k8s.io/utils/ptr" "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/logging" @@ -349,10 +350,10 @@ func (g *GraphQuery) validateUsers(ctx context.Context, client *msgraphsdk.Graph if result.GetValue() != nil { for _, user := range result.GetValue() { userMap := map[string]interface{}{ - "id": user.GetId(), - "displayName": user.GetDisplayName(), - "userPrincipalName": user.GetUserPrincipalName(), - "mail": user.GetMail(), + "id": ptr.Deref(user.GetId(), ""), + "displayName": ptr.Deref(user.GetDisplayName(), ""), + "userPrincipalName": ptr.Deref(user.GetUserPrincipalName(), ""), + "mail": ptr.Deref(user.GetMail(), ""), } results = append(results, userMap) } @@ -488,7 +489,7 @@ func (g *GraphQuery) processMember(member models.DirectoryObjectable) map[string unknownType = "unknown" ) - memberID := member.GetId() + memberID := ptr.Deref(member.GetId(), "") additionalData := member.GetAdditionalData() // Create basic member info @@ -524,7 +525,7 @@ func (g *GraphQuery) processMember(member models.DirectoryObjectable) map[string memberMap["type"] = memberType // Extract display name - memberMap["displayName"] = g.extractDisplayName(member, *memberID) + memberMap["displayName"] = g.extractDisplayName(member, memberID) // Extract type-specific properties switch memberType { @@ -604,9 +605,9 @@ func (g *GraphQuery) getGroupObjectIDs(ctx context.Context, client *msgraphsdk.G if groupResult.GetValue() != nil && len(groupResult.GetValue()) > 0 { for _, group := range groupResult.GetValue() { groupMap := map[string]interface{}{ - "id": group.GetId(), - "displayName": group.GetDisplayName(), - "description": group.GetDescription(), + "id": ptr.Deref(group.GetId(), ""), + "displayName": ptr.Deref(group.GetDisplayName(), ""), + "description": ptr.Deref(group.GetDescription(), ""), } results = append(results, groupMap) } @@ -649,10 +650,10 @@ func (g *GraphQuery) getServicePrincipalDetails(ctx context.Context, client *msg if spResult.GetValue() != nil && len(spResult.GetValue()) > 0 { for _, sp := range spResult.GetValue() { spMap := map[string]interface{}{ - "id": sp.GetId(), - "appId": sp.GetAppId(), - "displayName": sp.GetDisplayName(), - "description": sp.GetDescription(), + "id": ptr.Deref(sp.GetId(), ""), + "appId": ptr.Deref(sp.GetAppId(), ""), + "displayName": ptr.Deref(sp.GetDisplayName(), ""), + "description": ptr.Deref(sp.GetDescription(), ""), } results = append(results, spMap) } diff --git a/fn_test.go b/fn_test.go index e8eb210..cb4b494 100644 --- a/fn_test.go +++ b/fn_test.go @@ -10,6 +10,7 @@ import ( "github.com/upbound/function-msgraph/input/v1beta1" "google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/types/known/durationpb" + "k8s.io/utils/ptr" "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/logging" @@ -26,10 +27,6 @@ func (m *MockGraphQuery) graphQuery(ctx context.Context, azureCreds map[string]s return m.GraphQueryFunc(ctx, azureCreds, in) } -func strPtr(s string) *string { - return &s -} - // TestResolveGroupsRef tests the functionality of resolving groupsRef from context, status, or spec func TestResolveGroupsRef(t *testing.T) { var ( @@ -2312,7 +2309,7 @@ func TestRunFunction(t *testing.T) { Conditions: []*fnv1.Condition{ { Type: "FunctionSkip", - Message: strPtr("Target already has data, skipped query to avoid throttling"), + Message: ptr.To("Target already has data, skipped query to avoid throttling"), Status: fnv1.Status_STATUS_CONDITION_TRUE, Reason: "SkippedQuery", Target: fnv1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum(), diff --git a/go.mod b/go.mod index f91bbc6..b5ea41c 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/microsoftgraph/msgraph-sdk-go v1.84.0 google.golang.org/protobuf v1.36.8 k8s.io/apimachinery v0.33.4 + k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 sigs.k8s.io/controller-tools v0.18.0 ) @@ -93,7 +94,6 @@ require ( k8s.io/gengo/v2 v2.0.0-20250207200755-1244d31929d7 // indirect k8s.io/klog/v2 v2.130.1 // indirect k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect - k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect sigs.k8s.io/controller-runtime v0.19.0 // indirect sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect sigs.k8s.io/randfill v1.0.0 // indirect