@@ -12,51 +12,49 @@ import (
12
12
)
13
13
14
14
// Create a policies_list and update the name and priority of its policy
15
- func TestAccCSMThreatsPoliciesList_CreateAndUpdate (t * testing.T ) {
15
+ func TestAccCSMThreatsPolicies_CreateAndUpdate (t * testing.T ) {
16
16
_ , providers , accProviders := testAccFrameworkMuxProviders (context .Background (), t )
17
17
18
- resourceName := "datadog_csm_threats_policies_list.all "
18
+ resourceName := "datadog_csm_threats_policies.all_policies "
19
19
20
20
resource .Test (t , resource.TestCase {
21
21
PreCheck : func () { testAccPreCheck (t ) },
22
22
ProtoV5ProviderFactories : accProviders ,
23
- CheckDestroy : testAccCheckCSMThreatsPoliciesListDestroy (providers .frameworkProvider ),
23
+ CheckDestroy : testAccCheckCSMThreatsPoliciesDestroy (providers .frameworkProvider ),
24
24
Steps : []resource.TestStep {
25
25
{
26
- Config : testAccCSMThreatsPoliciesListConfigBasic (),
26
+ Config : testAccCSMThreatsPoliciesConfig (),
27
27
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.description" , "description" ),
31
+ resource .TestCheckResourceAttr (resourceName , "policies.0.enabled" , "false" ),
32
+ resource .TestCheckResourceAttr (resourceName , "policies.0.tags.0" , "env:staging" ),
34
33
),
35
34
},
36
35
{
37
- Config : testAccCSMThreatsPoliciesListConfigUpdate (),
36
+ Config : testAccCSMThreatsPoliciesConfigUpdate (),
38
37
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" ),
38
+ testAccCheckCSMThreatsPoliciesExists (providers .frameworkProvider , resourceName ),
39
+ resource .TestCheckResourceAttr (resourceName , "policies.0.name" , "terraform_policy updated" ),
40
+ resource .TestCheckResourceAttr (resourceName , "policies.0.description" , "new description" ),
41
+ resource .TestCheckResourceAttr (resourceName , "policies.0.enabled" , "true" ),
42
+ resource .TestCheckResourceAttr (resourceName , "policies.0.tags.0" , "foo:bar" ),
45
43
),
46
44
},
47
45
},
48
46
})
49
47
}
50
48
51
- func testAccCheckCSMThreatsPoliciesListExists (accProvider * fwprovider.FrameworkProvider , resourceName string ) resource.TestCheckFunc {
49
+ func testAccCheckCSMThreatsPoliciesExists (accProvider * fwprovider.FrameworkProvider , resourceName string ) resource.TestCheckFunc {
52
50
return func (s * terraform.State ) error {
53
51
rs , ok := s .RootModule ().Resources [resourceName ]
54
52
if ! ok {
55
53
return fmt .Errorf ("resource '%s' not found in state" , resourceName )
56
54
}
57
- if rs .Type != "datadog_csm_threats_policies_list " {
55
+ if rs .Type != "datadog_csm_threats_policies " {
58
56
return fmt .Errorf (
59
- "resource %s is not a datadog_csm_threats_policies_list , got: %s" ,
57
+ "resource %s is not a datadog_csm_threats_policies , got: %s" ,
60
58
resourceName ,
61
59
rs .Type ,
62
60
)
@@ -70,85 +68,44 @@ func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkP
70
68
}
71
69
}
72
70
73
- func testAccCheckCSMThreatsPoliciesListDestroy (accProvider * fwprovider.FrameworkProvider ) resource.TestCheckFunc {
71
+ func testAccCheckCSMThreatsPoliciesDestroy (accProvider * fwprovider.FrameworkProvider ) resource.TestCheckFunc {
74
72
return func (s * terraform.State ) error {
75
- apiInstances := accProvider .DatadogApiInstances
76
- auth := accProvider .Auth
77
-
78
73
for _ , r := range s .RootModule ().Resources {
79
- if r .Type != "datadog_csm_threats_policies_list " {
74
+ if r .Type != "datadog_csm_threats_policies " {
80
75
continue
81
76
}
82
77
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" )
78
+ if _ , ok := s .RootModule ().Resources [r .Primary .ID ]; ok {
79
+ return fmt .Errorf ("Resource %s still exists in state" , r .Primary .ID )
93
80
}
94
81
}
95
82
return nil
96
83
}
97
84
}
98
85
99
- func testAccCSMThreatsPoliciesListConfigBasic () string {
86
+ func testAccCSMThreatsPoliciesConfig () string {
100
87
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
88
+ resource "datadog_csm_threats_policies" "all_policies" {
89
+ policies {
90
+ policy_label = "policy"
91
+ name = "terraform_policy"
92
+ description = "description"
93
+ enabled = false
94
+ tags = ["env:staging"]
123
95
}
124
96
}
125
97
`
126
98
}
127
99
128
- func testAccCSMThreatsPoliciesListConfigUpdate () string {
100
+ func testAccCSMThreatsPoliciesConfigUpdate () string {
129
101
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
102
+ resource "datadog_csm_threats_policies" "all_policies" {
103
+ policies {
104
+ policy_label = "policy"
105
+ name = "terraform_policy updated"
106
+ description = "new description"
107
+ enabled = true
108
+ tags = ["foo:bar"]
152
109
}
153
110
}
154
111
`
0 commit comments