Skip to content

Commit

Permalink
Merge pull request #20 from deploymenttheory/feature-scaffolding
Browse files Browse the repository at this point in the history
added debugging for provider
  • Loading branch information
ShocOne authored Jul 25, 2024
2 parents fab4be7 + 5db2902 commit c65d713
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
26 changes: 21 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type M365ProviderModel struct {
Cloud types.String `tfsdk:"cloud"`
EnableChaos types.Bool `tfsdk:"enable_chaos"`
TelemetryOptout types.Bool `tfsdk:"telemetry_optout"`
Debug types.Bool `tfsdk:"debug"`
}

func (p *M365Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand Down Expand Up @@ -182,6 +183,10 @@ func (p *M365Provider) Schema(ctx context.Context, req provider.SchemaRequest, r
"Can also be set using the `M365_TELEMETRY_OPTOUT` environment variable.",
Optional: true,
},
"debug": schema.BoolAttribute{
Optional: true,
Description: "Enable debug mode for the provider.",
},
},
}
}
Expand Down Expand Up @@ -214,13 +219,14 @@ func (p *M365Provider) Schema(ctx context.Context, req provider.SchemaRequest, r
// If any errors occur during these steps, appropriate diagnostics are added
// to the response.
func (p *M365Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data M365ProviderModel

tflog.Debug(ctx, "Configure request received")
tflog.Info(ctx, "Configuring Microsoft365 Provider")

var data M365ProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Error getting provider configuration", map[string]interface{}{
"diagnostics": resp.Diagnostics.ErrorsCount(),
})
return
}

Expand Down Expand Up @@ -369,14 +375,24 @@ func (p *M365Provider) Configure(ctx context.Context, req provider.ConfigureRequ

resp.DataSourceData = clients
resp.ResourceData = clients

tflog.Debug(ctx, "Provider configuration completed", map[string]interface{}{
"stable_client_set": p.clients.StableClient != nil,
"beta_client_set": p.clients.BetaClient != nil,
})
}

// New returns a new provider.Provider instance for the Microsoft365 provider.
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &M365Provider{
tflog.Info(context.Background(), "Initializing Microsoft365 Provider")
p := &M365Provider{
version: version,
clients: &client.GraphClients{},
}
tflog.Debug(context.Background(), "Created new provider instance", map[string]interface{}{
"provider": fmt.Sprintf("%+v", p),
})
return p
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,33 @@ 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 {
resp.Diagnostics.AddError(
"Provider Data is nil",
"Cannot initialize the client because the provider data is nil. This likely indicates a configuration error.",
)
tflog.Warn(ctx, "Provider data is nil, skipping resource configuration")
return
}

providerData, ok := req.ProviderData.(*client.GraphClients)
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 *GraphClients, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *client.GraphClients, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

r.client = providerData.BetaClient
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 BetaClient")
}

// ImportState imports the resource state.
Expand Down
20 changes: 17 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"context"
"flag"
"log"

"github.com/deploymenttheory/terraform-provider-microsoft365/internal/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

var (
Expand All @@ -23,14 +23,28 @@ func main() {
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

ctx := context.Background()

tflog.Info(ctx, "Starting Microsoft365 provider", map[string]interface{}{
"version": version,
"commit": commit,
})

opts := providerserver.ServeOpts{
Address: "registry.terraform.io/deploymenttheory/microsoft365",
Debug: debug,
}

err := providerserver.Serve(context.Background(), provider.New(version), opts)
tflog.Debug(ctx, "Provider serve options", map[string]interface{}{
"address": opts.Address,
"debug": opts.Debug,
})

err := providerserver.Serve(ctx, provider.New(version), opts)

if err != nil {
log.Fatal(err.Error())
tflog.Error(ctx, "Provider server error", map[string]interface{}{
"error": err.Error(),
})
}
}

0 comments on commit c65d713

Please sign in to comment.