-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathresource_opensearch_monitor_test.go
123 lines (108 loc) · 2.62 KB
/
resource_opensearch_monitor_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
package provider
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccOpensearchOpenDistroMonitor(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccOpendistroProviders,
CheckDestroy: testCheckOpensearchMonitorDestroy,
Steps: []resource.TestStep{
{
Config: testAccOpensearchOpenDistroMonitor,
Check: resource.ComposeTestCheckFunc(
testCheckOpensearchMonitorExists("opensearch_monitor.test_monitor"),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testCheckOpensearchMonitorExists(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 monitor ID is set")
}
meta := testAccOpendistroProvider.Meta()
var err error
_, err = resourceOpensearchOpenDistroGetMonitor(rs.Primary.ID, meta.(*ProviderConf))
if err != nil {
return err
}
return nil
}
}
func testCheckOpensearchMonitorDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "opensearch_monitor" {
continue
}
meta := testAccOpendistroProvider.Meta()
var err error
_, err = resourceOpensearchOpenDistroGetMonitor(rs.Primary.ID, meta.(*ProviderConf))
if err != nil {
return nil // should be not found error
}
return fmt.Errorf("Monitor %q still exists", rs.Primary.ID)
}
return nil
}
var testAccOpensearchOpenDistroMonitor = `
resource "opensearch_monitor" "test_monitor" {
body = <<EOF
{
"name": "test-monitor",
"type": "monitor",
"monitor_type": "query_level_monitor",
"enabled": true,
"schedule": {
"period": {
"interval": 1,
"unit": "MINUTES"
}
},
"inputs": [
{
"search": {
"indices": ["*"],
"query": {
"size": 0,
"aggregations": {},
"query": {
"bool": {
"adjust_pure_negative": true,
"boost": 1,
"filter": [
{
"range": {
"@timestamp": {
"boost": 1,
"from": "||-1h",
"to": "",
"include_lower": true,
"include_upper": true,
"format": "epoch_millis"
}
}
}
]
}
}
}
}
}
],
"triggers": []
}
EOF
}
`