forked from phillbaker/terraform-provider-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_elasticsearch_index_template_test.go
121 lines (108 loc) · 2.84 KB
/
resource_elasticsearch_index_template_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
package main
import (
"context"
"fmt"
"testing"
elastic "gopkg.in/olivere/elastic.v5"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccElasticsearchIndexTemplate(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckElasticsearchIndexTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccElasticsearchIndexTemplate,
Check: resource.ComposeTestCheckFunc(
testCheckElasticsearchIndexTemplateExists("elasticsearch_index_template.test"),
),
},
},
})
}
func testCheckElasticsearchIndexTemplateExists(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 index template ID is set")
}
conn := testAccProvider.Meta().(*elastic.Client)
_, err := conn.IndexGetTemplate(rs.Primary.ID).Do(context.TODO())
if err != nil {
return err
}
return nil
}
}
func testCheckElasticsearchIndexTemplateDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*elastic.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "elasticsearch_index_template" {
continue
}
_, err := conn.IndexGetTemplate(rs.Primary.ID).Do(context.TODO())
if err != nil {
return nil
}
return fmt.Errorf("Index template %q still exists", rs.Primary.ID)
}
return nil
}
var testAccElasticsearchIndexTemplate = `
resource "elasticsearch_index_template" "test" {
name = "terraform-test"
body = <<EOF
{
"template": "logstash-*",
"version": 50001,
"settings": {
"index.refresh_interval": "5s"
},
"mappings": {
"_default_": {
"_all": {"enabled": true, "norms": false},
"dynamic_templates": [ {
"message_field": {
"path_match": "message",
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false
}
}
}, {
"string_fields": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "text", "norms": false,
"fields": {
"keyword": { "type": "keyword" }
}
}
}
} ],
"properties": {
"@timestamp": { "type": "date", "include_in_all": false },
"@version": { "type": "keyword", "include_in_all": false },
"geoip" : {
"dynamic": true,
"properties": {
"ip": { "type": "ip" },
"location": { "type": "geo_point" },
"latitude": { "type": "half_float" },
"longitude": { "type": "half_float" }
}
}
}
}
}
}
EOF
}
`