-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathresource_opensearch_ism_policy_test.go
151 lines (134 loc) · 3.09 KB
/
resource_opensearch_ism_policy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package provider
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccOpensearchISMPolicy(t *testing.T) {
provider := Provider()
diags := provider.Configure(context.Background(), &terraform.ResourceConfig{})
if diags.HasError() {
t.Skipf("err: %#v", diags)
}
var allowed bool
config := testAccOpensearchISMPolicyV7
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if !allowed {
t.Skip("OpenSearch ISMPolicies only supported on ES 6.")
}
},
Providers: testAccOpendistroProviders,
CheckDestroy: testCheckOpensearchISMPolicyDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckOpensearchISMPolicyExists("opensearch_ism_policy.test_policy"),
resource.TestCheckResourceAttr(
"opensearch_ism_policy.test_policy",
"policy_id",
"test_policy",
),
),
},
},
})
}
func testCheckOpensearchISMPolicyExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No policy ID is set")
}
meta := testAccOpendistroProvider.Meta()
var err error
_, err = resourceOpensearchGetISMPolicy(rs.Primary.ID, meta.(*ProviderConf))
if err != nil {
return err
}
return nil
}
}
func testCheckOpensearchISMPolicyDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "opensearch_ism_policy" {
continue
}
meta := testAccOpendistroProvider.Meta()
var err error
if err != nil {
return err
}
_, err = resourceOpensearchGetISMPolicy(rs.Primary.ID, meta.(*ProviderConf))
if err != nil {
return nil // should be not found error
}
return fmt.Errorf("OpenDistroISMPolicy %q still exists", rs.Primary.ID)
}
return nil
}
var testAccOpensearchISMPolicyV7 = `
resource "opensearch_ism_policy" "test_policy" {
policy_id = "test_policy"
body = <<EOF
{
"policy": {
"description": "ingesting logs",
"default_state": "ingest",
"ism_template": [{
"index_patterns": ["foo-*"],
"priority": 0
}],
"error_notification": {
"destination": {
"slack": {
"url": "https://webhook.slack.example.com"
}
},
"message_template": {
"lang": "mustache",
"source": "The index *{{ctx.index}}* failed to rollover."
}
},
"states": [
{
"name": "ingest",
"actions": [{
"rollover": {
"min_doc_count": 5
}
}],
"transitions": [{
"state_name": "search"
}]
},
{
"name": "search",
"actions": [],
"transitions": [{
"state_name": "delete",
"conditions": {
"min_index_age": "5m"
}
}]
},
{
"name": "delete",
"actions": [{
"delete": {}
}],
"transitions": []
}
]
}
}
EOF
}
`