Skip to content

Commit

Permalink
Add and implement ConfigureTypeErrorDiagnostic
Browse files Browse the repository at this point in the history
Adds and implements a new helper to call when there's an unexpected type
during configuration of a data source or resource.
  • Loading branch information
mitchnielsen committed Jun 13, 2024
1 parent d0b5fb8 commit f11c594
Show file tree
Hide file tree
Showing 24 changed files with 45 additions and 93 deletions.
5 changes: 1 addition & 4 deletions internal/provider/datasources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func (d *AccountDataSource) Configure(_ context.Context, req datasource.Configur

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/datasources/account_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ func (d *AccountMemberDataSource) Configure(_ context.Context, req datasource.Co

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/datasources/account_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ func (d *AccountMembersDataSource) Configure(_ context.Context, req datasource.C

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/datasources/account_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ func (d *AccountRoleDataSource) Configure(_ context.Context, req datasource.Conf

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/datasources/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ func (d *blockDataSource) Configure(_ context.Context, req datasource.ConfigureR

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var _ = datasource.DataSourceWithConfigure(&ServiceAccountDataSource{})
Expand Down Expand Up @@ -58,10 +59,7 @@ func (d *ServiceAccountDataSource) Configure(_ context.Context, req datasource.C

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/datasources/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ func (d *TeamDataSource) Configure(_ context.Context, req datasource.ConfigureRe

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var _ = datasource.DataSourceWithConfigure(&TeamsDataSource{})
Expand Down Expand Up @@ -47,10 +48,7 @@ func (d *TeamsDataSource) Configure(_ context.Context, req datasource.ConfigureR

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var _ = datasource.DataSourceWithConfigure(&VariableDataSource{})
Expand Down Expand Up @@ -52,10 +53,7 @@ func (d *VariableDataSource) Configure(_ context.Context, req datasource.Configu

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var _ = datasource.DataSourceWithConfigure(&WorkPoolDataSource{})
Expand Down Expand Up @@ -59,10 +60,7 @@ func (d *WorkPoolDataSource) Configure(_ context.Context, req datasource.Configu

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/work_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var _ = datasource.DataSourceWithConfigure(&WorkPoolsDataSource{})
Expand Down Expand Up @@ -52,10 +53,7 @@ func (d *WorkPoolsDataSource) Configure(_ context.Context, req datasource.Config

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/datasources/worker_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ func (d *WorkerMetadataDataSource) Configure(_ context.Context, req datasource.C

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var _ = datasource.DataSourceWithConfigure(&WorkspaceDataSource{})
Expand Down Expand Up @@ -53,10 +54,7 @@ func (d *WorkspaceDataSource) Configure(_ context.Context, req datasource.Config

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/datasources/workspace_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -106,10 +107,7 @@ func (d *WorkspaceRoleDataSource) Configure(_ context.Context, req datasource.Co

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("data source", req.ProviderData))

return
}
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/helpers/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ func ResourceClientErrorDiagnostic(resourceName string, operation string, err er
fmt.Sprintf("Could not %s %s, unexpected error: %s", operation, resourceName, err.Error()),
)
}

// ConfigureTypeErrorDiagnostic returns an error diagnostic for when a
// given type does not implement PrefectClient.
//
//nolint:ireturn // required by Terraform API
func ConfigureTypeErrorDiagnostic(componentKind string, data any) diag.Diagnostic {
return diag.NewErrorDiagnostic(
fmt.Sprintf("Unexpected %s Configure type", componentKind),
fmt.Sprintf("Expected api.PrefectClient type, got %T. %s", data, reportMessage),
)
}
5 changes: 1 addition & 4 deletions internal/provider/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func (r *AccountResource) Configure(_ context.Context, req resource.ConfigureReq

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/resources/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ func (r *BlockResource) Configure(_ context.Context, req resource.ConfigureReque

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
6 changes: 1 addition & 5 deletions internal/provider/resources/block_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resources

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -49,10 +48,7 @@ func (r *BlockAccessResource) Configure(_ context.Context, req resource.Configur

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/resources/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ func (r *ServiceAccountResource) Configure(_ context.Context, req resource.Confi

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/resources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var (
Expand Down Expand Up @@ -64,10 +65,7 @@ func (r *VariableResource) Configure(_ context.Context, req resource.ConfigureRe

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/resources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var (
Expand Down Expand Up @@ -68,10 +69,7 @@ func (r *WorkPoolResource) Configure(_ context.Context, req resource.ConfigureRe

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/resources/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var (
Expand Down Expand Up @@ -60,10 +61,7 @@ func (r *WorkspaceResource) Configure(_ context.Context, req resource.ConfigureR

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/resources/workspace_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func (r *WorkspaceAccessResource) Configure(_ context.Context, req resource.Conf

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/resources/workspace_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers"
)

var (
Expand Down Expand Up @@ -60,10 +61,7 @@ func (r *WorkspaceRoleResource) Configure(_ context.Context, req resource.Config

client, ok := req.ProviderData.(api.PrefectClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected api.PrefectClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
resp.Diagnostics.Append(helpers.ConfigureTypeErrorDiagnostic("resource", req.ProviderData))

return
}
Expand Down

0 comments on commit f11c594

Please sign in to comment.