Skip to content

Commit

Permalink
Merge pull request #51 from deploymenttheory/feature-graph_beta-condi…
Browse files Browse the repository at this point in the history
…tional_access

centralized graph and graph beta client configure functions with helper
  • Loading branch information
ShocOne authored Aug 8, 2024
2 parents 0c5d6e9 + c3ade91 commit 7fdd6d9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 89 deletions.
59 changes: 59 additions & 0 deletions internal/resources/common/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 {
return getClient(ctx, req, resp, resourceName, func(clients *client.GraphClients) *msgraphsdk.GraphServiceClient {
return clients.StableClient
})
}

// 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 {
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
}

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 Client", resourceName))
return client
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ 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"
"github.com/hashicorp/terraform-plugin-framework/resource"
"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"
)
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ 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"
"github.com/hashicorp/terraform-plugin-framework/resource"
"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"
)
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7fdd6d9

Please sign in to comment.