From e9d9754bb3c55e4bbd6bc936879d63a04ed9603a Mon Sep 17 00:00:00 2001 From: ShocOne Date: Thu, 8 Aug 2024 16:45:09 +0100 Subject: [PATCH 1/2] centralized graph and graph beta client configure functions with helper --- internal/resources/common/resource.go | 75 +++++++++++++++++++ .../beta/assignmentFilter/resource.go | 31 +------- .../cloudPcProvisioningPolicy/resource.go | 33 +------- .../beta/conditionalaccesspolicy/resource.go | 32 +------- 4 files changed, 82 insertions(+), 89 deletions(-) create mode 100644 internal/resources/common/resource.go diff --git a/internal/resources/common/resource.go b/internal/resources/common/resource.go new file mode 100644 index 00000000..6e3bbc20 --- /dev/null +++ b/internal/resources/common/resource.go @@ -0,0 +1,75 @@ +package common + +import ( + "context" + "fmt" + + "github.com/deploymenttheory/terraform-provider-microsoft365/internal/client" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-log/tflog" + + msgraphbetasdk "github.com/microsoftgraph/msgraph-beta-sdk-go" + msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" +) + +// 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 +} + +// SetGraphBetaClient is a helper function to configure Graph Beta client. +func SetGraphBetaClient(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse, resourceName string) *msgraphbetasdk.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.BetaClient == nil { + tflog.Warn(ctx, fmt.Sprintf("BetaClient is nil, %s resource may not be fully configured", resourceName)) + return nil + } + + tflog.Debug(ctx, fmt.Sprintf("Initialized %s Resource with Graph Beta Client", resourceName)) + return clients.BetaClient +} diff --git a/internal/resources/deviceandappmanagement/beta/assignmentFilter/resource.go b/internal/resources/deviceandappmanagement/beta/assignmentFilter/resource.go index 4d10e21c..83dff3e8 100644 --- a/internal/resources/deviceandappmanagement/beta/assignmentFilter/resource.go +++ b/internal/resources/deviceandappmanagement/beta/assignmentFilter/resource.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/deploymenttheory/terraform-provider-microsoft365/internal/client" + "github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common" "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-log/tflog" msgraphbetasdk "github.com/microsoftgraph/msgraph-beta-sdk-go" ) @@ -49,33 +48,7 @@ func (r *AssignmentFilterResource) Metadata(ctx context.Context, req resource.Me // Configure sets the client for the resource. func (r *AssignmentFilterResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - tflog.Debug(ctx, "Configuring AssignmentFilterResource") - - if req.ProviderData == nil { - tflog.Warn(ctx, "Provider data is nil, skipping resource configuration") - return - } - - 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 - } - - if clients.BetaClient == nil { - tflog.Warn(ctx, "BetaClient is nil, resource may not be fully configured") - return - } - - r.client = clients.BetaClient - tflog.Debug(ctx, "Initialized graphBetaAssignmentFilter resource with Graph Beta Client") + r.client = common.SetGraphBetaClient(ctx, req, resp, r.TypeName) } // ImportState imports the resource state. diff --git a/internal/resources/devicemanagement/v1.0/cloudPcProvisioningPolicy/resource.go b/internal/resources/devicemanagement/v1.0/cloudPcProvisioningPolicy/resource.go index d8cf7651..55f7255c 100644 --- a/internal/resources/devicemanagement/v1.0/cloudPcProvisioningPolicy/resource.go +++ b/internal/resources/devicemanagement/v1.0/cloudPcProvisioningPolicy/resource.go @@ -2,9 +2,9 @@ package graphCloudPcProvisioningPolicy import ( "context" - "fmt" - "github.com/deploymenttheory/terraform-provider-microsoft365/internal/client" + "github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -12,7 +12,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-log/tflog" msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" ) @@ -52,33 +51,7 @@ func (r *CloudPcProvisioningPolicyResource) Metadata(ctx context.Context, req re // Configure sets the client for the resource. func (r *CloudPcProvisioningPolicyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - tflog.Debug(ctx, "Configuring CloudPcProvisioningPolicyResource") - - if req.ProviderData == nil { - tflog.Warn(ctx, "Provider data is nil, skipping resource configuration") - return - } - - 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 - } - - if clients.StableClient == nil { - tflog.Warn(ctx, "StableClient is nil, resource may not be fully configured") - return - } - - r.client = clients.StableClient - tflog.Debug(ctx, "Initialized graphCloudPcProvisioningPolicy resource with Graph Client") + r.client = common.SetGraphStableClient(ctx, req, resp, "CloudPcProvisioningPolicyResource") } // ImportState imports the resource state. diff --git a/internal/resources/identityandaccess/beta/conditionalaccesspolicy/resource.go b/internal/resources/identityandaccess/beta/conditionalaccesspolicy/resource.go index acecc656..59eccc0a 100644 --- a/internal/resources/identityandaccess/beta/conditionalaccesspolicy/resource.go +++ b/internal/resources/identityandaccess/beta/conditionalaccesspolicy/resource.go @@ -2,10 +2,9 @@ package graphBetaConditionalAccessPolicy import ( "context" - "fmt" "regexp" - "github.com/deploymenttheory/terraform-provider-microsoft365/internal/client" + "github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common" "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator" @@ -15,7 +14,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-log/tflog" msgraphbetasdk "github.com/microsoftgraph/msgraph-beta-sdk-go" ) @@ -51,33 +49,7 @@ func (r *ConditionalAccessPolicyResource) Metadata(ctx context.Context, req reso // Configure sets the client for the resource. func (r *ConditionalAccessPolicyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - tflog.Debug(ctx, "Configuring ConditionalAccessPolicyResource") - - if req.ProviderData == nil { - tflog.Warn(ctx, "Provider data is nil, skipping resource configuration") - return - } - - 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 - } - - if clients.BetaClient == nil { - tflog.Warn(ctx, "BetaClient is nil, resource may not be fully configured") - return - } - - r.client = clients.BetaClient - tflog.Debug(ctx, "Initialized graphBetaAssignmentFilter resource with Graph Beta Client") + r.client = common.SetGraphBetaClient(ctx, req, resp, r.TypeName) } // ImportState imports the resource state. From c3ade91eb9eaea798f373279ab1fbedf30641ccd Mon Sep 17 00:00:00 2001 From: ShocOne Date: Thu, 8 Aug 2024 16:58:55 +0100 Subject: [PATCH 2/2] streamline graph client setter funcs --- internal/resources/common/resource.go | 48 +++++++++------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/internal/resources/common/resource.go b/internal/resources/common/resource.go index 6e3bbc20..7c4543e0 100644 --- a/internal/resources/common/resource.go +++ b/internal/resources/common/resource.go @@ -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 { @@ -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 }