Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Adjusts search_deployment schema definitions with structure of new schema scaffolding command #1830

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

Expand All @@ -20,52 +18,16 @@ func DataSource() datasource.DataSource {
}
}

type tfSearchDeploymentDSModel struct {
ID types.String `tfsdk:"id"`
ClusterName types.String `tfsdk:"cluster_name"`
ProjectID types.String `tfsdk:"project_id"`
Specs types.List `tfsdk:"specs"`
StateName types.String `tfsdk:"state_name"`
}

type searchDeploymentDS struct {
config.DSCommon
}

func (d *searchDeploymentDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"cluster_name": schema.StringAttribute{
Required: true,
},
"project_id": schema.StringAttribute{
Required: true,
},
"specs": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"instance_size": schema.StringAttribute{
Computed: true,
},
"node_count": schema.Int64Attribute{
Computed: true,
},
},
},
Computed: true,
},
"state_name": schema.StringAttribute{
Computed: true,
},
},
}
resp.Schema = DataSourceSchema(ctx)
}

func (d *searchDeploymentDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var searchDeploymentConfig tfSearchDeploymentDSModel
var searchDeploymentConfig TFSearchDeploymentDSModel
resp.Diagnostics.Append(req.Config.Get(ctx, &searchDeploymentConfig)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -89,8 +51,8 @@ func (d *searchDeploymentDS) Read(ctx context.Context, req datasource.ReadReques
resp.Diagnostics.Append(resp.State.Set(ctx, dsModel)...)
}

func convertToDSModel(inputModel *TFSearchDeploymentRSModel) tfSearchDeploymentDSModel {
return tfSearchDeploymentDSModel{
func convertToDSModel(inputModel *TFSearchDeploymentRSModel) TFSearchDeploymentDSModel {
return TFSearchDeploymentDSModel{
ID: inputModel.ID,
ClusterName: inputModel.ClusterName,
ProjectID: inputModel.ProjectID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package searchdeployment

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func DataSourceSchema(ctx context.Context) schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Unique 24-hexadecimal digit string that identifies the search deployment.",
MarkdownDescription: "Unique 24-hexadecimal digit string that identifies the search deployment.",
},
"cluster_name": schema.StringAttribute{
Required: true,
Description: "Label that identifies the cluster to return the search nodes for.",
MarkdownDescription: "Label that identifies the cluster to return the search nodes for.",
Copy link
Member

@lantoli lantoli Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will scaffolding be used only for new TF plugin or also for older one? just to understand if it's enough to generate MarkdownDescription or we also want to generate Description.

or maybe having an input param (defaulted to new plugin?) in the tool if we want to support both?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tool is limited to generating new TF framework code. I have left both definitions as they are both generated by the tool and provide unique value as per comments in the terraform code:

  • MarkdownDescription: used in various tooling, like the documentation generator
  • Description: used in various tooling, like the language server, to give practitioners more information about what this attribute is, what it's for, and how it should be used.

},
"project_id": schema.StringAttribute{
Required: true,
Description: "Unique 24-hexadecimal digit string that identifies your project.",
MarkdownDescription: "Unique 24-hexadecimal digit string that identifies your project.",
},
"specs": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"instance_size": schema.StringAttribute{
Computed: true,
Description: "Hardware specification for the search node instance sizes.",
MarkdownDescription: "Hardware specification for the search node instance sizes.",
},
"node_count": schema.Int64Attribute{
Computed: true,
Description: "Number of search nodes in the cluster.",
MarkdownDescription: "Number of search nodes in the cluster.",
},
},
},
Description: "List of settings that configure the search nodes for your cluster.",
MarkdownDescription: "List of settings that configure the search nodes for your cluster.",
},
"state_name": schema.StringAttribute{
Computed: true,
Description: "Human-readable label that indicates the current operating condition of this search deployment.",
MarkdownDescription: "Human-readable label that indicates the current operating condition of this search deployment.",
},
},
}
}

type TFSearchDeploymentDSModel struct {
ID types.String `tfsdk:"id"`
ClusterName types.String `tfsdk:"cluster_name"`
ProjectID types.String `tfsdk:"project_id"`
Specs types.List `tfsdk:"specs"`
StateName types.String `tfsdk:"state_name"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ import (
"regexp"
"time"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"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"
"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/retrystrategy"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)
Expand All @@ -37,70 +29,8 @@ type searchDeploymentRS struct {
config.RSCommon
}

type TFSearchDeploymentRSModel struct {
ID types.String `tfsdk:"id"`
ClusterName types.String `tfsdk:"cluster_name"`
ProjectID types.String `tfsdk:"project_id"`
Specs types.List `tfsdk:"specs"`
StateName types.String `tfsdk:"state_name"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

type TFSearchNodeSpecModel struct {
InstanceSize types.String `tfsdk:"instance_size"`
NodeCount types.Int64 `tfsdk:"node_count"`
}

var SpecObjectType = types.ObjectType{AttrTypes: map[string]attr.Type{
"instance_size": types.StringType,
"node_count": types.Int64Type,
}}

func (r *searchDeploymentRS) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"cluster_name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"project_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"specs": schema.ListNestedAttribute{
Validators: []validator.List{
listvalidator.SizeAtMost(1),
listvalidator.SizeAtLeast(1),
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"instance_size": schema.StringAttribute{
Required: true,
},
"node_count": schema.Int64Attribute{
Required: true,
},
},
},
Required: true,
},
"state_name": schema.StringAttribute{
Computed: true,
},
"timeouts": timeouts.Attributes(ctx, timeouts.Opts{
Create: true,
Update: true,
Delete: true,
}),
},
}
resp.Schema = ResourceSchema(ctx)
}

const defaultSearchNodeTimeout time.Duration = 3 * time.Hour
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package searchdeployment

import (
"context"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func ResourceSchema(ctx context.Context) schema.Schema {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is creating the schema in a separate file a new convention in the repo? Is the what the scaffolding command will do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, scaffolding command will be adjusted to this convention of having the schema and tf models in separate files.

return schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Unique 24-hexadecimal digit string that identifies the search deployment.",
MarkdownDescription: "Unique 24-hexadecimal digit string that identifies the search deployment.",
},
"cluster_name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Description: "Label that identifies the cluster to return the search nodes for.",
MarkdownDescription: "Label that identifies the cluster to return the search nodes for.",
},
"project_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Description: "Unique 24-hexadecimal character string that identifies the project.",
MarkdownDescription: "Unique 24-hexadecimal character string that identifies the project.",
},
"specs": schema.ListNestedAttribute{
Validators: []validator.List{
listvalidator.SizeAtMost(1),
listvalidator.SizeAtLeast(1),
},
Required: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"instance_size": schema.StringAttribute{
Required: true,
Description: "Hardware specification for the search node instance sizes.",
MarkdownDescription: "Hardware specification for the search node instance sizes.",
},
"node_count": schema.Int64Attribute{
Required: true,
Description: "Number of search nodes in the cluster.",
MarkdownDescription: "Number of search nodes in the cluster.",
},
},
},
Description: "List of settings that configure the search nodes for your cluster.",
MarkdownDescription: "List of settings that configure the search nodes for your cluster.",
},
"state_name": schema.StringAttribute{
Computed: true,
Description: "Human-readable label that indicates the current operating condition of this search deployment.",
MarkdownDescription: "Human-readable label that indicates the current operating condition of this search deployment.",
},
"timeouts": timeouts.Attributes(ctx, timeouts.Opts{
Create: true,
Update: true,
Delete: true,
}),
},
}
}

type TFSearchDeploymentRSModel struct {
ID types.String `tfsdk:"id"`
ClusterName types.String `tfsdk:"cluster_name"`
ProjectID types.String `tfsdk:"project_id"`
Specs types.List `tfsdk:"specs"`
StateName types.String `tfsdk:"state_name"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

type TFSearchNodeSpecModel struct {
InstanceSize types.String `tfsdk:"instance_size"`
NodeCount types.Int64 `tfsdk:"node_count"`
}

var SpecObjectType = types.ObjectType{AttrTypes: map[string]attr.Type{
"instance_size": types.StringType,
"node_count": types.Int64Type,
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
provider:
name: mongodbatlas

resources:
search_deployment:
read:
path: /api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/deployment
method: GET
create:
path: /api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/deployment
method: POST
schema:
ignores: []


data_sources:
search_deployment:
read:
path: /api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/deployment
method: GET
schema:
ignores: []
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved