Skip to content

Commit

Permalink
Add support for v6
Browse files Browse the repository at this point in the history
Closes #10.
  • Loading branch information
phillbaker committed Sep 14, 2018
1 parent f8f8659 commit d6faf31
Show file tree
Hide file tree
Showing 13 changed files with 583 additions and 158 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go:
- master
env:
- ES_VERSION=5.3.3 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
- ES_VERSION=6.0.1 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
matrix:
allow_failures:
- go: master
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/phillbaker/terraform-provider-elasticsearch.svg?branch=master)](https://travis-ci.org/phillbaker/terraform-provider-elasticsearch)

This is a terraform provider that lets you provision elasticsearch resources, compatible with v5 of elasticsearch. Based off of an [original PR to Terraform](https://github.com/hashicorp/terraform/pull/13238).
This is a terraform provider that lets you provision elasticsearch resources, compatible with v5 and v6 of elasticsearch. Based off of an [original PR to Terraform](https://github.com/hashicorp/terraform/pull/13238).

## Installation

Expand Down Expand Up @@ -144,7 +144,7 @@ $ terraform plan

#### Shared Credentials file

You can use an AWS credentials file to specify your credentials. The default location is `$HOME/.aws/credentials` on Linux and macOS, or `%USERPROFILE%\.aws\credentials` for Windows users.
You can use an AWS credentials file to specify your credentials. The default location is `$HOME/.aws/credentials` on Linux and macOS, or `%USERPROFILE%\.aws\credentials` for Windows users.

Please refer to the official [userguide](https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html) for instructions on how to create the credentials file.

Expand Down
13 changes: 11 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import:
version: v5.0.69
subpackages:
- uritemplates
- package: gopkg.in/olivere/elastic.v6
version: v6.2.5
subpackages:
- uritemplates
- package: github.com/stretchr/testify
version: ^1.1.4
subpackages:
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: Provider,
})
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: Provider,
})
}
46 changes: 39 additions & 7 deletions provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/tls"
"crypto/x509"
"log"
Expand All @@ -14,7 +15,8 @@ import (
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
elastic "gopkg.in/olivere/elastic.v5"
elastic5 "gopkg.in/olivere/elastic.v5"
elastic6 "gopkg.in/olivere/elastic.v6"
)

var awsUrlRegexp = regexp.MustCompile(`([a-z0-9-]+).es.amazonaws.com$`)
Expand Down Expand Up @@ -83,19 +85,49 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
if err != nil {
return nil, err
}
opts := []elastic.ClientOptionFunc{
elastic.SetURL(rawUrl),
elastic.SetScheme(parsedUrl.Scheme),
opts := []elastic6.ClientOptionFunc{
elastic6.SetURL(rawUrl),
elastic6.SetScheme(parsedUrl.Scheme),
}

if m := awsUrlRegexp.FindStringSubmatch(parsedUrl.Hostname()); m != nil {
log.Printf("[INFO] Using AWS: %+v", m[1])
opts = append(opts, elastic.SetHttpClient(awsHttpClient(m[1], d)), elastic.SetSniff(false))
opts = append(opts, elastic6.SetHttpClient(awsHttpClient(m[1], d)), elastic6.SetSniff(false))
} else if insecure || cacertFile != "" {
opts = append(opts, elastic.SetHttpClient(tlsHttpClient(d)), elastic.SetSniff(false))
opts = append(opts, elastic6.SetHttpClient(tlsHttpClient(d)), elastic6.SetSniff(false))
}

return elastic.NewClient(opts...)
var relevantClient interface{}
client, err := elastic6.NewClient(opts...)
if err != nil {
return nil, err
}
relevantClient = client

// Use the v6 client to ping the cluster to determine the version
info, _, err := client.Ping(rawUrl).Do(context.TODO())
if err != nil {
return nil, err
}
if info.Version.Number < "6.0.0" {
log.Printf("[INFO] Using ES 5")
opts := []elastic5.ClientOptionFunc{
elastic5.SetURL(rawUrl),
elastic5.SetScheme(parsedUrl.Scheme),
}

if m := awsUrlRegexp.FindStringSubmatch(parsedUrl.Hostname()); m != nil {
opts = append(opts, elastic5.SetHttpClient(awsHttpClient(m[1], d)), elastic5.SetSniff(false))
} else if insecure || cacertFile != "" {
opts = append(opts, elastic5.SetHttpClient(tlsHttpClient(d)), elastic5.SetSniff(false))
}
relevantClient, err = elastic5.NewClient(opts...)
if err != nil {
return nil, err
}
}

return relevantClient, nil
}

func awsHttpClient(region string, d *schema.ResourceData) *http.Client {
Expand Down
97 changes: 86 additions & 11 deletions resource_elasticsearch_index_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"encoding/json"

"github.com/hashicorp/terraform/helper/schema"
elastic "gopkg.in/olivere/elastic.v5"
elastic5 "gopkg.in/olivere/elastic.v5"
elastic6 "gopkg.in/olivere/elastic.v6"
)

func resourceElasticsearchIndexTemplate() *schema.Resource {
Expand Down Expand Up @@ -39,39 +40,113 @@ func resourceElasticsearchIndexTemplateCreate(d *schema.ResourceData, meta inter
}

func resourceElasticsearchIndexTemplateRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*elastic.Client)
res, err := client.IndexGetTemplate(d.Id()).Do(context.TODO())
if err != nil {
return err
id := d.Id()

var result string
var err error
switch meta.(type) {
case *elastic6.Client:
client := meta.(*elastic6.Client)
result, err = elastic6IndexGetTemplate(client, id)
default:
client := meta.(*elastic5.Client)
result, err = elastic5IndexGetTemplate(client, id)
}
t := res[d.Id()]
tj, err := json.Marshal(t)
if err != nil {
return err
}

d.Set("name", d.Id())
d.Set("body", string(tj))
d.Set("body", result)
return nil
}

func elastic6IndexGetTemplate(client *elastic6.Client, id string) (string, error) {
res, err := client.IndexGetTemplate(id).Do(context.TODO())
if err != nil {
return "", err
}

t := res[id]
tj, err := json.Marshal(t)
if err != nil {
return "", err
}
return string(tj), nil
}

func elastic5IndexGetTemplate(client *elastic5.Client, id string) (string, error) {
res, err := client.IndexGetTemplate(id).Do(context.TODO())
if err != nil {
return "", err
}

t := res[id]
tj, err := json.Marshal(t)
if err != nil {
return "", err
}

return string(tj), nil
}

func resourceElasticsearchIndexTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceElasticsearchPutIndexTemplate(d, meta, false)
}

func resourceElasticsearchIndexTemplateDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*elastic.Client)
_, err := client.IndexDeleteTemplate(d.Id()).Do(context.TODO())
id := d.Id()

var err error
switch meta.(type) {
case *elastic6.Client:
client := meta.(*elastic6.Client)
err = elastic6IndexDeleteTemplate(client, id)
default:
client := meta.(*elastic5.Client)
err = elastic5IndexDeleteTemplate(client, id)
}

if err != nil {
return err
}
d.SetId("")
return nil
}

func elastic6IndexDeleteTemplate(client *elastic6.Client, id string) error {
_, err := client.IndexDeleteTemplate(id).Do(context.TODO())
return err
}

func elastic5IndexDeleteTemplate(client *elastic5.Client, id string) error {
_, err := client.IndexDeleteTemplate(id).Do(context.TODO())
return err
}

func resourceElasticsearchPutIndexTemplate(d *schema.ResourceData, meta interface{}, create bool) error {
client := meta.(*elastic.Client)
name := d.Get("name").(string)
body := d.Get("body").(string)

var err error
switch meta.(type) {
case *elastic6.Client:
client := meta.(*elastic6.Client)
err = elastic6IndexPutTemplate(client, name, body, create)
default:
client := meta.(*elastic5.Client)
err = elastic5IndexPutTemplate(client, name, body, create)
}

return err
}

func elastic6IndexPutTemplate(client *elastic6.Client, name string, body string, create bool) error {
_, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
return err
}

func elastic5IndexPutTemplate(client *elastic5.Client, name string, body string, create bool) error {
_, err := client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
return err
}
Loading

0 comments on commit d6faf31

Please sign in to comment.