Skip to content

Commit

Permalink
streamline graph client setter funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShocOne committed Aug 8, 2024
1 parent e9d9754 commit c3ade91
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions internal/resources/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,20 @@ import (

// SetGraphStableClient is a helper function to retrieve and validate the Graph Stable client from provider data.
func SetGraphStableClient(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse, resourceName string) *msgraphsdk.GraphServiceClient {
tflog.Debug(ctx, fmt.Sprintf("Configuring %s Resource", resourceName))

if req.ProviderData == nil {
tflog.Warn(ctx, fmt.Sprintf("Provider data is nil, skipping %s resource configuration", resourceName))
return nil
}

clients, ok := req.ProviderData.(*client.GraphClients)
if !ok {
tflog.Error(ctx, "Unexpected Provider Data Type", map[string]interface{}{
"expected": "*client.GraphClients",
"actual": fmt.Sprintf("%T", req.ProviderData),
})
resp.Diagnostics.AddError(
"Unexpected Provider Data Type",
fmt.Sprintf("Expected *client.GraphClients, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return nil
}

if clients.StableClient == nil {
tflog.Warn(ctx, fmt.Sprintf("StableClient is nil, %s resource may not be fully configured", resourceName))
return nil
}

tflog.Debug(ctx, fmt.Sprintf("Initialized %s Resource with Graph Stable Client", resourceName))
return clients.StableClient
return getClient(ctx, req, resp, resourceName, func(clients *client.GraphClients) *msgraphsdk.GraphServiceClient {
return clients.StableClient
})
}

// SetGraphBetaClient is a helper function to configure Graph Beta client.
// SetGraphBetaClient is a helper function to retrieve and validate the Graph Beta client from provider data.
func SetGraphBetaClient(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse, resourceName string) *msgraphbetasdk.GraphServiceClient {
return getClient(ctx, req, resp, resourceName, func(clients *client.GraphClients) *msgraphbetasdk.GraphServiceClient {
return clients.BetaClient
})
}

// getClient is a helper function to retrieve and validate the appropriate Graph client from provider data.
func getClient[T any](ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse, resourceName string, getClientFunc func(*client.GraphClients) *T) *T {
tflog.Debug(ctx, fmt.Sprintf("Configuring %s Resource", resourceName))

if req.ProviderData == nil {
Expand All @@ -65,11 +48,12 @@ func SetGraphBetaClient(ctx context.Context, req resource.ConfigureRequest, resp
return nil
}

if clients.BetaClient == nil {
tflog.Warn(ctx, fmt.Sprintf("BetaClient is nil, %s resource may not be fully configured", resourceName))
client := getClientFunc(clients)
if client == nil {
tflog.Warn(ctx, fmt.Sprintf("%s is nil, %s resource may not be fully configured", resourceName, resourceName))
return nil
}

tflog.Debug(ctx, fmt.Sprintf("Initialized %s Resource with Graph Beta Client", resourceName))
return clients.BetaClient
tflog.Debug(ctx, fmt.Sprintf("Initialized %s Resource with Graph Client", resourceName))
return client
}

0 comments on commit c3ade91

Please sign in to comment.