From 981882c341c7b723e7db953e4fb9c2e3a79992d0 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:18:07 +0100 Subject: [PATCH 1/5] move data source checks to resource --- .../auditing/data_source_auditing_test.go | 38 ------------------- .../auditing/resource_auditing_test.go | 10 +++++ 2 files changed, 10 insertions(+), 38 deletions(-) delete mode 100644 internal/service/auditing/data_source_auditing_test.go diff --git a/internal/service/auditing/data_source_auditing_test.go b/internal/service/auditing/data_source_auditing_test.go deleted file mode 100644 index 8ab54047ca..0000000000 --- a/internal/service/auditing/data_source_auditing_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package auditing_test - -import ( - "os" - "testing" - - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" -) - -func TestAccGenericAuditingDS_basic(t *testing.T) { - var ( - dataSourceName = "data.mongodbatlas_auditing.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() - auditAuth = true - auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true - ) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Steps: []resource.TestStep{ - { - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), - Check: resource.ComposeTestCheckFunc( - checkExists("mongodbatlas_auditing.test"), - resource.TestCheckResourceAttrSet(dataSourceName, "project_id"), - resource.TestCheckResourceAttr(dataSourceName, "audit_filter", auditFilter), - resource.TestCheckResourceAttr(dataSourceName, "audit_authorization_success", "true"), - resource.TestCheckResourceAttr(dataSourceName, "enabled", "true"), - resource.TestCheckResourceAttr(dataSourceName, "configuration_type", "FILTER_JSON"), - ), - }, - }, - }) -} diff --git a/internal/service/auditing/resource_auditing_test.go b/internal/service/auditing/resource_auditing_test.go index 00c340d4c1..69955167d5 100644 --- a/internal/service/auditing/resource_auditing_test.go +++ b/internal/service/auditing/resource_auditing_test.go @@ -11,6 +11,10 @@ import ( "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" ) +const ( + dataSourceName = "data.mongodbatlas_auditing.test" +) + func TestAccGenericAuditing_basic(t *testing.T) { var ( resourceName = "mongodbatlas_auditing.test" @@ -38,6 +42,12 @@ func TestAccGenericAuditing_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), + + resource.TestCheckResourceAttrSet(dataSourceName, "project_id"), + resource.TestCheckResourceAttr(dataSourceName, "audit_filter", auditFilter), + resource.TestCheckResourceAttr(dataSourceName, "audit_authorization_success", "true"), + resource.TestCheckResourceAttr(dataSourceName, "enabled", "true"), + resource.TestCheckResourceAttr(dataSourceName, "configuration_type", "FILTER_JSON"), ), }, { From 6c33853d5280da29dcb13bdc07f56393bd3e3646 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:19:30 +0100 Subject: [PATCH 2/5] join import test --- .../auditing/resource_auditing_test.go | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/internal/service/auditing/resource_auditing_test.go b/internal/service/auditing/resource_auditing_test.go index 69955167d5..b0e4eb83c7 100644 --- a/internal/service/auditing/resource_auditing_test.go +++ b/internal/service/auditing/resource_auditing_test.go @@ -64,39 +64,6 @@ func TestAccGenericAuditing_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), ), }, - }, - }) -} - -func TestAccGenericAuditing_importBasic(t *testing.T) { - var ( - resourceName = "mongodbatlas_auditing.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() - auditAuth = true - auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true - ) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - CheckDestroy: checkDestroy, - Steps: []resource.TestStep{ - { - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), - Check: resource.ComposeTestCheckFunc( - checkExists(resourceName), - resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "audit_filter"), - resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"), - resource.TestCheckResourceAttrSet(resourceName, "enabled"), - resource.TestCheckResourceAttr(resourceName, "audit_filter", auditFilter), - resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"), - resource.TestCheckResourceAttr(resourceName, "enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), - ), - }, { ResourceName: resourceName, ImportStateIdFunc: importStateIDFunc(resourceName), From 448f5be7f4d8dbeddbc79af837967c70a6dec9e1 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:28:03 +0100 Subject: [PATCH 3/5] mig tests --- .../resource_auditing_migration_test.go | 20 +++-------- .../auditing/resource_auditing_test.go | 35 +++++++++---------- .../resource_cloud_backup_schedule_test.go | 2 +- .../resource_cloud_backup_snapshot_test.go | 2 +- internal/testutil/acc/cluster.go | 5 +-- internal/testutil/acc/search_index.go | 2 +- 6 files changed, 26 insertions(+), 40 deletions(-) diff --git a/internal/service/auditing/resource_auditing_migration_test.go b/internal/service/auditing/resource_auditing_migration_test.go index 48a6a26343..4817aff031 100644 --- a/internal/service/auditing/resource_auditing_migration_test.go +++ b/internal/service/auditing/resource_auditing_migration_test.go @@ -1,23 +1,20 @@ package auditing_test import ( - "os" "testing" "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 TestMigGenericAuditing_basic(t *testing.T) { var ( resourceName = "mongodbatlas_auditing.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() + projectID = mig.ProjectIDGlobal(t) auditAuth = true auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" enabled = true + config = configBasic(projectID, auditFilter, auditAuth, enabled) ) resource.ParallelTest(t, resource.TestCase{ @@ -25,7 +22,7 @@ func TestMigGenericAuditing_basic(t *testing.T) { CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), + Config: config, ExternalProviders: mig.ExternalProviders(), Check: resource.ComposeTestCheckFunc( checkExists(resourceName), @@ -39,16 +36,7 @@ func TestMigGenericAuditing_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), ), }, - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), - ConfigPlanChecks: resource.ConfigPlanChecks{ - PreApply: []plancheck.PlanCheck{ - acc.DebugPlan(), - plancheck.ExpectEmptyPlan(), - }, - }, - }, + mig.TestStepCheckEmptyPlan(config), }, }) } diff --git a/internal/service/auditing/resource_auditing_test.go b/internal/service/auditing/resource_auditing_test.go index b0e4eb83c7..43e4903a3f 100644 --- a/internal/service/auditing/resource_auditing_test.go +++ b/internal/service/auditing/resource_auditing_test.go @@ -3,7 +3,6 @@ package auditing_test import ( "context" "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -12,17 +11,16 @@ import ( ) const ( + resourceName = "mongodbatlas_auditing.test" dataSourceName = "data.mongodbatlas_auditing.test" ) func TestAccGenericAuditing_basic(t *testing.T) { var ( - resourceName = "mongodbatlas_auditing.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() - auditAuth = true - auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true + projectID = acc.ProjectIDExecution(t) + auditAuth = true + auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" + enabled = true ) resource.ParallelTest(t, resource.TestCase{ @@ -31,7 +29,7 @@ func TestAccGenericAuditing_basic(t *testing.T) { CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), + Config: configBasic(projectID, auditFilter, auditAuth, enabled), Check: resource.ComposeTestCheckFunc( checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -51,7 +49,7 @@ func TestAccGenericAuditing_basic(t *testing.T) { ), }, { - Config: configBasic(orgID, projectName, "{}", false, false), + Config: configBasic(projectID, "{}", false, false), Check: resource.ComposeTestCheckFunc( checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -93,6 +91,9 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { + if acc.UsingLocalResources() { + return nil + } for _, rs := range s.RootModule().Resources { if rs.Type != "mongodbatlas_auditing" { continue @@ -116,21 +117,17 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { } } -func configBasic(orgID, projectName, auditFilter string, auditAuth, enabled bool) string { +func configBasic(projectID, auditFilter string, auditAuth, enabled bool) string { return fmt.Sprintf(` - resource "mongodbatlas_project" "test" { - name = %[2]q - org_id = %[1]q - } resource "mongodbatlas_auditing" "test" { - project_id = mongodbatlas_project.test.id - audit_filter = %[3]q - audit_authorization_success = %[4]t - enabled = %[5]t + project_id = %[1]q + audit_filter = %[2]q + audit_authorization_success = %[3]t + enabled = %[4]t } data "mongodbatlas_auditing" "test" { project_id = mongodbatlas_auditing.test.id } - `, orgID, projectName, auditFilter, auditAuth, enabled) + `, projectID, auditFilter, auditAuth, enabled) } diff --git a/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go b/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go index 085489a984..283cfb9b89 100644 --- a/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go +++ b/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go @@ -401,7 +401,7 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.ExistingClusterUsed() { + if acc.UsingLocalResources() { return nil } for _, rs := range s.RootModule().Resources { diff --git a/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go b/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go index a4282abda3..59efc234f2 100644 --- a/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go +++ b/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go @@ -121,7 +121,7 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.ExistingClusterUsed() { + if acc.UsingLocalResources() { return nil } for _, rs := range s.RootModule().Resources { diff --git a/internal/testutil/acc/cluster.go b/internal/testutil/acc/cluster.go index 7541605863..b0fa3079a5 100644 --- a/internal/testutil/acc/cluster.go +++ b/internal/testutil/acc/cluster.go @@ -80,8 +80,9 @@ func GetClusterInfo(tb testing.TB, req *ClusterRequest) ClusterInfo { } } -func ExistingClusterUsed() bool { +// UsingLocalResources is normally used in CheckDestroy when an existing project or cluster is used in local. +func UsingLocalResources() bool { clusterName := os.Getenv("MONGODB_ATLAS_CLUSTER_NAME") projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID") - return clusterName != "" && projectID != "" + return clusterName != "" || projectID != "" } diff --git a/internal/testutil/acc/search_index.go b/internal/testutil/acc/search_index.go index 04023b5928..108cbd144f 100644 --- a/internal/testutil/acc/search_index.go +++ b/internal/testutil/acc/search_index.go @@ -9,7 +9,7 @@ import ( ) func CheckDestroySearchIndex(state *terraform.State) error { - if ExistingClusterUsed() { + if UsingLocalResources() { return nil } for _, rs := range state.RootModule().Resources { From 4de6c617a212824649975d2058d2fe1a3b4d6ca6 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:58:49 +0100 Subject: [PATCH 4/5] checkDestroy --- .../auditing/resource_auditing_migration_test.go | 11 +++++------ internal/service/auditing/resource_auditing_test.go | 5 +---- .../resource_cloud_backup_schedule_test.go | 2 +- .../resource_cloud_backup_snapshot_test.go | 2 +- internal/testutil/acc/cluster.go | 5 ++--- internal/testutil/acc/search_index.go | 2 +- 6 files changed, 11 insertions(+), 16 deletions(-) diff --git a/internal/service/auditing/resource_auditing_migration_test.go b/internal/service/auditing/resource_auditing_migration_test.go index 4817aff031..0fb7fdcc47 100644 --- a/internal/service/auditing/resource_auditing_migration_test.go +++ b/internal/service/auditing/resource_auditing_migration_test.go @@ -9,12 +9,11 @@ import ( func TestMigGenericAuditing_basic(t *testing.T) { var ( - resourceName = "mongodbatlas_auditing.test" - projectID = mig.ProjectIDGlobal(t) - auditAuth = true - auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true - config = configBasic(projectID, auditFilter, auditAuth, enabled) + projectID = mig.ProjectIDGlobal(t) + auditAuth = true + auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" + enabled = true + config = configBasic(projectID, auditFilter, auditAuth, enabled) ) resource.ParallelTest(t, resource.TestCase{ diff --git a/internal/service/auditing/resource_auditing_test.go b/internal/service/auditing/resource_auditing_test.go index 43e4903a3f..a5b5fa27ad 100644 --- a/internal/service/auditing/resource_auditing_test.go +++ b/internal/service/auditing/resource_auditing_test.go @@ -91,15 +91,12 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.UsingLocalResources() { - return nil - } for _, rs := range s.RootModule().Resources { if rs.Type != "mongodbatlas_auditing" { continue } auditingRes, _, _ := acc.ConnV2().AuditingApi.GetAuditingConfiguration(context.Background(), rs.Primary.ID).Execute() - if auditingRes != nil { + if auditingRes.GetEnabled() { return fmt.Errorf("auditing (%s) exists", rs.Primary.ID) } } diff --git a/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go b/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go index 283cfb9b89..085489a984 100644 --- a/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go +++ b/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go @@ -401,7 +401,7 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.UsingLocalResources() { + if acc.ExistingClusterUsed() { return nil } for _, rs := range s.RootModule().Resources { diff --git a/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go b/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go index 59efc234f2..a4282abda3 100644 --- a/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go +++ b/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go @@ -121,7 +121,7 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.UsingLocalResources() { + if acc.ExistingClusterUsed() { return nil } for _, rs := range s.RootModule().Resources { diff --git a/internal/testutil/acc/cluster.go b/internal/testutil/acc/cluster.go index b0fa3079a5..7541605863 100644 --- a/internal/testutil/acc/cluster.go +++ b/internal/testutil/acc/cluster.go @@ -80,9 +80,8 @@ func GetClusterInfo(tb testing.TB, req *ClusterRequest) ClusterInfo { } } -// UsingLocalResources is normally used in CheckDestroy when an existing project or cluster is used in local. -func UsingLocalResources() bool { +func ExistingClusterUsed() bool { clusterName := os.Getenv("MONGODB_ATLAS_CLUSTER_NAME") projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID") - return clusterName != "" || projectID != "" + return clusterName != "" && projectID != "" } diff --git a/internal/testutil/acc/search_index.go b/internal/testutil/acc/search_index.go index 108cbd144f..04023b5928 100644 --- a/internal/testutil/acc/search_index.go +++ b/internal/testutil/acc/search_index.go @@ -9,7 +9,7 @@ import ( ) func CheckDestroySearchIndex(state *terraform.State) error { - if UsingLocalResources() { + if ExistingClusterUsed() { return nil } for _, rs := range state.RootModule().Resources { From a9fba9fa8815dc5760b2f8648535057860e01615 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:53:35 +0100 Subject: [PATCH 5/5] refactor checks --- .../resource_auditing_migration_test.go | 16 +------ .../auditing/resource_auditing_test.go | 48 +++++++------------ 2 files changed, 19 insertions(+), 45 deletions(-) diff --git a/internal/service/auditing/resource_auditing_migration_test.go b/internal/service/auditing/resource_auditing_migration_test.go index 0fb7fdcc47..990d8ba6a3 100644 --- a/internal/service/auditing/resource_auditing_migration_test.go +++ b/internal/service/auditing/resource_auditing_migration_test.go @@ -10,10 +10,8 @@ import ( func TestMigGenericAuditing_basic(t *testing.T) { var ( projectID = mig.ProjectIDGlobal(t) - auditAuth = true auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true - config = configBasic(projectID, auditFilter, auditAuth, enabled) + config = configBasic(projectID, auditFilter, true, true) ) resource.ParallelTest(t, resource.TestCase{ @@ -23,17 +21,7 @@ func TestMigGenericAuditing_basic(t *testing.T) { { Config: config, ExternalProviders: mig.ExternalProviders(), - Check: resource.ComposeTestCheckFunc( - checkExists(resourceName), - resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "audit_filter"), - resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"), - resource.TestCheckResourceAttrSet(resourceName, "enabled"), - resource.TestCheckResourceAttr(resourceName, "audit_filter", auditFilter), - resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"), - resource.TestCheckResourceAttr(resourceName, "enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), - ), + Check: resource.ComposeTestCheckFunc(checks(auditFilter, true, true)...), }, mig.TestStepCheckEmptyPlan(config), }, diff --git a/internal/service/auditing/resource_auditing_test.go b/internal/service/auditing/resource_auditing_test.go index a5b5fa27ad..7e9e9eae16 100644 --- a/internal/service/auditing/resource_auditing_test.go +++ b/internal/service/auditing/resource_auditing_test.go @@ -3,6 +3,7 @@ package auditing_test import ( "context" "fmt" + "strconv" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -18,9 +19,7 @@ const ( func TestAccGenericAuditing_basic(t *testing.T) { var ( projectID = acc.ProjectIDExecution(t) - auditAuth = true auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true ) resource.ParallelTest(t, resource.TestCase{ @@ -29,38 +28,12 @@ func TestAccGenericAuditing_basic(t *testing.T) { CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: configBasic(projectID, auditFilter, auditAuth, enabled), - Check: resource.ComposeTestCheckFunc( - checkExists(resourceName), - resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "audit_filter"), - resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"), - resource.TestCheckResourceAttrSet(resourceName, "enabled"), - resource.TestCheckResourceAttr(resourceName, "audit_filter", auditFilter), - resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"), - resource.TestCheckResourceAttr(resourceName, "enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), - - resource.TestCheckResourceAttrSet(dataSourceName, "project_id"), - resource.TestCheckResourceAttr(dataSourceName, "audit_filter", auditFilter), - resource.TestCheckResourceAttr(dataSourceName, "audit_authorization_success", "true"), - resource.TestCheckResourceAttr(dataSourceName, "enabled", "true"), - resource.TestCheckResourceAttr(dataSourceName, "configuration_type", "FILTER_JSON"), - ), + Config: configBasic(projectID, auditFilter, true, true), + Check: resource.ComposeTestCheckFunc(checks(auditFilter, true, true)...), }, { Config: configBasic(projectID, "{}", false, false), - Check: resource.ComposeTestCheckFunc( - checkExists(resourceName), - resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "audit_filter"), - resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"), - resource.TestCheckResourceAttrSet(resourceName, "enabled"), - resource.TestCheckResourceAttr(resourceName, "audit_filter", "{}"), - resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "false"), - resource.TestCheckResourceAttr(resourceName, "enabled", "false"), - resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), - ), + Check: resource.ComposeTestCheckFunc(checks("{}", false, false)...), }, { ResourceName: resourceName, @@ -128,3 +101,16 @@ func configBasic(projectID, auditFilter string, auditAuth, enabled bool) string } `, projectID, auditFilter, auditAuth, enabled) } + +func checks(auditFilter string, auditAuth, enabled bool) []resource.TestCheckFunc { + commonChecks := map[string]string{ + "audit_filter": auditFilter, + "audit_authorization_success": strconv.FormatBool(auditAuth), + "enabled": strconv.FormatBool(auditAuth), + "configuration_type": "FILTER_JSON", + } + checks := acc.AddAttrChecks(resourceName, nil, commonChecks) + checks = acc.AddAttrChecks(dataSourceName, checks, commonChecks) + checks = append(checks, checkExists(resourceName), checkExists(dataSourceName)) + return checks +}