Skip to content

Commit

Permalink
move ds schemas together with resource one
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Dec 15, 2024
1 parent 6937e40 commit 36973eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
12 changes: 1 addition & 11 deletions internal/service/advancedclustertpf/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
Expand All @@ -28,16 +27,7 @@ type ds struct {
}

func (d *ds) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = conversion.DataSourceSchemaFromResource(ResourceSchema(ctx), &conversion.DataSourceSchemaRequest{
RequiredFields: []string{"project_id", "name"},
OverridenFields: map[string]schema.Attribute{
"use_replication_spec_per_shard": schema.BoolAttribute{ // TODO: added as in current resource
Optional: true,
MarkdownDescription: "use_replication_spec_per_shard", // TODO: add documentation
},
"accept_data_risks_and_force_replica_set_reconfig": nil,
},
})
resp.Schema = dataSourceSchema(ctx)
}

func (d *ds) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down
29 changes: 1 addition & 28 deletions internal/service/advancedclustertpf/plural_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
Expand All @@ -31,26 +30,7 @@ type pluralDS struct {
}

func (d *pluralDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = conversion.PluralDataSourceSchemaFromResource(ResourceSchema(ctx), &conversion.PluralDataSourceSchemaRequest{
RequiredFields: []string{"project_id"},
OverridenRootFields: map[string]schema.Attribute{
"use_replication_spec_per_shard": schema.BoolAttribute{ // TODO: added as in current resource
Optional: true,
MarkdownDescription: "use_replication_spec_per_shard", // TODO: add documentation
},
"include_deleted_with_retained_backups": schema.BoolAttribute{ // TODO: not in current resource, decide if keep
Optional: true,
MarkdownDescription: "Flag that indicates whether to return Clusters with retain backups.",
},
},
OverridenFields: map[string]schema.Attribute{
"use_replication_spec_per_shard": schema.BoolAttribute{
Optional: true,
MarkdownDescription: "use_replication_spec_per_shard", // TODO: add documentation
},
"accept_data_risks_and_force_replica_set_reconfig": nil,
},
})
resp.Schema = pluralDataSourceSchema(ctx)
}

func (d *pluralDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down Expand Up @@ -115,10 +95,3 @@ func (d *pluralDS) readClusters(ctx context.Context, diags *diag.Diagnostics, pl
}
return outs
}

type TFModelPluralDS struct {
ProjectID types.String `tfsdk:"project_id"`
Results []*TFModelDS `tfsdk:"results"`
UseReplicationSpecPerShard types.Bool `tfsdk:"use_replication_spec_per_shard"` // TODO: added as in current resource
IncludeDeletedWithRetainedBackups types.Bool `tfsdk:"include_deleted_with_retained_backups"` // TODO: not in current resource, decide if keep
}
2 changes: 1 addition & 1 deletion internal/service/advancedclustertpf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type rs struct {
}

func (r *rs) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = ResourceSchema(ctx)
resp.Schema = resourceSchema(ctx)
conversion.UpdateSchemaDescription(&resp.Schema)
}

Expand Down
45 changes: 44 additions & 1 deletion internal/service/advancedclustertpf/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
dsschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/schemafunc"
)

func ResourceSchema(ctx context.Context) schema.Schema {
func resourceSchema(ctx context.Context) schema.Schema {
return schema.Schema{
Version: 1,
Attributes: map[string]schema.Attribute{
Expand Down Expand Up @@ -351,6 +353,40 @@ func ResourceSchema(ctx context.Context) schema.Schema {
}
}

func dataSourceSchema(ctx context.Context) dsschema.Schema {
return conversion.DataSourceSchemaFromResource(resourceSchema(ctx), &conversion.DataSourceSchemaRequest{
RequiredFields: []string{"project_id", "name"},
OverridenFields: dataSourceOverridenFields(),
})
}

func pluralDataSourceSchema(ctx context.Context) dsschema.Schema {
return conversion.PluralDataSourceSchemaFromResource(resourceSchema(ctx), &conversion.PluralDataSourceSchemaRequest{
RequiredFields: []string{"project_id"},
OverridenRootFields: map[string]dsschema.Attribute{
"use_replication_spec_per_shard": dsschema.BoolAttribute{ // TODO: added as in current resource
Optional: true,
MarkdownDescription: "use_replication_spec_per_shard", // TODO: add documentation
},
"include_deleted_with_retained_backups": dsschema.BoolAttribute{ // TODO: not in current resource, decide if keep
Optional: true,
MarkdownDescription: "Flag that indicates whether to return Clusters with retain backups.",
},
},
OverridenFields: dataSourceOverridenFields(),
})
}

func dataSourceOverridenFields() map[string]dsschema.Attribute {
return map[string]dsschema.Attribute{
"use_replication_spec_per_shard": dsschema.BoolAttribute{ // TODO: added as in current resource
Optional: true,
MarkdownDescription: "use_replication_spec_per_shard", // TODO: add documentation
},
"accept_data_risks_and_force_replica_set_reconfig": nil,
}
}

func AutoScalingSchema() schema.SingleNestedAttribute {
return schema.SingleNestedAttribute{
Computed: true,
Expand Down Expand Up @@ -571,6 +607,13 @@ type TFModelDS struct {
PitEnabled types.Bool `tfsdk:"pit_enabled"`
}

type TFModelPluralDS struct {
ProjectID types.String `tfsdk:"project_id"`
Results []*TFModelDS `tfsdk:"results"`
UseReplicationSpecPerShard types.Bool `tfsdk:"use_replication_spec_per_shard"` // TODO: added as in current resource
IncludeDeletedWithRetainedBackups types.Bool `tfsdk:"include_deleted_with_retained_backups"` // TODO: not in current resource, decide if keep
}

type TFBiConnectorModel struct {
ReadPreference types.String `tfsdk:"read_preference"`
Enabled types.Bool `tfsdk:"enabled"`
Expand Down

0 comments on commit 36973eb

Please sign in to comment.