Skip to content

Commit

Permalink
feat: implement search node data source with acceptance tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinBettati committed Nov 14, 2023
1 parent 1034904 commit 5815052
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 16 deletions.
99 changes: 99 additions & 0 deletions mongodbatlas/fw_data_source_mongodbatlas_search_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package mongodbatlas

import (
"context"

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

var _ datasource.DataSource = &SearchNodeDS{}
var _ datasource.DataSourceWithConfigure = &SearchNodeDS{}

func NewSearchNodeDS() datasource.DataSource {
return &SearchNodeDS{
DSCommon: DSCommon{
dataSourceName: searchNodeName,
},
}
}

type tfSearchNodeDSModel 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 SearchNodeDS struct {
DSCommon
}

func (d *SearchNodeDS) 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,
},
},
}
}

func (d *SearchNodeDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var searchNodeConfig tfSearchNodeDSModel
resp.Diagnostics.Append(req.Config.Get(ctx, &searchNodeConfig)...)
if resp.Diagnostics.HasError() {
return
}

connV2 := d.client.AtlasV2
projectID := searchNodeConfig.ProjectID.ValueString()
clusterName := searchNodeConfig.ClusterName.ValueString()
deploymentResp, _, err := connV2.AtlasSearchApi.GetAtlasSearchDeployment(ctx, projectID, clusterName).Execute()
if err != nil {
resp.Diagnostics.AddError("error getting search node information", err.Error())
return
}

newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, nil)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
}
dsModel := convertToDSModel(newSearchNodeModel)
resp.Diagnostics.Append(resp.State.Set(ctx, dsModel)...)
}

func convertToDSModel(inputModel *tfSearchNodeRSModel) tfSearchNodeDSModel {
return tfSearchNodeDSModel{
ID: inputModel.ID,
ClusterName: inputModel.ClusterName,
ProjectID: inputModel.ProjectID,
Specs: inputModel.Specs,
StateName: inputModel.StateName,
}
}
1 change: 1 addition & 0 deletions mongodbatlas/fw_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ func (p *MongodbtlasProvider) DataSources(context.Context) []func() datasource.D
NewProjectIPAccessListDS,
NewAtlasUserDS,
NewAtlasUsersDS,
NewSearchNodeDS,
}
}

Expand Down
19 changes: 11 additions & 8 deletions mongodbatlas/fw_resource_mongodbatlas_search_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ import (
"go.mongodb.org/atlas-sdk/v20231001002/admin"
)

var _ resource.ResourceWithConfigure = &ProjectRS{}
var _ resource.ResourceWithImportState = &ProjectRS{}
var _ resource.ResourceWithConfigure = &SearchNodeRS{}
var _ resource.ResourceWithImportState = &SearchNodeRS{}

const (
searchNodeDoesNotExistsError = "ATLAS_FTS_DEPLOYMENT_DOES_NOT_EXIST"
searchNodeName = "search_node"
)

func NewSearchNodeRS() resource.Resource {
return &SearchNodeRS{
RSCommon: RSCommon{
resourceName: "search_node",
resourceName: searchNodeName,
},
}
}
Expand Down Expand Up @@ -139,7 +140,7 @@ func (r *SearchNodeRS) Create(ctx context.Context, req resource.CreateRequest, r
resp.Diagnostics.AddError("error during search node creation", err.Error())
return
}
newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, searchNodePlan.Timeouts)
newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, &searchNodePlan.Timeouts)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -163,7 +164,7 @@ func (r *SearchNodeRS) Read(ctx context.Context, req resource.ReadRequest, resp
return
}

newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, searchNodePlan.Timeouts)
newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, &searchNodePlan.Timeouts)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -197,7 +198,7 @@ func (r *SearchNodeRS) Update(ctx context.Context, req resource.UpdateRequest, r
resp.Diagnostics.AddError("error during search node update", err.Error())
return
}
newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, searchNodePlan.Timeouts)
newSearchNodeModel, diagnostics := newTFSearchDeployment(ctx, clusterName, deploymentResp, &searchNodePlan.Timeouts)
resp.Diagnostics.Append(diagnostics...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -333,13 +334,15 @@ func newSearchDeploymentReq(ctx context.Context, searchNodePlan *tfSearchNodeRSM
}
}

func newTFSearchDeployment(ctx context.Context, clusterName string, deployResp *admin.ApiSearchDeploymentResponse, timeout timeouts.Value) (*tfSearchNodeRSModel, diag.Diagnostics) {
func newTFSearchDeployment(ctx context.Context, clusterName string, deployResp *admin.ApiSearchDeploymentResponse, timeout *timeouts.Value) (*tfSearchNodeRSModel, diag.Diagnostics) {
result := tfSearchNodeRSModel{
ID: types.StringPointerValue(deployResp.Id),
ClusterName: types.StringValue(clusterName),
ProjectID: types.StringPointerValue(deployResp.GroupId),
StateName: types.StringPointerValue(deployResp.StateName),
Timeouts: timeout,
}
if timeout != nil {
result.Timeouts = *timeout
}

specsList, diagnostics := types.ListValueFrom(ctx, SpecObjectType, newTFSpecsModel(deployResp.Specs))
Expand Down
28 changes: 20 additions & 8 deletions mongodbatlas/fw_resource_mongodbatlas_search_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ func TestAccSearchNode_basic(t *testing.T) {
}

func newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, instanceSize string, searchNodeCount int) resource.TestStep {
resourceChecks := searchNodeChecks(resourceName, clusterName, instanceSize, searchNodeCount)
dataSourceChecks := searchNodeChecks(fmt.Sprintf("data.%s", resourceName), clusterName, instanceSize, searchNodeCount)
return resource.TestStep{
Config: testAccMongoDBAtlasSearchNodeConfig(orgID, projectName, clusterName, instanceSize, searchNodeCount),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasSearchNodeExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
resource.TestCheckResourceAttr(resourceName, "specs.0.instance_size", instanceSize),
resource.TestCheckResourceAttr(resourceName, "specs.0.node_count", fmt.Sprintf("%d", searchNodeCount)),
resource.TestCheckResourceAttrSet(resourceName, "state_name"),
),
Check: resource.ComposeTestCheckFunc(append(resourceChecks, dataSourceChecks...)...),
}
}

func searchNodeChecks(targetName, clusterName, instanceSize string, searchNodeCount int) []resource.TestCheckFunc {
return []resource.TestCheckFunc{
testAccCheckMongoDBAtlasSearchNodeExists(targetName),
resource.TestCheckResourceAttrSet(targetName, "id"),
resource.TestCheckResourceAttrSet(targetName, "project_id"),
resource.TestCheckResourceAttr(targetName, "cluster_name", clusterName),
resource.TestCheckResourceAttr(targetName, "specs.0.instance_size", instanceSize),
resource.TestCheckResourceAttr(targetName, "specs.0.node_count", fmt.Sprintf("%d", searchNodeCount)),
resource.TestCheckResourceAttrSet(targetName, "state_name"),
}
}

Expand All @@ -65,6 +72,11 @@ func testAccMongoDBAtlasSearchNodeConfig(orgID, projectName, clusterName, instan
}
]
}
data "mongodbatlas_search_node" "test" {
project_id = mongodbatlas_project.test.id
cluster_name = mongodbatlas_advanced_cluster.test.name
}
`, clusterConfig, instanceSize, searchNodeCount)
}

Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/search_node.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
layout: "mongodbatlas"
page_title: "MongoDB Atlas: search node"
sidebar_current: "docs-mongodbatlas-datasource-search-node"
description: |-
Describes a Search Node.
---

# Data Source: mongodbatlas_search_node

`mongodbatlas_search_node` describes a search node deployment.

## Example Usage

```terraform
data "mongodbatlas_search_node" "test" {
project_id = "<PROJECT_ID>"
cluster_name = "<CLUSTER_NAME>"
}
```

## Argument Reference

* `project_id` - (Required) The unique identifier for the [project](https://docs.atlas.mongodb.com/organizations-projects/#std-label-projects) that contains the specified cluster.
* `cluster_name` - (Required) The name of the cluster containing a search node deployment.

## Attributes Reference

* `specs` - List of settings that configure the search nodes for your cluster. See [specs](#specs).
* `state_name` - Human-readable label that indicates the current operating condition of this search node deployment.

### Specs
TODO: add proper link here
* `instance_size` - (Required) Hardware specification for the search node instance sizes. The [MongoDB Atlas API](https://docs.atlas.mongodb.com/reference/api/) describes the valid values. More details can also be found in the [Search Node Documentation](https://www.mongodb.com/docs/atlas/cluster-config/multi-cloud-distribution/#search-tier).
* `node_count` - (Required) Number of search nodes in the cluster.


TODO: add proper link here
For more information see: [MongoDB Atlas API - Search Node](https://docs.atlas.mongodb.com/reference/api/) Documentation.

0 comments on commit 5815052

Please sign in to comment.