Skip to content

Commit

Permalink
Improve Diagnostics consistency (#213)
Browse files Browse the repository at this point in the history
* Use a constant for the report message
* Add and implement ConfigureTypeErrorDiagnostic
* Use ResourceClientErrorDiagnostic consistently
* Use CreateClientErrorDiagnostic consistently
* Implement SerializeDataErrorDiagnostic helper
* Implement ParseUUIDErrorDiagnostic helper

Closes #212
  • Loading branch information
mitchnielsen authored Jun 18, 2024
1 parent ad98dc5 commit 5ff5236
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 505 deletions.
11 changes: 2 additions & 9 deletions internal/provider/datasources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datasources

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -54,10 +53,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 Expand Up @@ -141,10 +137,7 @@ func (d *AccountDataSource) Read(ctx context.Context, req datasource.ReadRequest

account, err := client.Get(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing account state",
fmt.Sprintf("Could not read account, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account", "get", err))

return
}
Expand Down
12 changes: 4 additions & 8 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 Expand Up @@ -158,10 +155,9 @@ func (d *AccountMemberDataSource) Read(ctx context.Context, req datasource.ReadR
// workspaceRoles, err := client.List(ctx, []string{model.Name.ValueString()})
accountMembers, err := client.List(ctx, []string{config.Email.ValueString()})
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing Account Member state",
fmt.Sprintf("Could not search for Account Members, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account Member", "list", err))

return
}

if len(accountMembers) != 1 {
Expand Down
13 changes: 4 additions & 9 deletions internal/provider/datasources/account_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datasources

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -72,10 +71,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 Expand Up @@ -104,10 +100,9 @@ func (d *AccountMembersDataSource) Read(ctx context.Context, req datasource.Read
var filter []string
accountMembers, err := client.List(ctx, filter)
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing Account Members state",
fmt.Sprintf("Could not retrieve Account Members, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account Members", "list", err))

return
}

attributeTypes := map[string]attr.Type{
Expand Down
12 changes: 4 additions & 8 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 Expand Up @@ -137,10 +134,9 @@ func (d *AccountRoleDataSource) Read(ctx context.Context, req datasource.ReadReq
// as we are querying a single Role name, not a list of names
accountRoles, err := client.List(ctx, []string{config.Name.ValueString()})
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing Workspace Role state",
fmt.Sprintf("Could not read Workspace Role, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account Role", "list", err))

return
}

if len(accountRoles) != 1 {
Expand Down
18 changes: 3 additions & 15 deletions internal/provider/datasources/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package datasources
import (
"context"
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/prefecthq/terraform-provider-prefect/internal/api"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
Expand Down Expand Up @@ -144,10 +142,7 @@ func (d *blockDataSource) Read(ctx context.Context, req datasource.ReadRequest,
}

if err != nil {
resp.Diagnostics.AddError(
"Error refreshing block state",
fmt.Sprintf("Could not read block, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Block", "get", err))

return
}
Expand All @@ -161,11 +156,7 @@ func (d *blockDataSource) Read(ctx context.Context, req datasource.ReadRequest,

byteSlice, err := json.Marshal(block.Data)
if err != nil {
resp.Diagnostics.AddAttributeError(
path.Root("data"),
"Failed to serialize Block Data",
fmt.Sprintf("Could not serialize Block Data as JSON string: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.SerializeDataErrorDiagnostic("data", "Block Data", err))

return
}
Expand All @@ -186,10 +177,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
19 changes: 7 additions & 12 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 Expand Up @@ -162,21 +160,21 @@ func (d *ServiceAccountDataSource) Read(ctx context.Context, req datasource.Read

client, err := d.client.ServiceAccounts(model.AccountID.ValueUUID())
if err != nil {
resp.Diagnostics.AddError(
"Error creating variable client",
fmt.Sprintf("Could not create variable client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()),
)
resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Service Account", err))

return
}

// A Service Account can be read by either ID or Name.
// If both are set, we prefer the ID
var serviceAccount *api.ServiceAccount
var operation string
if !model.ID.IsNull() {
operation = "get"
serviceAccount, err = client.Get(ctx, model.ID.ValueString())
} else if !model.Name.IsNull() {
var serviceAccounts []*api.ServiceAccount
operation = "list"
serviceAccounts, err = client.List(ctx, []string{model.Name.ValueString()})

// The error from the API call should take precedence
Expand All @@ -200,10 +198,7 @@ func (d *ServiceAccountDataSource) Read(ctx context.Context, req datasource.Read
}

if err != nil {
resp.Diagnostics.AddError(
"Error refreshing Service Account state",
fmt.Sprintf("Could not read Service Account, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Service Account", operation, err))

return
}
Expand Down
12 changes: 4 additions & 8 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 Expand Up @@ -139,10 +136,9 @@ func (d *TeamDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
// teams, err := client.List(ctx, []string{model.Name.ValueString()})
teams, err := client.List(ctx, []string{config.Name.ValueString()})
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing Team state",
fmt.Sprintf("Could not search for Team, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Team", "list", err))

return
}

if len(teams) != 1 {
Expand Down
17 changes: 4 additions & 13 deletions internal/provider/datasources/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datasources

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -11,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(&TeamsDataSource{})
Expand Down Expand Up @@ -47,10 +47,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 Expand Up @@ -95,10 +92,7 @@ func (d *TeamsDataSource) Read(ctx context.Context, req datasource.ReadRequest,

client, err := d.client.Teams(model.AccountID.ValueUUID())
if err != nil {
resp.Diagnostics.AddError(
"Error creating variable client",
fmt.Sprintf("Could not create variable client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()),
)
resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Teams", err))

return
}
Expand All @@ -107,10 +101,7 @@ func (d *TeamsDataSource) Read(ctx context.Context, req datasource.ReadRequest,
var filter []string
teams, err := client.List(ctx, filter)
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing team state",
fmt.Sprintf("Could not read team, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Teams", "list", err))

return
}
Expand Down
17 changes: 4 additions & 13 deletions internal/provider/datasources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package datasources

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"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 _ = datasource.DataSourceWithConfigure(&VariableDataSource{})
Expand Down Expand Up @@ -52,10 +52,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 Expand Up @@ -139,10 +136,7 @@ func (d *VariableDataSource) Read(ctx context.Context, req datasource.ReadReques

client, err := d.client.Variables(model.AccountID.ValueUUID(), model.WorkspaceID.ValueUUID())
if err != nil {
resp.Diagnostics.AddError(
"Error creating variable client",
fmt.Sprintf("Could not create variable client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()),
)
resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Variable", err))

return
}
Expand All @@ -164,10 +158,7 @@ func (d *VariableDataSource) Read(ctx context.Context, req datasource.ReadReques
}

if err != nil {
resp.Diagnostics.AddError(
"Error refreshing variable state",
fmt.Sprintf("Could not read variable, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Variable", "get", err))

return
}
Expand Down
16 changes: 4 additions & 12 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 Expand Up @@ -167,20 +165,14 @@ func (d *WorkPoolDataSource) Read(ctx context.Context, req datasource.ReadReques

client, err := d.client.WorkPools(model.AccountID.ValueUUID(), model.WorkspaceID.ValueUUID())
if err != nil {
resp.Diagnostics.AddError(
"Error creating variable client",
fmt.Sprintf("Could not create variable client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()),
)
resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Work Pool", err))

return
}

pool, err := client.Get(ctx, model.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error refreshing work pool state",
fmt.Sprintf("Could not read work pool, unexpected error: %s", err.Error()),
)
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Work Pool", "get", err))

return
}
Expand Down
Loading

0 comments on commit 5ff5236

Please sign in to comment.