diff --git a/internal/provider/datasources/account.go b/internal/provider/datasources/account.go index 1b9e76ea..eb9924a7 100644 --- a/internal/provider/datasources/account.go +++ b/internal/provider/datasources/account.go @@ -2,7 +2,6 @@ package datasources import ( "context" - "fmt" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -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 } @@ -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 } diff --git a/internal/provider/datasources/account_member.go b/internal/provider/datasources/account_member.go index 68978a91..a2e56963 100644 --- a/internal/provider/datasources/account_member.go +++ b/internal/provider/datasources/account_member.go @@ -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 } @@ -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 { diff --git a/internal/provider/datasources/account_members.go b/internal/provider/datasources/account_members.go index 98c6cc4e..2da20aff 100644 --- a/internal/provider/datasources/account_members.go +++ b/internal/provider/datasources/account_members.go @@ -2,7 +2,6 @@ package datasources import ( "context" - "fmt" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -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 } @@ -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{ diff --git a/internal/provider/datasources/account_role.go b/internal/provider/datasources/account_role.go index aa73ac6a..94b759a7 100644 --- a/internal/provider/datasources/account_role.go +++ b/internal/provider/datasources/account_role.go @@ -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 } @@ -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 { diff --git a/internal/provider/datasources/block.go b/internal/provider/datasources/block.go index fd16bb2d..1a5cb46c 100644 --- a/internal/provider/datasources/block.go +++ b/internal/provider/datasources/block.go @@ -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" @@ -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 } @@ -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 } @@ -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 } diff --git a/internal/provider/datasources/service_account.go b/internal/provider/datasources/service_account.go index e97dffaa..7e0e1e48 100644 --- a/internal/provider/datasources/service_account.go +++ b/internal/provider/datasources/service_account.go @@ -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{}) @@ -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 } @@ -162,10 +160,7 @@ 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 } @@ -173,10 +168,13 @@ func (d *ServiceAccountDataSource) Read(ctx context.Context, req datasource.Read // 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 @@ -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 } diff --git a/internal/provider/datasources/team.go b/internal/provider/datasources/team.go index 9c143e6b..43ae52b1 100644 --- a/internal/provider/datasources/team.go +++ b/internal/provider/datasources/team.go @@ -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 } @@ -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 { diff --git a/internal/provider/datasources/teams.go b/internal/provider/datasources/teams.go index 9c3294e6..85cd5b78 100644 --- a/internal/provider/datasources/teams.go +++ b/internal/provider/datasources/teams.go @@ -2,7 +2,6 @@ package datasources import ( "context" - "fmt" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -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{}) @@ -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 } @@ -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 } @@ -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 } diff --git a/internal/provider/datasources/variable.go b/internal/provider/datasources/variable.go index c42b551d..5a905600 100644 --- a/internal/provider/datasources/variable.go +++ b/internal/provider/datasources/variable.go @@ -2,7 +2,6 @@ package datasources import ( "context" - "fmt" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -10,6 +9,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{}) @@ -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 } @@ -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 } @@ -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 } diff --git a/internal/provider/datasources/work_pool.go b/internal/provider/datasources/work_pool.go index f459ad99..1bcd66a3 100644 --- a/internal/provider/datasources/work_pool.go +++ b/internal/provider/datasources/work_pool.go @@ -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{}) @@ -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 } @@ -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 } diff --git a/internal/provider/datasources/work_pools.go b/internal/provider/datasources/work_pools.go index f674b2ef..3911c5b9 100644 --- a/internal/provider/datasources/work_pools.go +++ b/internal/provider/datasources/work_pools.go @@ -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{}) @@ -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 } @@ -110,10 +108,7 @@ func (d *WorkPoolsDataSource) Read(ctx context.Context, req datasource.ReadReque 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 } @@ -122,10 +117,7 @@ func (d *WorkPoolsDataSource) Read(ctx context.Context, req datasource.ReadReque pools, err := client.List(ctx, filter) 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 Pools", "list", err)) return } diff --git a/internal/provider/datasources/worker_metadata.go b/internal/provider/datasources/worker_metadata.go index f8a42f0c..76d2c115 100644 --- a/internal/provider/datasources/worker_metadata.go +++ b/internal/provider/datasources/worker_metadata.go @@ -3,7 +3,6 @@ package datasources import ( "context" "encoding/json" - "fmt" "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -42,10 +41,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 } @@ -146,10 +142,7 @@ func (d *WorkerMetadataDataSource) Read(ctx context.Context, req datasource.Read workerTypeByPackage, err := client.GetWorkerMetadataViews(ctx) if err != nil { - resp.Diagnostics.AddError( - "Error refreshing Worker Metadata state", - fmt.Sprintf("Could not read Worker Metadata, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Worker Metadata", "get", err)) return } diff --git a/internal/provider/datasources/workspace.go b/internal/provider/datasources/workspace.go index ce48db38..7d40349c 100644 --- a/internal/provider/datasources/workspace.go +++ b/internal/provider/datasources/workspace.go @@ -7,11 +7,11 @@ import ( "github.com/google/uuid" "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" + "github.com/prefecthq/terraform-provider-prefect/internal/provider/helpers" ) var _ = datasource.DataSourceWithConfigure(&WorkspaceDataSource{}) @@ -53,10 +53,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 } @@ -134,10 +131,7 @@ func (d *WorkspaceDataSource) Read(ctx context.Context, req datasource.ReadReque client, err := d.client.Workspaces(model.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating workspace client", - fmt.Sprintf("Could not create workspace client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace", err)) return } @@ -146,22 +140,21 @@ func (d *WorkspaceDataSource) Read(ctx context.Context, req datasource.ReadReque // If both are set, we prefer the ID // If neither are set, we will fail early. var workspace *api.Workspace + var operation string if !model.ID.IsNull() { var workspaceID uuid.UUID workspaceID, err = uuid.Parse(model.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace ID", - fmt.Sprintf("Could not parse workspace ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace", err)) return } + operation = "get" workspace, err = client.Get(ctx, workspaceID) } else if !model.Handle.IsNull() { var workspaces []*api.Workspace + operation = "list" workspaces, err = client.List(ctx, []string{model.Handle.ValueString()}) // The error from the API call should take precedence @@ -185,10 +178,7 @@ func (d *WorkspaceDataSource) Read(ctx context.Context, req datasource.ReadReque } if err != nil { - resp.Diagnostics.AddError( - "Error refreshing workspace state", - fmt.Sprintf("Could not read workspace, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace", operation, err)) return } diff --git a/internal/provider/datasources/workspace_role.go b/internal/provider/datasources/workspace_role.go index bc6f920c..316b35cc 100644 --- a/internal/provider/datasources/workspace_role.go +++ b/internal/provider/datasources/workspace_role.go @@ -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. @@ -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 } @@ -129,10 +127,7 @@ func (d *WorkspaceRoleDataSource) Read(ctx context.Context, req datasource.ReadR client, err := d.client.WorkspaceRoles(model.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating the Workspace Roles client", - fmt.Sprintf("Could not create Workspace Roles client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace Role", err)) return } @@ -142,10 +137,9 @@ func (d *WorkspaceRoleDataSource) Read(ctx context.Context, req datasource.ReadR // as we are querying a single Role name, not a list of names workspaceRoles, err := client.List(ctx, []string{model.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("Workspace Role", "list", err)) + + return } if len(workspaceRoles) != 1 { diff --git a/internal/provider/helpers/diagnostics.go b/internal/provider/helpers/diagnostics.go index af092e91..8fd25d96 100644 --- a/internal/provider/helpers/diagnostics.go +++ b/internal/provider/helpers/diagnostics.go @@ -4,9 +4,12 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/path" ) -// https://developer.hashicorp.com/terraform/plugin/framework/diagnostics#custom-diagnostics-types +const ( + reportMessage = "Please report this issue to the provider developers." +) // CreateClientErrorDiagnostic returns an error diagnostic for when one of the // HTTP clients failed to be instantiated. @@ -15,7 +18,7 @@ import ( func CreateClientErrorDiagnostic(clientName string, err error) diag.Diagnostic { return diag.NewErrorDiagnostic( fmt.Sprintf("Error creating %s client", clientName), - fmt.Sprintf("Could not create %s client, due to error: %s. If you believe this to be a bug in the provider, please report this to the maintainers.", clientName, err.Error()), + fmt.Sprintf("Could not create %s client, due to error: %s. %s", clientName, err.Error(), reportMessage), ) } @@ -29,3 +32,38 @@ 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), + ) +} + +// SerializeDataErrorDiagnostic returns an error diagnostic for when an +// attempt to serialize data into a JSON string fails. +// +//nolint:ireturn // required by Terraform API +func SerializeDataErrorDiagnostic(pathRoot, resourceName string, err error) diag.Diagnostic { + return diag.NewAttributeErrorDiagnostic( + path.Root(pathRoot), + fmt.Sprintf("Failed to serialize %s data", resourceName), + fmt.Sprintf("Could not serialize %s as JSON string", err.Error()), + ) +} + +// ParseUUIDErrorDiagnostic returns an error diagnostic for when an attempt +// to parse a string into a UUID fails. +// +//nolint:ireturn // required by Terraform API +func ParseUUIDErrorDiagnostic(resourceName string, err error) diag.Diagnostic { + return diag.NewAttributeErrorDiagnostic( + path.Root("id"), + fmt.Sprintf("Error parsing %s ID", resourceName), + fmt.Sprintf("Could not parse %s ID to UUID, unexpected error: %s", resourceName, err.Error()), + ) +} diff --git a/internal/provider/resources/account.go b/internal/provider/resources/account.go index 8d6aa1a0..22df76ac 100644 --- a/internal/provider/resources/account.go +++ b/internal/provider/resources/account.go @@ -2,7 +2,6 @@ package resources import ( "context" - "fmt" "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-framework/diag" @@ -62,10 +61,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 } @@ -168,11 +164,7 @@ func (r *AccountResource) Read(ctx context.Context, req resource.ReadRequest, re accountID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Account ID", - fmt.Sprintf("Could not parse account ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Account", err)) return } @@ -184,10 +176,7 @@ func (r *AccountResource) Read(ctx context.Context, req resource.ReadRequest, re 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 } @@ -214,11 +203,7 @@ func (r *AccountResource) Update(ctx context.Context, req resource.UpdateRequest accountID, err := uuid.Parse(plan.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Account ID", - fmt.Sprintf("Could not parse account ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Account", err)) return } @@ -237,20 +222,14 @@ func (r *AccountResource) Update(ctx context.Context, req resource.UpdateRequest BillingEmail: plan.BillingEmail.ValueStringPointer(), }) if err != nil { - resp.Diagnostics.AddError( - "Error updating account", - fmt.Sprintf("Could not update account, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account", "update", err)) return } 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 } @@ -277,11 +256,7 @@ func (r *AccountResource) Delete(ctx context.Context, req resource.DeleteRequest accountID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Account ID", - fmt.Sprintf("Could not parse account ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Account", err)) return } @@ -293,10 +268,7 @@ func (r *AccountResource) Delete(ctx context.Context, req resource.DeleteRequest err = client.Delete(ctx) if err != nil { - resp.Diagnostics.AddError( - "Error deleting account", - fmt.Sprintf("Could not delete account, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account", "delete", err)) return } diff --git a/internal/provider/resources/block.go b/internal/provider/resources/block.go index b3e3c801..04a3bda9 100644 --- a/internal/provider/resources/block.go +++ b/internal/provider/resources/block.go @@ -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 } @@ -281,10 +278,7 @@ func (r *BlockResource) Read(ctx context.Context, req resource.ReadRequest, resp client, err := r.client.BlockDocuments(state.AccountID.ValueUUID(), state.WorkspaceID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating block client", - fmt.Sprintf("Could not create block client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Block Document", err)) return } @@ -292,21 +286,14 @@ func (r *BlockResource) Read(ctx context.Context, req resource.ReadRequest, resp var blockID uuid.UUID blockID, err = uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Block ID", - fmt.Sprintf("Could not parse block ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Block", err)) return } block, err := client.Get(ctx, blockID) 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 } @@ -319,11 +306,7 @@ func (r *BlockResource) Read(ctx context.Context, req resource.ReadRequest, resp byteSlice, err := json.Marshal(block.Data) if err != nil { - diags.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 } @@ -392,11 +375,7 @@ func (r *BlockResource) Update(ctx context.Context, req resource.UpdateRequest, blockID, err := uuid.Parse(plan.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing block ID", - fmt.Sprintf("Could not parse block ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Block", err)) return } @@ -426,10 +405,7 @@ func (r *BlockResource) Update(ctx context.Context, req resource.UpdateRequest, block, err := blockDocumentClient.Get(ctx, blockID) 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 } @@ -442,11 +418,7 @@ func (r *BlockResource) Update(ctx context.Context, req resource.UpdateRequest, byteSlice, err := json.Marshal(block.Data) if err != nil { - diags.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 } @@ -477,11 +449,7 @@ func (r *BlockResource) Delete(ctx context.Context, req resource.DeleteRequest, blockDocumentID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Block ID", - fmt.Sprintf("Could not parse block ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Block", err)) return } @@ -521,10 +489,7 @@ func (r *BlockResource) ImportState(ctx context.Context, req resource.ImportStat if len(parts) == 2 && parts[1] != "" { workspaceID, err := uuid.Parse(parts[1]) if err != nil { - resp.Diagnostics.AddError( - "Error parsing Workspace ID", - fmt.Sprintf("Could not parse workspace ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace", err)) return } diff --git a/internal/provider/resources/block_access.go b/internal/provider/resources/block_access.go index 963f6ef1..ec1aa849 100644 --- a/internal/provider/resources/block_access.go +++ b/internal/provider/resources/block_access.go @@ -2,7 +2,6 @@ package resources import ( "context" - "fmt" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -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 } diff --git a/internal/provider/resources/service_account.go b/internal/provider/resources/service_account.go index 32072c8a..d648f618 100644 --- a/internal/provider/resources/service_account.go +++ b/internal/provider/resources/service_account.go @@ -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 } @@ -217,10 +214,7 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create accountRoles, err := accountRoleClient.List(ctx, []string{plan.AccountRoleName.ValueString()}) if err != nil { - resp.Diagnostics.AddError( - "Error fetching Account Role", - fmt.Sprintf("Could not fetch Account Role, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account Role", "list", err)) return } @@ -245,10 +239,7 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create serviceAccount, err := serviceAccountClient.Create(ctx, createReq) if err != nil { - resp.Diagnostics.AddError( - "Error creating service account", - fmt.Sprintf("Could not create service account, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Service Account", "create", err)) return } @@ -295,10 +286,13 @@ func (r *ServiceAccountResource) Read(ctx context.Context, req resource.ReadRequ // 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 !state.ID.IsNull() { + operation = "get" serviceAccount, err = client.Get(ctx, state.ID.ValueString()) } else if !state.Name.IsNull() { var serviceAccounts []*api.ServiceAccount + operation = "list" serviceAccounts, err = client.List(ctx, []string{state.Name.ValueString()}) // The error from the API call should take precedence @@ -322,10 +316,7 @@ func (r *ServiceAccountResource) Read(ctx context.Context, req resource.ReadRequ } 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 } @@ -386,10 +377,7 @@ func (r *ServiceAccountResource) Update(ctx context.Context, req resource.Update accountRoles, err := accountRoleClient.List(ctx, []string{plan.AccountRoleName.ValueString()}) if err != nil { - resp.Diagnostics.AddError( - "Error fetching Account Role", - fmt.Sprintf("Could not fetch Account Role, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Account Role", "list", err)) return } @@ -409,20 +397,14 @@ func (r *ServiceAccountResource) Update(ctx context.Context, req resource.Update // Update client method requires context, botID, request args err = client.Update(ctx, plan.ID.ValueString(), updateReq) if err != nil { - resp.Diagnostics.AddError( - "Error updating Service Account", - fmt.Sprintf("Could not update Service Account, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Service Account", "update", err)) return } serviceAccount, err := client.Get(ctx, plan.ID.ValueString()) if err != nil { - resp.Diagnostics.AddError( - "Error refreshing Service Account state", - fmt.Sprintf("Could not read Service Account, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Service Account", "get", err)) return } @@ -438,10 +420,7 @@ func (r *ServiceAccountResource) Update(ctx context.Context, req resource.Update APIKeyExpiration: providedExpiration, }) if err != nil { - resp.Diagnostics.AddError( - "Error rotating Service Account key", - fmt.Sprintf("Could not rotate Service Account key, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Service Account", "key rotate", err)) return } @@ -483,10 +462,7 @@ func (r *ServiceAccountResource) Delete(ctx context.Context, req resource.Delete err = client.Delete(ctx, state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddError( - "Error deleting Service Account", - fmt.Sprintf("Could not delete Service Account, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Service Account", "delete", err)) return } diff --git a/internal/provider/resources/variable.go b/internal/provider/resources/variable.go index cb3337a0..7b740281 100644 --- a/internal/provider/resources/variable.go +++ b/internal/provider/resources/variable.go @@ -2,7 +2,6 @@ package resources import ( "context" - "fmt" "strings" "github.com/google/uuid" @@ -19,6 +18,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 ( @@ -64,10 +64,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 } @@ -173,10 +170,7 @@ func (r *VariableResource) Create(ctx context.Context, req resource.CreateReques client, err := r.client.Variables(plan.AccountID.ValueUUID(), plan.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 } @@ -187,10 +181,7 @@ func (r *VariableResource) Create(ctx context.Context, req resource.CreateReques Tags: tags, }) if err != nil { - resp.Diagnostics.AddError( - "Error creating variable", - fmt.Sprintf("Could not create variable, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Variable", "create", err)) return } @@ -218,10 +209,7 @@ func (r *VariableResource) Read(ctx context.Context, req resource.ReadRequest, r client, err := r.client.Variables(state.AccountID.ValueUUID(), state.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 } @@ -236,11 +224,7 @@ func (r *VariableResource) Read(ctx context.Context, req resource.ReadRequest, r var variableID uuid.UUID variableID, err = uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Variable ID", - fmt.Sprintf("Could not parse variable ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Variable", err)) return } @@ -257,10 +241,7 @@ func (r *VariableResource) Read(ctx context.Context, req resource.ReadRequest, r } 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 } @@ -287,10 +268,7 @@ func (r *VariableResource) Update(ctx context.Context, req resource.UpdateReques client, err := r.client.Variables(plan.AccountID.ValueUUID(), plan.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 } @@ -303,11 +281,7 @@ func (r *VariableResource) Update(ctx context.Context, req resource.UpdateReques variableID, err := uuid.Parse(plan.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Variable ID", - fmt.Sprintf("Could not parse variable ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Variable", err)) return } @@ -318,20 +292,14 @@ func (r *VariableResource) Update(ctx context.Context, req resource.UpdateReques Tags: tags, }) if err != nil { - resp.Diagnostics.AddError( - "Error updating variable", - fmt.Sprintf("Could not update variable, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Variable", "update", err)) return } variable, err := client.Get(ctx, variableID) 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 } @@ -358,31 +326,21 @@ func (r *VariableResource) Delete(ctx context.Context, req resource.DeleteReques client, err := r.client.Variables(state.AccountID.ValueUUID(), state.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 } variableID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Variable ID", - fmt.Sprintf("Could not parse variable ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Variable", err)) return } err = client.Delete(ctx, variableID) if err != nil { - resp.Diagnostics.AddError( - "Error deleting variable", - fmt.Sprintf("Could not delete variable, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Variable", "delete", err)) return } @@ -423,10 +381,7 @@ func (r *VariableResource) ImportState(ctx context.Context, req resource.ImportS if len(parts) == 2 && parts[1] != "" { workspaceID, err := uuid.Parse(parts[1]) if err != nil { - resp.Diagnostics.AddError( - "Error parsing Workspace ID", - fmt.Sprintf("Could not parse workspace ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace", err)) return } diff --git a/internal/provider/resources/work_pool.go b/internal/provider/resources/work_pool.go index e2c6d630..ccfebb5b 100644 --- a/internal/provider/resources/work_pool.go +++ b/internal/provider/resources/work_pool.go @@ -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 ( @@ -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 } @@ -242,10 +240,7 @@ func (r *WorkPoolResource) Create(ctx context.Context, req resource.CreateReques client, err := r.client.WorkPools(plan.AccountID.ValueUUID(), plan.WorkspaceID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating work pool client", - fmt.Sprintf("Could not create work pool 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 } @@ -259,10 +254,7 @@ func (r *WorkPoolResource) Create(ctx context.Context, req resource.CreateReques ConcurrencyLimit: plan.ConcurrencyLimit.ValueInt64Pointer(), }) if err != nil { - resp.Diagnostics.AddError( - "Error creating work pool", - fmt.Sprintf("Could not create work pool, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Work Pool", "create", err)) return } @@ -290,20 +282,14 @@ func (r *WorkPoolResource) Read(ctx context.Context, req resource.ReadRequest, r client, err := r.client.WorkPools(state.AccountID.ValueUUID(), state.WorkspaceID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating work pool client", - fmt.Sprintf("Could not create work pool 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, state.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError( - "Error refreshing work pool state", - fmt.Sprintf("Could not read work pool, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Work Pool", "get", err)) return } @@ -346,10 +332,7 @@ func (r *WorkPoolResource) Update(ctx context.Context, req resource.UpdateReques client, err := r.client.WorkPools(plan.AccountID.ValueUUID(), plan.WorkspaceID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating work pool client", - fmt.Sprintf("Could not create work pool 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 } @@ -361,20 +344,14 @@ func (r *WorkPoolResource) Update(ctx context.Context, req resource.UpdateReques ConcurrencyLimit: plan.ConcurrencyLimit.ValueInt64Pointer(), }) if err != nil { - resp.Diagnostics.AddError( - "Error updating work pool", - fmt.Sprintf("Could not update work pool, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Work Pool", "update", err)) return } pool, err := client.Get(ctx, plan.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError( - "Error refreshing work pool state", - fmt.Sprintf("Could not read work pool, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Work Pool", "get", err)) return } @@ -401,20 +378,14 @@ func (r *WorkPoolResource) Delete(ctx context.Context, req resource.DeleteReques client, err := r.client.WorkPools(state.AccountID.ValueUUID(), state.WorkspaceID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating work pool client", - fmt.Sprintf("Could not create work pool 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 } err = client.Delete(ctx, state.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError( - "Error deleting work pool", - fmt.Sprintf("Could not delete work pool, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Work Pool", "delete", err)) return } diff --git a/internal/provider/resources/workspace.go b/internal/provider/resources/workspace.go index 1e88712d..f97ab860 100644 --- a/internal/provider/resources/workspace.go +++ b/internal/provider/resources/workspace.go @@ -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 ( @@ -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 } @@ -151,10 +149,9 @@ func (r *WorkspaceResource) Create(ctx context.Context, req resource.CreateReque client, err := r.client.Workspaces(plan.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating workspace client", - fmt.Sprintf("Could not create workspace client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace", err)) + + return } workspace, err := client.Create(ctx, api.WorkspaceCreate{ @@ -163,10 +160,7 @@ func (r *WorkspaceResource) Create(ctx context.Context, req resource.CreateReque Description: plan.Description.ValueStringPointer(), }) if err != nil { - resp.Diagnostics.AddError( - "Error creating workspace", - fmt.Sprintf("Could not create workspace, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace", "create", err)) return } @@ -203,31 +197,30 @@ func (r *WorkspaceResource) Read(ctx context.Context, req resource.ReadRequest, client, err := r.client.Workspaces(state.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating workspace client", - fmt.Sprintf("Could not create workspace client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace", err)) + + return } // A workspace can be imported + read by either ID or Handle // If both are set, we prefer the ID var workspace *api.Workspace + var operation string + if !state.ID.IsNull() { var workspaceID uuid.UUID workspaceID, err = uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace ID", - fmt.Sprintf("Could not parse workspace ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace", err)) return } + operation = "get" workspace, err = client.Get(ctx, workspaceID) } else if !state.Handle.IsNull() { var workspaces []*api.Workspace + operation = "list" workspaces, err = client.List(ctx, []string{state.Handle.ValueString()}) // The error from the API call should take precedence @@ -246,6 +239,7 @@ func (r *WorkspaceResource) Read(ctx context.Context, req resource.ReadRequest, "Error refreshing Workspace state", fmt.Sprintf("Could not read Workspace, unexpected error: %s", err.Error()), ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace", operation, err)) return } @@ -272,19 +266,14 @@ func (r *WorkspaceResource) Update(ctx context.Context, req resource.UpdateReque client, err := r.client.Workspaces(plan.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating workspace client", - fmt.Sprintf("Could not create workspace client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace", err)) + + return } workspaceID, err := uuid.Parse(plan.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace ID", - fmt.Sprintf("Could not parse workspace ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace", err)) return } @@ -297,20 +286,14 @@ func (r *WorkspaceResource) Update(ctx context.Context, req resource.UpdateReque err = client.Update(ctx, workspaceID, payload) if err != nil { - resp.Diagnostics.AddError( - "Error updating workspace", - fmt.Sprintf("Could not update workspace, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace", "update", err)) return } workspace, err := client.Get(ctx, workspaceID) if err != nil { - resp.Diagnostics.AddError( - "Error refreshing Workspace state", - fmt.Sprintf("Could not read Workspace, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace", "get", err)) return } @@ -337,29 +320,21 @@ func (r *WorkspaceResource) Delete(ctx context.Context, req resource.DeleteReque client, err := r.client.Workspaces(state.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating workspace client", - fmt.Sprintf("Could not create workspace client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace", err)) + + return } workspaceID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace ID", - fmt.Sprintf("Could not parse workspace ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace", err)) return } err = client.Delete(ctx, workspaceID) if err != nil { - resp.Diagnostics.AddError( - "Error deleting Workspace", - fmt.Sprintf("Could not delete Workspace, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace", "delete", err)) return } diff --git a/internal/provider/resources/workspace_access.go b/internal/provider/resources/workspace_access.go index fca6c085..da633d64 100644 --- a/internal/provider/resources/workspace_access.go +++ b/internal/provider/resources/workspace_access.go @@ -2,11 +2,9 @@ package resources import ( "context" - "fmt" "github.com/google/uuid" "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/resource/schema/planmodifier" @@ -55,10 +53,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 } @@ -187,11 +182,9 @@ func (r *WorkspaceAccessResource) Read(ctx context.Context, req resource.ReadReq accessID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace Role ID", - fmt.Sprintf("Could not parse Workspace Access ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace Role", err)) + + return } accessorType := state.AccessorType.ValueString() @@ -261,11 +254,7 @@ func (r *WorkspaceAccessResource) Delete(ctx context.Context, req resource.Delet accessID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace Access ID", - fmt.Sprintf("Could not parse Workspace Access ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace Access", err)) return } diff --git a/internal/provider/resources/workspace_role.go b/internal/provider/resources/workspace_role.go index b5a0cdf0..baf019bf 100644 --- a/internal/provider/resources/workspace_role.go +++ b/internal/provider/resources/workspace_role.go @@ -2,7 +2,6 @@ package resources import ( "context" - "fmt" "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-framework/diag" @@ -15,6 +14,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 ( @@ -60,10 +60,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 } @@ -178,10 +175,7 @@ func (r *WorkspaceRoleResource) Create(ctx context.Context, req resource.CreateR client, err := r.client.WorkspaceRoles(plan.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating Workspace Role client", - fmt.Sprintf("Could not create Workspace Role client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace Role", err)) return } @@ -193,10 +187,7 @@ func (r *WorkspaceRoleResource) Create(ctx context.Context, req resource.CreateR InheritedRoleID: plan.InheritedRoleID.ValueUUIDPointer(), }) if err != nil { - resp.Diagnostics.AddError( - "Error creating Workspace Role", - fmt.Sprintf("Could not create Workspace Role, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace Role", "create", err)) return } @@ -224,29 +215,21 @@ func (r *WorkspaceRoleResource) Read(ctx context.Context, req resource.ReadReque client, err := r.client.WorkspaceRoles(state.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating Workspace Role client", - fmt.Sprintf("Could not create Workspace Role client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace Role", err)) return } roleID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace Role ID", - fmt.Sprintf("Could not parse Workspace Role ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace Role", err)) + + return } role, err := client.Get(ctx, roleID) if err != nil { - resp.Diagnostics.AddError( - "Error refreshing Workspace Role state", - fmt.Sprintf("Could not read Workspace Role, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace Role", "get", err)) return } @@ -273,10 +256,7 @@ func (r *WorkspaceRoleResource) Update(ctx context.Context, req resource.UpdateR client, err := r.client.WorkspaceRoles(plan.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating Workspace Role client", - fmt.Sprintf("Could not create Workspace Role client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace Role", err)) return } @@ -289,11 +269,7 @@ func (r *WorkspaceRoleResource) Update(ctx context.Context, req resource.UpdateR roleID, err := uuid.Parse(plan.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace Role ID", - fmt.Sprintf("Could not parse Workspace Role ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace Role", err)) return } @@ -305,20 +281,14 @@ func (r *WorkspaceRoleResource) Update(ctx context.Context, req resource.UpdateR InheritedRoleID: plan.InheritedRoleID.ValueUUIDPointer(), }) if err != nil { - resp.Diagnostics.AddError( - "Error updating Workspace Role", - fmt.Sprintf("Could not update Workspace Role, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace Role", "update", err)) return } role, err := client.Get(ctx, roleID) 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("Workspace Role", "get", err)) return } @@ -345,31 +315,21 @@ func (r *WorkspaceRoleResource) Delete(ctx context.Context, req resource.DeleteR client, err := r.client.WorkspaceRoles(state.AccountID.ValueUUID()) if err != nil { - resp.Diagnostics.AddError( - "Error creating Workspace Role client", - fmt.Sprintf("Could not create Workspace Role client, unexpected error: %s. This is a bug in the provider, please report this to the maintainers.", err.Error()), - ) + resp.Diagnostics.Append(helpers.CreateClientErrorDiagnostic("Workspace Role`", err)) return } roleID, err := uuid.Parse(state.ID.ValueString()) if err != nil { - resp.Diagnostics.AddAttributeError( - path.Root("id"), - "Error parsing Workspace Role ID", - fmt.Sprintf("Could not parse Workspace Role ID to UUID, unexpected error: %s", err.Error()), - ) + resp.Diagnostics.Append(helpers.ParseUUIDErrorDiagnostic("Workspace Role", err)) return } err = client.Delete(ctx, roleID) if err != nil { - resp.Diagnostics.AddError( - "Error deleting Workspace Role", - fmt.Sprintf("Could not delete Workspace Role, unexpected error: %s", err), - ) + resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Workspace Role", "delete", err)) return }