Skip to content

Commit

Permalink
chore: move common fields into BaseModel types (#320)
Browse files Browse the repository at this point in the history
* Implement helpers.BaseModel in datasources

* Use datasource-specific BaseModel

The datasource BaseModel should use UUID as the type for ID, while the
resource BaseModel should use String as the type for ID.

I'd like to avoid making resource conversion logic for each of those
resources, so let's just use separate helpers for now.

* Implement BaseModel for resources
  • Loading branch information
mitchnielsen authored Dec 11, 2024
1 parent 082614f commit 68d768a
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 95 deletions.
4 changes: 1 addition & 3 deletions internal/provider/datasources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ type AccountDataSource struct {

// AccountDataSourceModel defines the Terraform data source model.
type AccountDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
Handle types.String `tfsdk:"handle"`
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/datasources/account_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ type AccountRoleDataSource struct {
// AccountRoleDataSource defines the Terraform data source model
// the TF data source configuration will be unmarshalled into this struct.
type AccountRoleDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
Permissions types.List `tfsdk:"permissions"`
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/datasources/automation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (

// AutomationResourceModel defines the Terraform resource model.
type AutomationDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helpers
package datasources

import "github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"

Expand Down
9 changes: 4 additions & 5 deletions internal/provider/datasources/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ type blockDataSource struct {

// BlockDataSourceModel defines the Terraform data source model.
type BlockDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Data jsontypes.Normalized `tfsdk:"data"`
Expand Down
8 changes: 5 additions & 3 deletions internal/provider/datasources/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ func copyDeploymentToModel(ctx context.Context, deployment *api.Deployment, mode
// type because struct embedding does not automatically make the embedding
// struct convertible to the embedded type.
compatibleModel := &resources.DeploymentResourceModel{
ID: model.ID,
Created: model.Created,
Updated: model.Updated,
BaseModel: resources.BaseModel{
ID: model.ID,
Created: model.Created,
Updated: model.Updated,
},

AccountID: model.AccountID,
ConcurrencyLimit: model.ConcurrencyLimit,
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/datasources/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ type ServiceAccountDataSource struct {

// ServiceAccountDataSourceModel defines the Terraform data source model.
type ServiceAccountDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
ActorID customtypes.UUIDValue `tfsdk:"actor_id"`
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/datasources/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ type TeamDataSource struct {
}

type TeamDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
BaseModel

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
}
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/datasources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ type VariableDataSource struct {

// VariableDataSourceModel defines the Terraform data source model.
type VariableDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Value types.Dynamic `tfsdk:"value"`
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/datasources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ type WorkPoolDataSource struct {

// WorkPoolDataSourceModel defines the Terraform data source model.
type WorkPoolDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Expand Down
7 changes: 3 additions & 4 deletions internal/provider/datasources/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ type WorkspaceDataSource struct {

// WorkspaceDataSourceModel defines the Terraform data source model.
type WorkspaceDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`

Name types.String `tfsdk:"name"`
Handle types.String `tfsdk:"handle"`
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/datasources/workspace_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ type WorkspaceRoleDataSource struct {
// WorkspaceRoleDataSourceModel defines the Terraform data source model
// the TF data source configuration will be unmarshalled into this struct.
type WorkspaceRoleDataSourceModel struct {
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ type AccountResource struct {

// AccountResourceModel defines the Terraform resource model.
type AccountResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
Handle types.String `tfsdk:"handle"`
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/resources/automation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (

// AutomationResourceModel defines the Terraform resource model.
type AutomationResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Expand Down
14 changes: 14 additions & 0 deletions internal/provider/resources/base_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package resources

import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes"
)

// BaseModel is embedded in all other types and defines fields
// common to all Prefect data models.
type BaseModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
}
9 changes: 4 additions & 5 deletions internal/provider/resources/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ type BlockResource struct {
}

type BlockResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
TypeSlug types.String `tfsdk:"type_slug"`
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/resources/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ type DeploymentResource struct {

// DeploymentResourceModel defines the Terraform resource model.
type DeploymentResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
Expand Down
6 changes: 5 additions & 1 deletion internal/provider/resources/deployment_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ type DeploymentScheduleResource struct {
}

type DeploymentScheduleResourceModel struct {
helpers.BaseModel
// This model uses UUIDValue for the ID type, while most other
// resources use types.String. This may eventually be made consistent.
ID customtypes.UUIDValue `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/resources/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ type FlowResource struct {

// FlowResourceModel defines the Terraform resource model.
type FlowResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
BaseModel

WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`

Name types.String `tfsdk:"name"`
Tags types.List `tfsdk:"tags"`
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/resources/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ type ServiceAccountResource struct {
}

type ServiceAccountResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
ActorID customtypes.UUIDValue `tfsdk:"actor_id"`
Expand Down
26 changes: 13 additions & 13 deletions internal/provider/resources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ type VariableResource struct {
//
// V0: Value is types.String.
type VariableResourceModelV0 struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
Expand All @@ -56,11 +55,10 @@ type VariableResourceModelV0 struct {

// V1: Value is types.Dynamic.
type VariableResourceModelV1 struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Value types.Dynamic `tfsdk:"value"`
Expand Down Expand Up @@ -197,9 +195,11 @@ func (r *VariableResource) UpgradeState(_ context.Context) map[int64]resource.St
// that is tied to the old schema version, we need to copy
// the existing state into the new schema version.
upgradedStateData := VariableResourceModelV1{
ID: priorStateData.ID,
Created: priorStateData.Created,
Updated: priorStateData.Updated,
BaseModel: BaseModel{
ID: priorStateData.ID,
Created: priorStateData.Created,
Updated: priorStateData.Updated,
},
AccountID: priorStateData.AccountID,
WorkspaceID: priorStateData.WorkspaceID,
Name: priorStateData.Name,
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/resources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ type WorkPoolResource struct {

// WorkPoolResourceModel defines the Terraform resource model.
type WorkPoolResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`
WorkspaceID customtypes.UUIDValue `tfsdk:"workspace_id"`

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Expand Down
7 changes: 3 additions & 4 deletions internal/provider/resources/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ type WorkspaceResource struct {

// WorkspaceResourceModel defines the Terraform resource model.
type WorkspaceResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
AccountID customtypes.UUIDValue `tfsdk:"account_id"`
BaseModel

AccountID customtypes.UUIDValue `tfsdk:"account_id"`

Name types.String `tfsdk:"name"`
Handle types.String `tfsdk:"handle"`
Expand Down
4 changes: 1 addition & 3 deletions internal/provider/resources/workspace_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ type WorkspaceRoleResource struct {

// WorkspaceRoleResourceModel defines the Terraform resource model.
type WorkspaceRoleResourceModel struct {
ID types.String `tfsdk:"id"`
Created customtypes.TimestampValue `tfsdk:"created"`
Updated customtypes.TimestampValue `tfsdk:"updated"`
BaseModel

Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Expand Down

0 comments on commit 68d768a

Please sign in to comment.