Skip to content

Commit

Permalink
chore: Updates search_deployment resource with new SDK (#1853)
Browse files Browse the repository at this point in the history
* remove NonEmptyToPtr

* remove NonEmptyToPtr

* rename test helper functions

* migration test config for search_deployment test group

* migration test

* fix CheckDestroy for mig tests

* fix checkExists

* add "updated" to fix flaky test as sometimes created time differs in a second
  • Loading branch information
lantoli committed Jan 22, 2024
1 parent 0b48aa2 commit cdfaef8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 47 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/migration-tests.yml
Expand Up @@ -62,6 +62,7 @@ jobs:
backup_online_archive: ${{ steps.filter.outputs.backup_online_archive == 'true' || env.mustTrigger == 'true' }}
stream: ${{ steps.filter.outputs.stream == 'true' || env.mustTrigger == 'true' }}
federation: ${{ steps.filter.outputs.federation == 'true' || env.mustTrigger == 'true' }}
search_deployment: ${{ steps.filter.outputs.search_deployment == 'true' || env.mustTrigger == 'true' }}
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dorny/paths-filter@4512585405083f25c027a35db413c2b3b9006d50
Expand All @@ -86,6 +87,8 @@ jobs:
- 'internal/service/streamconnection/*.go'
federation:
- 'internal/service/federateddatabaseinstance/*.go'
search_deployment:
- 'internal/service/searchdeployment/*.go'
project:
needs: [ change-detection, get-provider-version ]
Expand Down Expand Up @@ -228,6 +231,7 @@ jobs:
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
TEST_REGEX: "^TestAccMigrationStream"
run: make testacc

federation:
needs: [ change-detection, get-provider-version ]
if: ${{ needs.change-detection.outputs.federation == 'true' || inputs.test_group == 'federation' }}
Expand Down Expand Up @@ -256,3 +260,28 @@ jobs:
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
TEST_REGEX: "^TestAccMigrationFederatedDatabaseInstance"
run: make testacc

search_deployment:
needs: [ change-detection, get-provider-version ]
if: ${{ needs.change-detection.outputs.search_deployment == 'true' || inputs.test_group == 'search_deployment' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.terraform_version }}
terraform_wrapper: false
- name: Migration Tests
env:
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV }}
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }}
MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
MONGODB_ATLAS_BASE_URL: ${{ vars.MONGODB_ATLAS_BASE_URL }}
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
TEST_REGEX: "^TestAccMigrationSearchDeployment"
run: make testacc
8 changes: 0 additions & 8 deletions internal/common/conversion/type_conversion.go
Expand Up @@ -56,11 +56,3 @@ func IsStringPresent(strPtr *string) bool {
func MongoDBRegionToAWSRegion(region string) string {
return strings.ReplaceAll(strings.ToLower(region), "_", "-")
}

// NonEmptyToPtr accepts an array or slice and returns a pointer to it, except if it's empty, in that case it returns nil.
func NonEmptyToPtr[T any](v []T) *[]T {
if len(v) == 0 {
return nil
}
return &v
}
23 changes: 0 additions & 23 deletions internal/common/conversion/type_conversion_test.go
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/stretchr/testify/assert"
)

func TestTimeToStringWithoutNanos(t *testing.T) {
Expand Down Expand Up @@ -68,25 +67,3 @@ func TestMongoDBRegionToAWSRegion(t *testing.T) {
}
}
}

func TestNonEmptyToPtr(t *testing.T) {
var (
nilSlice []string
emptyNonNilSlice = []string{}
)
tests := []struct {
name string
expected *[]string
given []string
}{
{"nil pointer", nil, nil},
{"nil slice", nil, nilSlice},
{"empty non-nil slice", nil, emptyNonNilSlice},
{"slice with content", &[]string{"hello", "there"}, []string{"hello", "there"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, conversion.NonEmptyToPtr(tc.given))
})
}
}
Expand Up @@ -237,7 +237,7 @@ func TestAccConfigRSAlertConfiguration_withThresholdUpdated(t *testing.T) {
ImportStateIdFunc: importStateProjectIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"project_id", "matcher.0.field_name"},
ImportStateVerifyIgnore: []string{"project_id", "updated", "matcher.0.field_name"},
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion internal/service/project/model_project_test.go
Expand Up @@ -251,7 +251,7 @@ func TestProjectDataSourceSDKToResourceTFModel(t *testing.T) {
project: &projectSDK,
projectProps: project.AdditionalProperties{
Teams: &admin.PaginatedTeamRole{
Results: conversion.NonEmptyToPtr(teamRolesSDK),
Results: &teamRolesSDK,
TotalCount: conversion.IntPtr(1),
},
Settings: &projectSettingsSDK,
Expand Down
4 changes: 1 addition & 3 deletions internal/service/searchdeployment/model_search_deployment.go
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"go.mongodb.org/atlas-sdk/v20231115004/admin"
)

Expand All @@ -21,9 +20,8 @@ func NewSearchDeploymentReq(ctx context.Context, searchDeploymentPlan *TFSearchD
NodeCount: int(spec.NodeCount.ValueInt64()),
}
}

return admin.ApiSearchDeploymentRequest{
Specs: conversion.NonEmptyToPtr(resultSpecs),
Specs: &resultSpecs,
}
}

Expand Down
@@ -0,0 +1,44 @@
package searchdeployment_test

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
)

func TestAccMigrationSearchDeployment_basic(t *testing.T) {
var (
resourceName = "mongodbatlas_search_deployment.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc-search-dep")
clusterName = acctest.RandomWithPrefix("test-acc-search-dep")
instanceSize = "S30_HIGHCPU_NVME"
searchNodeCount = 3
)
mig.SkipIfVersionBelow(t, "1.13.0")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: configBasic(orgID, projectName, clusterName, instanceSize, searchNodeCount),
Check: resource.ComposeTestCheckFunc(searchNodeChecks(resourceName, clusterName, instanceSize, searchNodeCount)...),
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: configBasic(orgID, projectName, clusterName, instanceSize, searchNodeCount),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
}
Expand Up @@ -23,14 +23,14 @@ func TestAccSearchDeployment_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasSearchNodeDestroy,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, "S20_HIGHCPU_NVME", 3),
newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, "S30_HIGHCPU_NVME", 4),
{
Config: testAccMongoDBAtlasSearchDeploymentConfig(orgID, projectName, clusterName, "S30_HIGHCPU_NVME", 4),
Config: configBasic(orgID, projectName, clusterName, "S30_HIGHCPU_NVME", 4),
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckSearchNodeImportStateIDFunc(resourceName),
ImportStateIdFunc: importStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
},
Expand All @@ -42,14 +42,14 @@ func newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, instan
resourceChecks := searchNodeChecks(resourceName, clusterName, instanceSize, searchNodeCount)
dataSourceChecks := searchNodeChecks(fmt.Sprintf("data.%s", resourceName), clusterName, instanceSize, searchNodeCount)
return resource.TestStep{
Config: testAccMongoDBAtlasSearchDeploymentConfig(orgID, projectName, clusterName, instanceSize, searchNodeCount),
Config: configBasic(orgID, projectName, clusterName, instanceSize, searchNodeCount),
Check: resource.ComposeTestCheckFunc(append(resourceChecks, dataSourceChecks...)...),
}
}

func searchNodeChecks(targetName, clusterName, instanceSize string, searchNodeCount int) []resource.TestCheckFunc {
return []resource.TestCheckFunc{
testAccCheckMongoDBAtlasSearchNodeExists(targetName),
checkExists(targetName),
resource.TestCheckResourceAttrSet(targetName, "id"),
resource.TestCheckResourceAttrSet(targetName, "project_id"),
resource.TestCheckResourceAttr(targetName, "cluster_name", clusterName),
Expand All @@ -59,7 +59,7 @@ func searchNodeChecks(targetName, clusterName, instanceSize string, searchNodeCo
}
}

func testAccMongoDBAtlasSearchDeploymentConfig(orgID, projectName, clusterName, instanceSize string, searchNodeCount int) string {
func configBasic(orgID, projectName, clusterName, instanceSize string, searchNodeCount int) string {
clusterConfig := advancedClusterConfig(orgID, projectName, clusterName)
return fmt.Sprintf(`
%[1]s
Expand Down Expand Up @@ -109,7 +109,7 @@ func advancedClusterConfig(orgID, projectName, clusterName string) string {
`, orgID, projectName, clusterName)
}

func testAccCheckSearchNodeImportStateIDFunc(resourceName string) resource.ImportStateIdFunc {
func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
Expand All @@ -119,14 +119,14 @@ func testAccCheckSearchNodeImportStateIDFunc(resourceName string) resource.Impor
}
}

func testAccCheckMongoDBAtlasSearchNodeExists(resourceName string) resource.TestCheckFunc {
func checkExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}

connV2 := acc.TestAccProviderSdkV2.Meta().(*config.MongoDBClient).AtlasV2
connV2 := acc.TestMongoDBClient.(*config.MongoDBClient).AtlasV2
_, _, err := connV2.AtlasSearchApi.GetAtlasSearchDeployment(context.Background(), rs.Primary.Attributes["project_id"], rs.Primary.Attributes["cluster_name"]).Execute()
if err != nil {
return fmt.Errorf("search deployment (%s:%s) does not exist", rs.Primary.Attributes["project_id"], rs.Primary.Attributes["cluster_name"])
Expand All @@ -135,14 +135,14 @@ func testAccCheckMongoDBAtlasSearchNodeExists(resourceName string) resource.Test
}
}

func testAccCheckMongoDBAtlasSearchNodeDestroy(state *terraform.State) error {
func checkDestroy(state *terraform.State) error {
if projectDestroyedErr := acc.CheckDestroyProject(state); projectDestroyedErr != nil {
return projectDestroyedErr
}
if clusterDestroyedErr := acc.CheckDestroyTeamAdvancedCluster(state); clusterDestroyedErr != nil {
return clusterDestroyedErr
}
connV2 := acc.TestAccProviderSdkV2.Meta().(*config.MongoDBClient).AtlasV2
connV2 := acc.TestMongoDBClient.(*config.MongoDBClient).AtlasV2
for _, rs := range state.RootModule().Resources {
if rs.Type == "mongodbatlas_search_deployment" {
_, _, err := connV2.AtlasSearchApi.GetAtlasSearchDeployment(context.Background(), rs.Primary.Attributes["project_id"], rs.Primary.Attributes["cluster_name"]).Execute()
Expand Down

0 comments on commit cdfaef8

Please sign in to comment.