Skip to content

Commit

Permalink
refactor: Adjusts search_deployment schema definitions with structure…
Browse files Browse the repository at this point in the history
… of new schema scaffolding command (#1830)

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

* adding final new line in generator_config.yml

Co-authored-by: Andrea Angiolillo <[email protected]>

---------

Co-authored-by: Andrea Angiolillo <[email protected]>
  • Loading branch information
AgustinBettati and andreaangiolillo authored Jan 12, 2024
1 parent d1fba18 commit d8eee33
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 113 deletions.
46 changes: 4 additions & 42 deletions internal/service/searchdeployment/data_source_search_deployment.go
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.",
},
"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"`
}
72 changes: 1 addition & 71 deletions internal/service/searchdeployment/resource_search_deployment.go
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 {
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,
}}
22 changes: 22 additions & 0 deletions internal/service/searchdeployment/tfplugingen/generator_config.yml
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: []

0 comments on commit d8eee33

Please sign in to comment.