@@ -18,6 +18,7 @@ import (
1818)
1919
2020var minVersionAgentPolicy = version .Must (version .NewVersion ("8.6.0" ))
21+ var minVersionGlobalDataTags = version .Must (version .NewVersion ("8.15.0" ))
2122
2223func TestAccResourceAgentPolicyFromSDK (t * testing.T ) {
2324 policyName := sdkacctest .RandStringFromCharSet (22 , sdkacctest .CharSetAlphaNum )
@@ -63,6 +64,8 @@ func TestAccResourceAgentPolicyFromSDK(t *testing.T) {
6364
6465func TestAccResourceAgentPolicy (t * testing.T ) {
6566 policyName := sdkacctest .RandStringFromCharSet (22 , sdkacctest .CharSetAlphaNum )
67+ policyNameGlobalDataTags := sdkacctest .RandStringFromCharSet (22 , sdkacctest .CharSetAlphaNum )
68+
6669 var originalPolicyId string
6770
6871 resource .Test (t , resource.TestCase {
@@ -118,6 +121,51 @@ func TestAccResourceAgentPolicy(t *testing.T) {
118121 ImportStateVerify : true ,
119122 ImportStateVerifyIgnore : []string {"skip_destroy" },
120123 },
124+ {
125+ SkipFunc : versionutils .CheckIfVersionIsUnsupported (minVersionGlobalDataTags ),
126+ Config : testAccResourceAgentPolicyCreateWithGlobalDataTags (policyNameGlobalDataTags , false ),
127+ Check : resource .ComposeTestCheckFunc (
128+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "name" , fmt .Sprintf ("Policy %s" , policyNameGlobalDataTags )),
129+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "namespace" , "default" ),
130+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "description" , "Test Agent Policy" ),
131+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "monitor_logs" , "true" ),
132+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "monitor_metrics" , "false" ),
133+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "skip_destroy" , "false" ),
134+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag1" , "value1" ),
135+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag2" , "value2" ),
136+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag3" , "value3" ),
137+ ),
138+ },
139+ {
140+ SkipFunc : versionutils .CheckIfVersionIsUnsupported (minVersionGlobalDataTags ),
141+ Config : testAccResourceAgentPolicyUpdateWithGlobalDataTags (policyNameGlobalDataTags , false ),
142+ Check : resource .ComposeTestCheckFunc (
143+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "name" , fmt .Sprintf ("Updated Policy %s" , policyNameGlobalDataTags )),
144+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "namespace" , "default" ),
145+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "description" , "This policy was updated" ),
146+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "monitor_logs" , "false" ),
147+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "monitor_metrics" , "true" ),
148+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "skip_destroy" , "false" ),
149+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag1" , "value1a" ),
150+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag2" , "value2b" ),
151+ resource .TestCheckNoResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag3" ),
152+ ),
153+ },
154+ {
155+ SkipFunc : versionutils .CheckIfVersionIsUnsupported (minVersionGlobalDataTags ),
156+ Config : testAccResourceAgentPolicyUpdateWithNoGlobalDataTags (policyNameGlobalDataTags , false ),
157+ Check : resource .ComposeTestCheckFunc (
158+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "name" , fmt .Sprintf ("Updated Policy %s" , policyNameGlobalDataTags )),
159+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "namespace" , "default" ),
160+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "description" , "This policy was updated without global data tags" ),
161+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "monitor_logs" , "false" ),
162+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "monitor_metrics" , "true" ),
163+ resource .TestCheckResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "skip_destroy" , "false" ),
164+ resource .TestCheckNoResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag1" ),
165+ resource .TestCheckNoResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag2" ),
166+ resource .TestCheckNoResourceAttr ("elasticstack_fleet_agent_policy.test_policy" , "global_data_tags.tag3" ),
167+ ),
168+ },
121169 },
122170 })
123171}
@@ -168,6 +216,81 @@ data "elasticstack_fleet_enrollment_tokens" "test_policy" {
168216
169217` , fmt .Sprintf ("Policy %s" , id ), skipDestroy )
170218}
219+ func testAccResourceAgentPolicyCreateWithGlobalDataTags (id string , skipDestroy bool ) string {
220+ return fmt .Sprintf (`
221+ provider "elasticstack" {
222+ elasticsearch {}
223+ kibana {}
224+ }
225+
226+ resource "elasticstack_fleet_agent_policy" "test_policy" {
227+ name = "%s"
228+ namespace = "default"
229+ description = "Test Agent Policy"
230+ monitor_logs = true
231+ monitor_metrics = false
232+ skip_destroy = %t
233+ global_data_tags = {
234+ tag1 = "value1"
235+ tag2 = "value2"
236+ tag3 = "value3"
237+ }
238+ }
239+
240+ data "elasticstack_fleet_enrollment_tokens" "test_policy" {
241+ policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
242+ }
243+
244+ ` , fmt .Sprintf ("Policy %s" , id ), skipDestroy )
245+ }
246+
247+ func testAccResourceAgentPolicyUpdateWithGlobalDataTags (id string , skipDestroy bool ) string {
248+ return fmt .Sprintf (`
249+ provider "elasticstack" {
250+ elasticsearch {}
251+ kibana {}
252+ }
253+
254+ resource "elasticstack_fleet_agent_policy" "test_policy" {
255+ name = "%s"
256+ namespace = "default"
257+ description = "This policy was updated"
258+ monitor_logs = false
259+ monitor_metrics = true
260+ skip_destroy = %t
261+ global_data_tags = {
262+ tag1 = "value1a"
263+ tag2 = "value2b"
264+ }
265+ }
266+
267+ data "elasticstack_fleet_enrollment_tokens" "test_policy" {
268+ policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
269+ }
270+ ` , fmt .Sprintf ("Updated Policy %s" , id ), skipDestroy )
271+ }
272+
273+ func testAccResourceAgentPolicyUpdateWithNoGlobalDataTags (id string , skipDestroy bool ) string {
274+ return fmt .Sprintf (`
275+ provider "elasticstack" {
276+ elasticsearch {}
277+ kibana {}
278+ }
279+
280+ resource "elasticstack_fleet_agent_policy" "test_policy" {
281+ name = "%s"
282+ namespace = "default"
283+ description = "This policy was updated without global data tags"
284+ monitor_logs = false
285+ monitor_metrics = true
286+ skip_destroy = %t
287+ }
288+
289+ data "elasticstack_fleet_enrollment_tokens" "test_policy" {
290+ policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
291+ }
292+ ` , fmt .Sprintf ("Updated Policy %s" , id ), skipDestroy )
293+ }
171294
172295func testAccResourceAgentPolicyUpdate (id string , skipDestroy bool ) string {
173296 return fmt .Sprintf (`
0 commit comments