@@ -15,48 +15,42 @@ import (
1515func TestAccCSMThreatsPoliciesList_CreateAndUpdate (t * testing.T ) {
1616 _ , providers , accProviders := testAccFrameworkMuxProviders (context .Background (), t )
1717
18- resourceName := "datadog_csm_threats_policies_list.all "
18+ resourceName := "datadog_csm_threats_policies.all_policies "
1919
2020 resource .Test (t , resource.TestCase {
2121 PreCheck : func () { testAccPreCheck (t ) },
2222 ProtoV5ProviderFactories : accProviders ,
23- CheckDestroy : testAccCheckCSMThreatsPoliciesListDestroy (providers .frameworkProvider ),
23+ CheckDestroy : testAccCheckCSMThreatsPoliciesDestroy (providers .frameworkProvider ),
2424 Steps : []resource.TestStep {
2525 {
26- Config : testAccCSMThreatsPoliciesListConfigBasic (),
26+ Config : testAccCSMThreatsPoliciesConfig (),
2727 Check : resource .ComposeTestCheckFunc (
28- testAccCheckCSMThreatsPoliciesListExists (providers .frameworkProvider , resourceName ),
29- resource .TestCheckResourceAttr (resourceName , "entries.#" , "2" ),
30- resource .TestCheckResourceAttr (resourceName , "entries.0.name" , "TERRAFORM_POLICY1" ),
31- resource .TestCheckResourceAttr (resourceName , "entries.0.priority" , "2" ),
32- resource .TestCheckResourceAttr (resourceName , "entries.1.name" , "TERRAFORM_POLICY2" ),
33- resource .TestCheckResourceAttr (resourceName , "entries.1.priority" , "3" ),
28+ testAccCheckCSMThreatsPoliciesExists (providers .frameworkProvider , resourceName ),
29+ resource .TestCheckResourceAttr (resourceName , "policies.0.name" , "terraform_policy" ),
30+ resource .TestCheckResourceAttr (resourceName , "policies.0.enabled" , "false" ),
3431 ),
3532 },
3633 {
37- Config : testAccCSMThreatsPoliciesListConfigUpdate (),
34+ Config : testAccCSMThreatsPoliciesConfigUpdate (),
3835 Check : resource .ComposeTestCheckFunc (
39- testAccCheckCSMThreatsPoliciesListExists (providers .frameworkProvider , resourceName ),
40- resource .TestCheckResourceAttr (resourceName , "entries.#" , "2" ),
41- resource .TestCheckResourceAttr (resourceName , "entries.0.name" , "TERRAFORM_POLICY1" ),
42- resource .TestCheckResourceAttr (resourceName , "entries.0.priority" , "2" ),
43- resource .TestCheckResourceAttr (resourceName , "entries.1.name" , "TERRAFORM_POLICY2 UPDATED" ),
44- resource .TestCheckResourceAttr (resourceName , "entries.1.priority" , "5" ),
36+ testAccCheckCSMThreatsPoliciesExists (providers .frameworkProvider , resourceName ),
37+ resource .TestCheckResourceAttr (resourceName , "policies.0.name" , "terraform_policy updated" ),
38+ resource .TestCheckResourceAttr (resourceName , "policies.0.enabled" , "true" ),
4539 ),
4640 },
4741 },
4842 })
4943}
5044
51- func testAccCheckCSMThreatsPoliciesListExists (accProvider * fwprovider.FrameworkProvider , resourceName string ) resource.TestCheckFunc {
45+ func testAccCheckCSMThreatsPoliciesExists (accProvider * fwprovider.FrameworkProvider , resourceName string ) resource.TestCheckFunc {
5246 return func (s * terraform.State ) error {
5347 rs , ok := s .RootModule ().Resources [resourceName ]
5448 if ! ok {
5549 return fmt .Errorf ("resource '%s' not found in state" , resourceName )
5650 }
57- if rs .Type != "datadog_csm_threats_policies_list " {
51+ if rs .Type != "datadog_csm_threats_policies " {
5852 return fmt .Errorf (
59- "resource %s is not a datadog_csm_threats_policies_list , got: %s" ,
53+ "resource %s is not a datadog_csm_threats_policies , got: %s" ,
6054 resourceName ,
6155 rs .Type ,
6256 )
@@ -70,85 +64,44 @@ func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkP
7064 }
7165}
7266
73- func testAccCheckCSMThreatsPoliciesListDestroy (accProvider * fwprovider.FrameworkProvider ) resource.TestCheckFunc {
67+ func testAccCheckCSMThreatsPoliciesDestroy (accProvider * fwprovider.FrameworkProvider ) resource.TestCheckFunc {
7468 return func (s * terraform.State ) error {
75- apiInstances := accProvider .DatadogApiInstances
76- auth := accProvider .Auth
77-
7869 for _ , r := range s .RootModule ().Resources {
79- if r .Type != "datadog_csm_threats_policies_list " {
70+ if r .Type != "datadog_csm_threats_policies " {
8071 continue
8172 }
8273
83- resp , httpResponse , err := apiInstances .GetCSMThreatsApiV2 ().ListCSMThreatsAgentPolicies (auth )
84- if err != nil {
85- if httpResponse != nil && httpResponse .StatusCode == 404 {
86- return nil
87- }
88- return fmt .Errorf ("Received an error while listing the policies: %s" , err )
89- }
90-
91- if len (resp .GetData ()) > 1 { // CWS_DD is always present
92- return fmt .Errorf ("Policies list not empty, some policies are still present" )
74+ if _ , ok := s .RootModule ().Resources [r .Primary .ID ]; ok {
75+ return fmt .Errorf ("Resource %s still exists in state" , r .Primary .ID )
9376 }
9477 }
9578 return nil
9679 }
9780}
9881
99- func testAccCSMThreatsPoliciesListConfigBasic () string {
82+ func testAccCSMThreatsPoliciesConfig () string {
10083 return `
101- resource "datadog_csm_threats_policy" "policy1" {
102- description = "created with terraform"
103- enabled = false
104- tags = []
105- }
106-
107- resource "datadog_csm_threats_policy" "policy2" {
108- description = "created with terraform 2"
109- enabled = true
110- tags = ["env:staging"]
111- }
112-
113- resource "datadog_csm_threats_policies_list" "all" {
114- entries {
115- policy_id = datadog_csm_threats_policy.policy1.id
116- name = "TERRAFORM_POLICY1"
117- priority = 2
118- }
119- entries {
120- policy_id = datadog_csm_threats_policy.policy2.id
121- name = "TERRAFORM_POLICY2"
122- priority = 3
84+ resource "datadog_csm_threats_policies" "all_policies" {
85+ policies {
86+ policy_label = "policy1"
87+ name = "terraform_policy"
88+ description = "description"
89+ enabled = false
90+ tags = ["env:staging"]
12391 }
12492 }
12593 `
12694}
12795
128- func testAccCSMThreatsPoliciesListConfigUpdate () string {
96+ func testAccCSMThreatsPoliciesConfigUpdate () string {
12997 return `
130- resource "datadog_csm_threats_policy" "policy1" {
131- description = "created with terraform"
132- enabled = false
133- tags = []
134- }
135-
136- resource "datadog_csm_threats_policy" "policy2" {
137- description = "created with terraform 2"
138- enabled = true
139- tags = ["env:staging"]
140- }
141-
142- resource "datadog_csm_threats_policies_list" "all" {
143- entries {
144- policy_id = datadog_csm_threats_policy.policy1.id
145- name = "TERRAFORM_POLICY1"
146- priority = 2
147- }
148- entries {
149- policy_id = datadog_csm_threats_policy.policy2.id
150- name = "TERRAFORM_POLICY2 UPDATED"
151- priority = 5
98+ resource "datadog_csm_threats_policies" "all_policies" {
99+ policies {
100+ policy_label = "policy"
101+ name = "terraform_policy updated"
102+ description = "new description"
103+ enabled = true
104+ tags = ["foo:bar"]
152105 }
153106 }
154107 `
0 commit comments