@@ -3,22 +3,23 @@ package auditing_test
33import (
44 "context"
55 "fmt"
6- "os "
6+ "strconv "
77 "testing"
88
99 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
1010 "github.com/hashicorp/terraform-plugin-testing/terraform"
1111 "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
1212)
1313
14+ const (
15+ resourceName = "mongodbatlas_auditing.test"
16+ dataSourceName = "data.mongodbatlas_auditing.test"
17+ )
18+
1419func TestAccGenericAuditing_basic (t * testing.T ) {
1520 var (
16- resourceName = "mongodbatlas_auditing.test"
17- orgID = os .Getenv ("MONGODB_ATLAS_ORG_ID" )
18- projectName = acc .RandomProjectName ()
19- auditAuth = true
20- auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}"
21- enabled = true
21+ projectID = acc .ProjectIDExecution (t )
22+ auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}"
2223 )
2324
2425 resource .ParallelTest (t , resource.TestCase {
@@ -27,65 +28,12 @@ func TestAccGenericAuditing_basic(t *testing.T) {
2728 CheckDestroy : checkDestroy ,
2829 Steps : []resource.TestStep {
2930 {
30- Config : configBasic (orgID , projectName , auditFilter , auditAuth , enabled ),
31- Check : resource .ComposeTestCheckFunc (
32- checkExists (resourceName ),
33- resource .TestCheckResourceAttrSet (resourceName , "project_id" ),
34- resource .TestCheckResourceAttrSet (resourceName , "audit_filter" ),
35- resource .TestCheckResourceAttrSet (resourceName , "audit_authorization_success" ),
36- resource .TestCheckResourceAttrSet (resourceName , "enabled" ),
37- resource .TestCheckResourceAttr (resourceName , "audit_filter" , auditFilter ),
38- resource .TestCheckResourceAttr (resourceName , "audit_authorization_success" , "true" ),
39- resource .TestCheckResourceAttr (resourceName , "enabled" , "true" ),
40- resource .TestCheckResourceAttr (resourceName , "configuration_type" , "FILTER_JSON" ),
41- ),
31+ Config : configBasic (projectID , auditFilter , true , true ),
32+ Check : resource .ComposeTestCheckFunc (checks (auditFilter , true , true )... ),
4233 },
4334 {
44- Config : configBasic (orgID , projectName , "{}" , false , false ),
45- Check : resource .ComposeTestCheckFunc (
46- checkExists (resourceName ),
47- resource .TestCheckResourceAttrSet (resourceName , "project_id" ),
48- resource .TestCheckResourceAttrSet (resourceName , "audit_filter" ),
49- resource .TestCheckResourceAttrSet (resourceName , "audit_authorization_success" ),
50- resource .TestCheckResourceAttrSet (resourceName , "enabled" ),
51- resource .TestCheckResourceAttr (resourceName , "audit_filter" , "{}" ),
52- resource .TestCheckResourceAttr (resourceName , "audit_authorization_success" , "false" ),
53- resource .TestCheckResourceAttr (resourceName , "enabled" , "false" ),
54- resource .TestCheckResourceAttr (resourceName , "configuration_type" , "FILTER_JSON" ),
55- ),
56- },
57- },
58- })
59- }
60-
61- func TestAccGenericAuditing_importBasic (t * testing.T ) {
62- var (
63- resourceName = "mongodbatlas_auditing.test"
64- orgID = os .Getenv ("MONGODB_ATLAS_ORG_ID" )
65- projectName = acc .RandomProjectName ()
66- auditAuth = true
67- auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}"
68- enabled = true
69- )
70-
71- resource .ParallelTest (t , resource.TestCase {
72- PreCheck : func () { acc .PreCheckBasic (t ) },
73- ProtoV6ProviderFactories : acc .TestAccProviderV6Factories ,
74- CheckDestroy : checkDestroy ,
75- Steps : []resource.TestStep {
76- {
77- Config : configBasic (orgID , projectName , auditFilter , auditAuth , enabled ),
78- Check : resource .ComposeTestCheckFunc (
79- checkExists (resourceName ),
80- resource .TestCheckResourceAttrSet (resourceName , "project_id" ),
81- resource .TestCheckResourceAttrSet (resourceName , "audit_filter" ),
82- resource .TestCheckResourceAttrSet (resourceName , "audit_authorization_success" ),
83- resource .TestCheckResourceAttrSet (resourceName , "enabled" ),
84- resource .TestCheckResourceAttr (resourceName , "audit_filter" , auditFilter ),
85- resource .TestCheckResourceAttr (resourceName , "audit_authorization_success" , "true" ),
86- resource .TestCheckResourceAttr (resourceName , "enabled" , "true" ),
87- resource .TestCheckResourceAttr (resourceName , "configuration_type" , "FILTER_JSON" ),
88- ),
35+ Config : configBasic (projectID , "{}" , false , false ),
36+ Check : resource .ComposeTestCheckFunc (checks ("{}" , false , false )... ),
8937 },
9038 {
9139 ResourceName : resourceName ,
@@ -121,7 +69,7 @@ func checkDestroy(s *terraform.State) error {
12169 continue
12270 }
12371 auditingRes , _ , _ := acc .ConnV2 ().AuditingApi .GetAuditingConfiguration (context .Background (), rs .Primary .ID ).Execute ()
124- if auditingRes != nil {
72+ if auditingRes . GetEnabled () {
12573 return fmt .Errorf ("auditing (%s) exists" , rs .Primary .ID )
12674 }
12775 }
@@ -139,21 +87,30 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
13987 }
14088}
14189
142- func configBasic (orgID , projectName , auditFilter string , auditAuth , enabled bool ) string {
90+ func configBasic (projectID , auditFilter string , auditAuth , enabled bool ) string {
14391 return fmt .Sprintf (`
144- resource "mongodbatlas_project" "test" {
145- name = %[2]q
146- org_id = %[1]q
147- }
14892 resource "mongodbatlas_auditing" "test" {
149- project_id = mongodbatlas_project.test.id
150- audit_filter = %[3 ]q
151- audit_authorization_success = %[4 ]t
152- enabled = %[5 ]t
93+ project_id = %[1]q
94+ audit_filter = %[2 ]q
95+ audit_authorization_success = %[3 ]t
96+ enabled = %[4 ]t
15397 }
15498
15599 data "mongodbatlas_auditing" "test" {
156100 project_id = mongodbatlas_auditing.test.id
157101 }
158- ` , orgID , projectName , auditFilter , auditAuth , enabled )
102+ ` , projectID , auditFilter , auditAuth , enabled )
103+ }
104+
105+ func checks (auditFilter string , auditAuth , enabled bool ) []resource.TestCheckFunc {
106+ commonChecks := map [string ]string {
107+ "audit_filter" : auditFilter ,
108+ "audit_authorization_success" : strconv .FormatBool (auditAuth ),
109+ "enabled" : strconv .FormatBool (auditAuth ),
110+ "configuration_type" : "FILTER_JSON" ,
111+ }
112+ checks := acc .AddAttrChecks (resourceName , nil , commonChecks )
113+ checks = acc .AddAttrChecks (dataSourceName , checks , commonChecks )
114+ checks = append (checks , checkExists (resourceName ), checkExists (dataSourceName ))
115+ return checks
159116}
0 commit comments