Skip to content

Commit

Permalink
fix: make disk_iops a computed attribute in advanced_cluster resource (
Browse files Browse the repository at this point in the history
…#1620)

* fix: make disk_iops a computed attribute in advanced_cluster resource

* define migration test for advanced cluster using single and multi cloud provider configurations
  • Loading branch information
AgustinBettati authored Nov 15, 2023
1 parent 5b38090 commit 8166c7f
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 122 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/migration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
outputs:
project: ${{ steps.filter.outputs.project }}
config: ${{ steps.filter.outputs.config }}
advanced_cluster: ${{ steps.filter.outputs.advanced_cluster }}
backup_online_archive: ${{ steps.filter.outputs.backup_online_archive }}
shouldTriggerResourceTest: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' || github.event.label.name == 'run-testacc' || inputs.parent-event-name == 'release' }}
steps:
Expand All @@ -39,6 +40,8 @@ jobs:
- 'mongodbatlas/fw_resource_mongodbatlas_database_user*.go'
backup_online_archive:
- 'mongodbatlas/**online_archive**.go'
advanced_cluster:
- 'mongodbatlas/**advanced_cluster**.go'
project:
needs: [ change-detection ]
Expand Down Expand Up @@ -146,3 +149,35 @@ jobs:
PARALLEL_GO_TEST: 20
TEST_REGEX: "^TestAccMigrationBackup"
run: make testacc

advanced_cluster:
needs: [ change-detection ]
if: ${{ needs.change-detection.outputs.advanced_cluster == 'true' || github.event.label.name == 'run-testacc-advanced-cluster'|| needs.change-detection.outputs.shouldTriggerResourceTest == 'true'}}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get Last Release
id: get_last_release
run: |
LAST_RELEASE=$(curl -sSfL -X GET https://api.github.com/repos/mongodb/terraform-provider-mongodbatlas/releases/latest | jq -r '.tag_name | ltrimstr("v")')
echo "Last release: $LAST_RELEASE"
echo "MONGODB_ATLAS_LAST_VERSION=$LAST_RELEASE" >> $GITHUB_ENV
shell: bash
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
- 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 }}
SKIP_TEST_EXTERNAL_CREDENTIALS: ${{ vars.SKIP_TEST_EXTERNAL_CREDENTIALS }}
ACCTEST_TIMEOUT: ${{ vars.ACCTEST_TIMEOUT }}
TF_LOG: ${{ vars.LOG_LEVEL }}
TF_ACC: 1
PARALLEL_GO_TEST: 20
TEST_REGEX: "^TestAccMigrationAdvancedCluster"
run: make testacc
5 changes: 2 additions & 3 deletions mongodbatlas/resource_mongodbatlas_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func advancedClusterRegionConfigsSpecsSchema() *schema.Schema {
"disk_iops": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"ebs_volume_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -1219,9 +1220,7 @@ func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *matlas.Specs, pro

if providerName == "AWS" {
if cast.ToInt64(apiObject.DiskIOPS) > 0 {
if v, ok := tfMapObject["disk_iops"]; ok && v.(int) > 0 {
tfMap["disk_iops"] = apiObject.DiskIOPS
}
tfMap["disk_iops"] = apiObject.DiskIOPS
}
if v, ok := tfMapObject["ebs_volume_type"]; ok && v.(string) != "" {
tfMap["ebs_volume_type"] = apiObject.EbsVolumeType
Expand Down
197 changes: 84 additions & 113 deletions mongodbatlas/resource_mongodbatlas_advanced_cluster_migration_test.go
Original file line number Diff line number Diff line change
@@ -1,132 +1,103 @@
package mongodbatlas

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

func TestAccClusterRSAdvancedClusterMigrateState_empty_advancedConfig(t *testing.T) {
v0State := map[string]any{
"project_id": "test-id",
"name": "test-cluster",
"cluster_type": "REPLICASET",
"replication_specs": []any{
map[string]any{
"region_configs": []any{
map[string]any{
"electable_specs": []any{
map[string]any{
"instance_size": "M30",
"node_count": 3,
},
},
"provider_name": "AWS",
"region_name": "US_EAST_1",
"priority": 7,
func TestAccMigrationAdvancedClusterRS_singleAWSProvider(t *testing.T) {
var (
cluster matlas.AdvancedCluster
resourceName = "mongodbatlas_advanced_cluster.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
rName = acctest.RandomWithPrefix("test-acc")
lastVersionConstraint = os.Getenv("MONGODB_ATLAS_LAST_VERSION")
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccMigrationPreCheckBasic(t) },
CheckDestroy: testAccCheckMongoDBAtlasAdvancedClusterDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"mongodbatlas": {
VersionConstraint: lastVersionConstraint,
Source: "mongodb/mongodbatlas",
},
},
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProvider(orgID, projectName, rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "retain_backups_enabled", "true"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.region_configs.#"),
),
},
},
"bi_connector": []any{
map[string]any{
"enabled": 1,
"read_preference": "secondary",
{
ProtoV6ProviderFactories: testAccProviderV6Factories,
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProvider(orgID, projectName, rName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{
DebugPlan(),
},
},
PlanOnly: true,
},
},
}

v0Config := terraform.NewResourceConfigRaw(v0State)
diags := resourceMongoDBAtlasAdvancedClusterResourceV0().Validate(v0Config)

if len(diags) > 0 {
t.Error("test precondition failed - invalid mongodb cluster v0 config")

return
}

// test migrate function
v1State := migrateBIConnectorConfig(v0State)

v1Config := terraform.NewResourceConfigRaw(v1State)
diags = resourceMongoDBAtlasAdvancedCluster().Validate(v1Config)
if len(diags) > 0 {
fmt.Println(diags)
t.Error("migrated cluster advanced config is invalid")

return
}
})
}

func TestAccClusterRSAdvancedClusterV0StateUpgrade_ReplicationSpecs(t *testing.T) {
v0State := map[string]any{
"project_id": "test-id",
"name": "test-cluster",
"cluster_type": "REPLICASET",
"backup_enabled": true,
"disk_size_gb": 256,
"replication_specs": []any{
map[string]any{
"zone_name": "Test Zone",
"region_configs": []any{
map[string]any{
"priority": 7,
"provider_name": "AWS",
"region_name": "US_EAST_1",
"electable_specs": []any{
map[string]any{
"instance_size": "M30",
"node_count": 3,
},
},
"read_only_specs": []any{
map[string]any{
"disk_iops": 0,
"instance_size": "M30",
"node_count": 0,
},
},
"auto_scaling": []any{
map[string]any{
"compute_enabled": true,
"compute_max_instance_size": "M60",
"compute_min_instance_size": "M30",
"compute_scale_down_enabled": true,
"disk_gb_enabled": false,
},
},
func TestAccMigrationAdvancedClusterRS_multiCloud(t *testing.T) {
var (
cluster matlas.AdvancedCluster
resourceName = "mongodbatlas_advanced_cluster.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
rName = acctest.RandomWithPrefix("test-acc")
lastVersionConstraint = os.Getenv("MONGODB_ATLAS_LAST_VERSION")
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccMigrationPreCheckBasic(t) },
CheckDestroy: testAccCheckMongoDBAtlasAdvancedClusterDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"mongodbatlas": {
VersionConstraint: lastVersionConstraint,
Source: "mongodb/mongodbatlas",
},
},
Config: testAccMongoDBAtlasAdvancedClusterConfigMultiCloud(orgID, projectName, rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "retain_backups_enabled", "false"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.region_configs.#"),
),
},
{
ProtoV6ProviderFactories: testAccProviderV6Factories,
Config: testAccMongoDBAtlasAdvancedClusterConfigMultiCloud(orgID, projectName, rName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{
DebugPlan(),
},
},
PlanOnly: true,
},
},
}

v0Config := terraform.NewResourceConfigRaw(v0State)
diags := resourceMongoDBAtlasAdvancedClusterResourceV0().Validate(v0Config)

if len(diags) > 0 {
fmt.Println(diags)
t.Error("test precondition failed - invalid mongodb cluster v0 config")

return
}

// test migrate function
v1State := migrateBIConnectorConfig(v0State)

v1Config := terraform.NewResourceConfigRaw(v1State)
diags = resourceMongoDBAtlasAdvancedCluster().Validate(v1Config)
if len(diags) > 0 {
fmt.Println(diags)
t.Error("migrated advanced cluster replication_specs invalid")

return
}

if len(v1State["replication_specs"].([]any)) != len(v0State["replication_specs"].([]any)) {
t.Error("migrated replication specs did not contain the same number of elements")

return
}
})
}
Loading

0 comments on commit 8166c7f

Please sign in to comment.