Skip to content

Commit 71ef408

Browse files
basvandijkphillbaker
authored andcommitted
Support ElasticSearch-7.x.x
1 parent 6cf7873 commit 71ef408

9 files changed

+229
-9
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ go:
55
env:
66
- ES_VERSION=5.6.16 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
77
- ES_VERSION=6.8.0 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
8+
- ES_VERSION=7.0.1 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz
89
matrix:
910
allow_failures:
1011
- go: master
1112
install:
1213
- wget ${ES_DOWNLOAD_URL}
13-
- tar -xzf elasticsearch-${ES_VERSION}.tar.gz
14+
- tar -xzf elasticsearch-${ES_VERSION}*.tar.gz
1415
- ./elasticsearch-${ES_VERSION}/bin/elasticsearch -Epath.repo=/tmp &
1516
- export ELASTICSEARCH_URL=http://127.0.0.1:9200
1617
- export TF_LOG=INFO

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/grpc-ecosystem/grpc-gateway v1.6.2 // indirect
1010
github.com/hashicorp/terraform v0.12.0
1111
github.com/olivere/elastic v6.2.18+incompatible // indirect
12+
github.com/olivere/elastic/v7 v7.0.1
1213
gopkg.in/olivere/elastic.v5 v5.0.81
1314
gopkg.in/olivere/elastic.v6 v6.2.18
1415
)

provider.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
78
"log"
89
"net/http"
910
"net/url"
@@ -15,6 +16,7 @@ import (
1516
"github.com/hashicorp/terraform/helper/pathorcontents"
1617
"github.com/hashicorp/terraform/helper/schema"
1718
"github.com/hashicorp/terraform/terraform"
19+
elastic7 "github.com/olivere/elastic/v7"
1820
elastic5 "gopkg.in/olivere/elastic.v5"
1921
elastic6 "gopkg.in/olivere/elastic.v6"
2022
)
@@ -85,20 +87,21 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
8587
if err != nil {
8688
return nil, err
8789
}
88-
opts := []elastic6.ClientOptionFunc{
89-
elastic6.SetURL(rawUrl),
90-
elastic6.SetScheme(parsedUrl.Scheme),
90+
91+
opts := []elastic7.ClientOptionFunc{
92+
elastic7.SetURL(rawUrl),
93+
elastic7.SetScheme(parsedUrl.Scheme),
9194
}
9295

9396
if m := awsUrlRegexp.FindStringSubmatch(parsedUrl.Hostname()); m != nil {
9497
log.Printf("[INFO] Using AWS: %+v", m[1])
95-
opts = append(opts, elastic6.SetHttpClient(awsHttpClient(m[1], d)), elastic6.SetSniff(false))
98+
opts = append(opts, elastic7.SetHttpClient(awsHttpClient(m[1], d)), elastic7.SetSniff(false))
9699
} else if insecure || cacertFile != "" {
97-
opts = append(opts, elastic6.SetHttpClient(tlsHttpClient(d)), elastic6.SetSniff(false))
100+
opts = append(opts, elastic7.SetHttpClient(tlsHttpClient(d)), elastic7.SetSniff(false))
98101
}
99102

100103
var relevantClient interface{}
101-
client, err := elastic6.NewClient(opts...)
104+
client, err := elastic7.NewClient(opts...)
102105
if err != nil {
103106
return nil, err
104107
}
@@ -109,7 +112,25 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
109112
if err != nil {
110113
return nil, err
111114
}
112-
if info.Version.Number < "6.0.0" {
115+
116+
if info.Version.Number < "7.0.0" && info.Version.Number >= "6.0.0" {
117+
log.Printf("[INFO] Using ES 6")
118+
opts := []elastic6.ClientOptionFunc{
119+
elastic6.SetURL(rawUrl),
120+
elastic6.SetScheme(parsedUrl.Scheme),
121+
}
122+
123+
if m := awsUrlRegexp.FindStringSubmatch(parsedUrl.Hostname()); m != nil {
124+
log.Printf("[INFO] Using AWS: %+v", m[1])
125+
opts = append(opts, elastic6.SetHttpClient(awsHttpClient(m[1], d)), elastic6.SetSniff(false))
126+
} else if insecure || cacertFile != "" {
127+
opts = append(opts, elastic6.SetHttpClient(tlsHttpClient(d)), elastic6.SetSniff(false))
128+
}
129+
relevantClient, err = elastic6.NewClient(opts...)
130+
if err != nil {
131+
return nil, err
132+
}
133+
} else if info.Version.Number < "6.0.0" && info.Version.Number >= "5.0.0" {
113134
log.Printf("[INFO] Using ES 5")
114135
opts := []elastic5.ClientOptionFunc{
115136
elastic5.SetURL(rawUrl),
@@ -125,6 +146,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
125146
if err != nil {
126147
return nil, err
127148
}
149+
} else if info.Version.Number < "5.0.0" {
150+
return nil, errors.New("ElasticSearch is older than 5.0.0!")
128151
}
129152

130153
return relevantClient, nil

resource_elasticsearch_index_template.go

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66

77
"github.com/hashicorp/terraform/helper/schema"
8+
elastic7 "github.com/olivere/elastic/v7"
89
elastic5 "gopkg.in/olivere/elastic.v5"
910
elastic6 "gopkg.in/olivere/elastic.v6"
1011
)
@@ -45,6 +46,9 @@ func resourceElasticsearchIndexTemplateRead(d *schema.ResourceData, meta interfa
4546
var result string
4647
var err error
4748
switch meta.(type) {
49+
case *elastic7.Client:
50+
client := meta.(*elastic7.Client)
51+
result, err = elastic7IndexGetTemplate(client, id)
4852
case *elastic6.Client:
4953
client := meta.(*elastic6.Client)
5054
result, err = elastic6IndexGetTemplate(client, id)
@@ -61,6 +65,20 @@ func resourceElasticsearchIndexTemplateRead(d *schema.ResourceData, meta interfa
6165
return nil
6266
}
6367

68+
func elastic7IndexGetTemplate(client *elastic7.Client, id string) (string, error) {
69+
res, err := client.IndexGetTemplate(id).Do(context.TODO())
70+
if err != nil {
71+
return "", err
72+
}
73+
74+
t := res[id]
75+
tj, err := json.Marshal(t)
76+
if err != nil {
77+
return "", err
78+
}
79+
return string(tj), nil
80+
}
81+
6482
func elastic6IndexGetTemplate(client *elastic6.Client, id string) (string, error) {
6583
res, err := client.IndexGetTemplate(id).Do(context.TODO())
6684
if err != nil {
@@ -99,6 +117,9 @@ func resourceElasticsearchIndexTemplateDelete(d *schema.ResourceData, meta inter
99117

100118
var err error
101119
switch meta.(type) {
120+
case *elastic7.Client:
121+
client := meta.(*elastic7.Client)
122+
err = elastic7IndexDeleteTemplate(client, id)
102123
case *elastic6.Client:
103124
client := meta.(*elastic6.Client)
104125
err = elastic6IndexDeleteTemplate(client, id)
@@ -114,6 +135,11 @@ func resourceElasticsearchIndexTemplateDelete(d *schema.ResourceData, meta inter
114135
return nil
115136
}
116137

138+
func elastic7IndexDeleteTemplate(client *elastic7.Client, id string) error {
139+
_, err := client.IndexDeleteTemplate(id).Do(context.TODO())
140+
return err
141+
}
142+
117143
func elastic6IndexDeleteTemplate(client *elastic6.Client, id string) error {
118144
_, err := client.IndexDeleteTemplate(id).Do(context.TODO())
119145
return err
@@ -130,6 +156,9 @@ func resourceElasticsearchPutIndexTemplate(d *schema.ResourceData, meta interfac
130156

131157
var err error
132158
switch meta.(type) {
159+
case *elastic7.Client:
160+
client := meta.(*elastic7.Client)
161+
err = elastic7IndexPutTemplate(client, name, body, create)
133162
case *elastic6.Client:
134163
client := meta.(*elastic6.Client)
135164
err = elastic6IndexPutTemplate(client, name, body, create)
@@ -141,6 +170,11 @@ func resourceElasticsearchPutIndexTemplate(d *schema.ResourceData, meta interfac
141170
return err
142171
}
143172

173+
func elastic7IndexPutTemplate(client *elastic7.Client, name string, body string, create bool) error {
174+
_, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
175+
return err
176+
}
177+
144178
func elastic6IndexPutTemplate(client *elastic6.Client, name string, body string, create bool) error {
145179
_, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
146180
return err

resource_elasticsearch_index_template_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8+
elastic7 "github.com/olivere/elastic/v7"
89
elastic5 "gopkg.in/olivere/elastic.v5"
910
elastic6 "gopkg.in/olivere/elastic.v6"
1011

@@ -22,6 +23,8 @@ func TestAccElasticsearchIndexTemplate(t *testing.T) {
2223
meta := provider.Meta()
2324
var config string
2425
switch meta.(type) {
26+
case *elastic7.Client:
27+
config = testAccElasticsearchIndexTemplateV7
2528
case *elastic6.Client:
2629
config = testAccElasticsearchIndexTemplateV6
2730
default:
@@ -58,6 +61,9 @@ func testCheckElasticsearchIndexTemplateExists(name string) resource.TestCheckFu
5861

5962
var err error
6063
switch meta.(type) {
64+
case *elastic7.Client:
65+
client := meta.(*elastic7.Client)
66+
_, err = client.IndexGetTemplate(rs.Primary.ID).Do(context.TODO())
6167
case *elastic6.Client:
6268
client := meta.(*elastic6.Client)
6369
_, err = client.IndexGetTemplate(rs.Primary.ID).Do(context.TODO())
@@ -84,6 +90,9 @@ func testCheckElasticsearchIndexTemplateDestroy(s *terraform.State) error {
8490

8591
var err error
8692
switch meta.(type) {
93+
case *elastic7.Client:
94+
client := meta.(*elastic7.Client)
95+
_, err = client.IndexGetTemplate(rs.Primary.ID).Do(context.TODO())
8796
case *elastic6.Client:
8897
client := meta.(*elastic6.Client)
8998
_, err = client.IndexGetTemplate(rs.Primary.ID).Do(context.TODO())
@@ -165,3 +174,33 @@ resource "elasticsearch_index_template" "test" {
165174
EOF
166175
}
167176
`
177+
178+
var testAccElasticsearchIndexTemplateV7 = `
179+
resource "elasticsearch_index_template" "test" {
180+
name = "terraform-test"
181+
body = <<EOF
182+
{
183+
"index_patterns": ["te*", "bar*"],
184+
"settings": {
185+
"index": {
186+
"number_of_shards": 1
187+
}
188+
},
189+
"mappings": {
190+
"_source": {
191+
"enabled": false
192+
},
193+
"properties": {
194+
"host_name": {
195+
"type": "keyword"
196+
},
197+
"created_at": {
198+
"type": "date",
199+
"format": "EEE MMM dd HH:mm:ss Z YYYY"
200+
}
201+
}
202+
}
203+
}
204+
EOF
205+
}
206+
`

0 commit comments

Comments
 (0)