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

test: Refactors GetClusterInfo #2426

Merged
merged 9 commits into from
Jul 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectO
retention_value = 1
}
}
`, info.ClusterTerraformStr, info.ClusterResourceName)
`, info.TerraformStr, info.ResourceName)
}

func configClusterWithBackupSchedule(projectName, orgID, projectOwnerID string, info *acc.ClusterInfo) string {
Expand All @@ -402,7 +402,7 @@ func configClusterWithBackupSchedule(projectName, orgID, projectOwnerID string,
should_copy_oplogs = false
}
}
`, info.ClusterTerraformStr, info.ClusterResourceName)
`, info.TerraformStr, info.ResourceName)
}

func basicChecks() []resource.TestCheckFunc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestMigBackupRSCloudBackupSchedule_basic(t *testing.T) {
Config: config,
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "0"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "0"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "7"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "3"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "45"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "4"),
Expand All @@ -45,7 +45,7 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.#", "0"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.#", "0"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(dataSourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttrSet(dataSourceName, "reference_hour_of_day"),
resource.TestCheckResourceAttrSet(dataSourceName, "reference_minute_of_hour"),
resource.TestCheckResourceAttrSet(dataSourceName, "restore_window_days"),
Expand All @@ -64,7 +64,7 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
}, true),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "0"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "0"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "7"),
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.0.frequency_interval", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.0.retention_unit", "years"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.0.retention_value", "1"),
resource.TestCheckResourceAttr(dataSourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(dataSourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttrSet(dataSourceName, "reference_hour_of_day"),
resource.TestCheckResourceAttrSet(dataSourceName, "reference_minute_of_hour"),
resource.TestCheckResourceAttrSet(dataSourceName, "restore_window_days"),
Expand All @@ -107,7 +107,7 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "auto_export_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "0"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "0"),
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestAccBackupRSCloudBackupSchedule_export(t *testing.T) {
Config: configExportPolicies(&clusterInfo, policyName, roleName, bucketName),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "auto_export_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "20"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "5"),
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestAccBackupRSCloudBackupSchedule_onePolicy(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "3"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "45"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "4"),
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestAccBackupRSCloudBackupSchedule_onePolicy(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "0"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "0"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "7"),
Expand All @@ -259,9 +259,9 @@ func TestAccBackupRSCloudBackupSchedule_copySettings(t *testing.T) {
},
PitEnabled: true, // you cannot copy oplogs when pit is not enabled
})
clusterName = clusterInfo.ClusterName
terraformStr = clusterInfo.ClusterTerraformStr
clusterResourceName = clusterInfo.ClusterResourceName
clusterName = clusterInfo.Name
terraformStr = clusterInfo.TerraformStr
clusterResourceName = clusterInfo.ResourceName
projectID = clusterInfo.ProjectID
checkMap = map[string]string{
"cluster_name": clusterName,
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestAccBackupRSCloudBackupScheduleImport_basic(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "3"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "45"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "4"),
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestAccBackupRSCloudBackupSchedule_azure(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.frequency_interval", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_unit", "days"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_value", "1")),
Expand All @@ -413,7 +413,7 @@ func TestAccBackupRSCloudBackupSchedule_azure(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.frequency_interval", "2"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_unit", "days"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_value", "3"),
Expand Down Expand Up @@ -473,10 +473,10 @@ func checkDestroy(s *terraform.State) error {
}

func configNoPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) string {
return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q

reference_hour_of_day = %[3]d
reference_minute_of_hour = %[4]d
Expand All @@ -485,16 +485,16 @@ func configNoPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule

data "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q
}
`, info.ClusterNameStr, info.ProjectIDStr, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
`, info.TerraformNameRef, info.ProjectID, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
}

func configDefault(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) string {
return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q

reference_hour_of_day = %[3]d
reference_minute_of_hour = %[4]d
Expand Down Expand Up @@ -529,9 +529,9 @@ func configDefault(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) s

data "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q
}
`, info.ClusterNameStr, info.ProjectIDStr, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
`, info.TerraformNameRef, info.ProjectID, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
}

func configCopySettings(terraformStr, projectID, clusterResourceName string, emptyCopySettings bool, p *admin.DiskBackupSnapshotSchedule) string {
Expand Down Expand Up @@ -592,10 +592,10 @@ func configCopySettings(terraformStr, projectID, clusterResourceName string, emp
}

func configOnePolicy(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) string {
return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q

reference_hour_of_day = %[3]d
reference_minute_of_hour = %[4]d
Expand All @@ -607,7 +607,7 @@ func configOnePolicy(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule)
retention_value = 1
}
}
`, info.ClusterNameStr, info.ProjectIDStr, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
`, info.TerraformNameRef, info.ProjectID, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
}

func configNewPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule, useYearly bool) string {
Expand All @@ -622,10 +622,10 @@ func configNewPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedul
`
}

return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q

reference_hour_of_day = %[3]d
reference_minute_of_hour = %[4]d
Expand Down Expand Up @@ -656,16 +656,16 @@ func configNewPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedul

data "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q
}
`, info.ClusterNameStr, info.ProjectIDStr, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays(), strYearly)
`, info.TerraformNameRef, info.ProjectID, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays(), strYearly)
}

func configAzure(info *acc.ClusterInfo, policy *admin.DiskBackupApiPolicyItem) string {
return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q

policy_item_hourly {
frequency_interval = %[3]d
Expand All @@ -676,16 +676,16 @@ func configAzure(info *acc.ClusterInfo, policy *admin.DiskBackupApiPolicyItem) s

data "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q
}
`, info.ClusterNameStr, info.ProjectIDStr, policy.GetFrequencyInterval(), policy.GetRetentionUnit(), policy.GetRetentionValue())
`, info.TerraformNameRef, info.ProjectID, policy.GetFrequencyInterval(), policy.GetRetentionUnit(), policy.GetRetentionValue())
}

func configAdvancedPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) string {
return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q
Copy link
Member

Choose a reason for hiding this comment

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

just to confirm this change is ok

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this is part of the (last bullet in description):

remove ClusterInfo.ProjectIDStr this field was not safe to use when a reference (e.g., mongodbatlas_project.test.id) was used for the project id.


auto_export_enabled = false
reference_hour_of_day = %[3]d
Expand Down Expand Up @@ -728,14 +728,14 @@ func configAdvancedPolicies(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSc
retention_value = 1
}
}
`, info.ClusterNameStr, info.ProjectIDStr, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
`, info.TerraformNameRef, info.ProjectID, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
}

func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketName string) string {
return info.ClusterTerraformStr + fmt.Sprintf(`
return info.TerraformStr + fmt.Sprintf(`
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
cluster_name = %[1]s
project_id = %[2]s
project_id = %[2]q
auto_export_enabled = true
reference_hour_of_day = 20
reference_minute_of_hour = "05"
Expand Down Expand Up @@ -775,20 +775,20 @@ func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketNam
}

resource "mongodbatlas_cloud_provider_access_setup" "setup_only" {
project_id = %[2]s
project_id = %[2]q
Copy link
Member

@lantoli lantoli Jul 16, 2024

Choose a reason for hiding this comment

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

it's ok to do quotes here and the rest?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See comment above: #2426 (comment)

provider_name = "AWS"
}

resource "mongodbatlas_cloud_provider_access_authorization" "auth_role" {
project_id = %[2]s
project_id = %[2]q
role_id = mongodbatlas_cloud_provider_access_setup.setup_only.role_id
aws {
iam_assumed_role_arn = aws_iam_role.test_role.arn
}
}

resource "mongodbatlas_cloud_backup_snapshot_export_bucket" "test" {
project_id = %[2]s
project_id = %[2]q
iam_role_id = mongodbatlas_cloud_provider_access_authorization.auth_role.role_id
bucket_name = aws_s3_bucket.backup.bucket
cloud_provider = "AWS"
Expand Down Expand Up @@ -837,7 +837,7 @@ func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketNam
}
EOF
}
`, info.ClusterNameStr, info.ProjectIDStr, policyName, roleName, bucketName)
`, info.TerraformNameRef, info.ProjectID, policyName, roleName, bucketName)
}

func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestMigBackupRSCloudBackupSnapshot_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "type", "replicaSet"),
resource.TestCheckResourceAttr(resourceName, "members.#", "0"),
resource.TestCheckResourceAttr(resourceName, "snapshot_ids.#", "0"),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "replica_set_name", clusterInfo.ClusterName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "replica_set_name", clusterInfo.Name),
resource.TestCheckResourceAttr(resourceName, "cloud_provider", "AWS"),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "retention_in_days", retentionInDays),
Expand Down
Loading