From 68d768a8319baf1510715e3ee193b86b4ad65757 Mon Sep 17 00:00:00 2001 From: Mitchell Nielsen Date: Wed, 11 Dec 2024 13:33:39 -0600 Subject: [PATCH] chore: move common fields into BaseModel types (#320) * 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 --- internal/provider/datasources/account.go | 4 +-- internal/provider/datasources/account_role.go | 4 +-- .../provider/datasources/automation_model.go | 9 +++---- .../{helpers => datasources}/base_model.go | 2 +- internal/provider/datasources/block.go | 9 +++---- internal/provider/datasources/deployment.go | 8 +++--- .../provider/datasources/service_account.go | 4 +-- internal/provider/datasources/team.go | 9 +++---- internal/provider/datasources/variable.go | 9 +++---- internal/provider/datasources/work_pool.go | 9 +++---- internal/provider/datasources/workspace.go | 7 +++-- .../provider/datasources/workspace_role.go | 4 +-- internal/provider/resources/account.go | 4 +-- .../provider/resources/automation_model.go | 9 +++---- internal/provider/resources/base_model.go | 14 ++++++++++ internal/provider/resources/block.go | 9 +++---- internal/provider/resources/deployment.go | 4 +-- .../provider/resources/deployment_schedule.go | 6 ++++- internal/provider/resources/flow.go | 9 +++---- .../provider/resources/service_account.go | 4 +-- internal/provider/resources/variable.go | 26 +++++++++---------- internal/provider/resources/work_pool.go | 9 +++---- internal/provider/resources/workspace.go | 7 +++-- internal/provider/resources/workspace_role.go | 4 +-- 24 files changed, 88 insertions(+), 95 deletions(-) rename internal/provider/{helpers => datasources}/base_model.go (94%) create mode 100644 internal/provider/resources/base_model.go diff --git a/internal/provider/datasources/account.go b/internal/provider/datasources/account.go index 8f42030c..d857348a 100644 --- a/internal/provider/datasources/account.go +++ b/internal/provider/datasources/account.go @@ -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"` diff --git a/internal/provider/datasources/account_role.go b/internal/provider/datasources/account_role.go index d5dcb5f4..feac8a3a 100644 --- a/internal/provider/datasources/account_role.go +++ b/internal/provider/datasources/account_role.go @@ -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"` diff --git a/internal/provider/datasources/automation_model.go b/internal/provider/datasources/automation_model.go index 4a34bd32..bf20b475 100644 --- a/internal/provider/datasources/automation_model.go +++ b/internal/provider/datasources/automation_model.go @@ -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"` diff --git a/internal/provider/helpers/base_model.go b/internal/provider/datasources/base_model.go similarity index 94% rename from internal/provider/helpers/base_model.go rename to internal/provider/datasources/base_model.go index 2fcaa053..a8bbb2d3 100644 --- a/internal/provider/helpers/base_model.go +++ b/internal/provider/datasources/base_model.go @@ -1,4 +1,4 @@ -package helpers +package datasources import "github.com/prefecthq/terraform-provider-prefect/internal/provider/customtypes" diff --git a/internal/provider/datasources/block.go b/internal/provider/datasources/block.go index 475f326e..ffcac892 100644 --- a/internal/provider/datasources/block.go +++ b/internal/provider/datasources/block.go @@ -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"` diff --git a/internal/provider/datasources/deployment.go b/internal/provider/datasources/deployment.go index 930db590..530ce0c7 100644 --- a/internal/provider/datasources/deployment.go +++ b/internal/provider/datasources/deployment.go @@ -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, diff --git a/internal/provider/datasources/service_account.go b/internal/provider/datasources/service_account.go index 7e0e1e48..a5b75925 100644 --- a/internal/provider/datasources/service_account.go +++ b/internal/provider/datasources/service_account.go @@ -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"` diff --git a/internal/provider/datasources/team.go b/internal/provider/datasources/team.go index 43ae52b1..90add497 100644 --- a/internal/provider/datasources/team.go +++ b/internal/provider/datasources/team.go @@ -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"` } diff --git a/internal/provider/datasources/variable.go b/internal/provider/datasources/variable.go index 95685d3b..60779972 100644 --- a/internal/provider/datasources/variable.go +++ b/internal/provider/datasources/variable.go @@ -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"` diff --git a/internal/provider/datasources/work_pool.go b/internal/provider/datasources/work_pool.go index 1bcd66a3..1e15840c 100644 --- a/internal/provider/datasources/work_pool.go +++ b/internal/provider/datasources/work_pool.go @@ -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"` diff --git a/internal/provider/datasources/workspace.go b/internal/provider/datasources/workspace.go index 7d40349c..b1cd5d52 100644 --- a/internal/provider/datasources/workspace.go +++ b/internal/provider/datasources/workspace.go @@ -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"` diff --git a/internal/provider/datasources/workspace_role.go b/internal/provider/datasources/workspace_role.go index 316b35cc..6c94fbfe 100644 --- a/internal/provider/datasources/workspace_role.go +++ b/internal/provider/datasources/workspace_role.go @@ -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"` diff --git a/internal/provider/resources/account.go b/internal/provider/resources/account.go index b1c59140..6c93f486 100644 --- a/internal/provider/resources/account.go +++ b/internal/provider/resources/account.go @@ -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"` diff --git a/internal/provider/resources/automation_model.go b/internal/provider/resources/automation_model.go index 6b296419..6a20db55 100644 --- a/internal/provider/resources/automation_model.go +++ b/internal/provider/resources/automation_model.go @@ -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"` diff --git a/internal/provider/resources/base_model.go b/internal/provider/resources/base_model.go new file mode 100644 index 00000000..7e7e1fed --- /dev/null +++ b/internal/provider/resources/base_model.go @@ -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"` +} diff --git a/internal/provider/resources/block.go b/internal/provider/resources/block.go index d9489745..a0cc4f7d 100644 --- a/internal/provider/resources/block.go +++ b/internal/provider/resources/block.go @@ -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"` diff --git a/internal/provider/resources/deployment.go b/internal/provider/resources/deployment.go index 2c372ccf..87751e98 100644 --- a/internal/provider/resources/deployment.go +++ b/internal/provider/resources/deployment.go @@ -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"` diff --git a/internal/provider/resources/deployment_schedule.go b/internal/provider/resources/deployment_schedule.go index 4142870a..23437890 100644 --- a/internal/provider/resources/deployment_schedule.go +++ b/internal/provider/resources/deployment_schedule.go @@ -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"` diff --git a/internal/provider/resources/flow.go b/internal/provider/resources/flow.go index 03fbdf47..be32d5b0 100644 --- a/internal/provider/resources/flow.go +++ b/internal/provider/resources/flow.go @@ -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"` diff --git a/internal/provider/resources/service_account.go b/internal/provider/resources/service_account.go index c0e5428e..e805d3bd 100644 --- a/internal/provider/resources/service_account.go +++ b/internal/provider/resources/service_account.go @@ -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"` diff --git a/internal/provider/resources/variable.go b/internal/provider/resources/variable.go index 4e295c51..95bdb798 100644 --- a/internal/provider/resources/variable.go +++ b/internal/provider/resources/variable.go @@ -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"` @@ -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"` @@ -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, diff --git a/internal/provider/resources/work_pool.go b/internal/provider/resources/work_pool.go index 5a8e5481..15a5d4cf 100644 --- a/internal/provider/resources/work_pool.go +++ b/internal/provider/resources/work_pool.go @@ -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"` diff --git a/internal/provider/resources/workspace.go b/internal/provider/resources/workspace.go index 19b23691..bb76a5d5 100644 --- a/internal/provider/resources/workspace.go +++ b/internal/provider/resources/workspace.go @@ -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"` diff --git a/internal/provider/resources/workspace_role.go b/internal/provider/resources/workspace_role.go index baf019bf..74691606 100644 --- a/internal/provider/resources/workspace_role.go +++ b/internal/provider/resources/workspace_role.go @@ -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"`