Skip to content

Commit

Permalink
Merge branch 'release/7.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
disaster37 committed Oct 18, 2019
2 parents cdbf87f + 77d6039 commit 8677c52
Show file tree
Hide file tree
Showing 2,792 changed files with 1,234 additions and 836,159 deletions.
12 changes: 11 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Golang CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2
version: 2.1
orbs:
codecov: codecov/[email protected]
jobs:
test:
docker:
Expand All @@ -26,6 +28,14 @@ jobs:
- run: until $(curl --output /dev/null --silent --head --fail -u elastic:changeme http://es:9200); do sleep 5; done
- run: curl -XPOST -u elastic:changeme http://es:9200/_license/start_trial?acknowledge=true
- run: make testacc
- run: |
cat coverage.txt >> coverage.out
mv es/resource_elasticsearch_license_test.go.disable es/resource_elasticsearch_license_test.go
TESTARGS="-run TestAccElasticsearchLicense" make testacc
cat coverage.txt >> coverage.out
- codecov/upload:
file: coverage.out
flags: unittest
build:
docker:
- image: circleci/golang:1.12
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/

dist/
dist/
coverage.txt
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ TEST?=./...
PKG_NAME=es
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
ELASTICSEARCH_URLS ?= http://127.0.0.1:9200
ELASTICSEARCH_USERNAME ?= elastic
ELASTICSEARCH_PASSWORD ?= changeme

default: build

Expand All @@ -16,7 +19,7 @@ test: fmtcheck
go test $(TEST) -timeout=30s -parallel=4

testacc: fmt fmtcheck
TF_ACC=1 go test $(TEST) -v -count 1 -parallel 1 $(TESTARGS) -timeout 120m
ELASTICSEARCH_URLS=${ELASTICSEARCH_URLS} ELASTICSEARCH_USERNAME=${ELASTICSEARCH_USERNAME} ELASTICSEARCH_PASSWORD=${ELASTICSEARCH_PASSWORD} TF_ACC=1 go test $(TEST) -v -count 1 -parallel 1 -race -coverprofile=coverage.txt -covermode=atomic $(TESTARGS) -timeout 120m

fmt:
@echo "==> Fixing source code with gofmt..."
Expand Down Expand Up @@ -86,4 +89,7 @@ ifeq (,$(wildcard $(GOPATH)/src/$(WEBSITE_REPO)))
endif
@$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider-test PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME)

.PHONY: build gen sweep test testacc fmt fmtcheck lint tools test-compile website website-lint website-test
trial-license:
curl -XPOST -u ${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD} ${ELASTICSEARCH_URLS}/_license/start_trial?acknowledge=true

.PHONY: build gen sweep test testacc fmt fmtcheck lint tools test-compile website website-lint website-test trial-license
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# terraform-provider-elasticsearch

[![CircleCI](https://circleci.com/gh/disaster37/terraform-provider-elasticsearch/tree/master.svg?style=svg)](https://circleci.com/gh/disaster37/terraform-provider-elasticsearch/tree/master)
[![CircleCI](https://circleci.com/gh/disaster37/terraform-provider-elasticsearch/tree/7.x.svg?style=svg)](https://circleci.com/gh/disaster37/terraform-provider-elasticsearch/tree/7.x)
[![Go Report Card](https://goreportcard.com/badge/github.com/disaster37/terraform-provider-elasticsearch)](https://goreportcard.com/report/github.com/disaster37/terraform-provider-elasticsearch)
[![GoDoc](https://godoc.org/github.com/disaster37/terraform-provider-elasticsearch?status.svg)](http://godoc.org/github.com/disaster37/terraform-provider-elasticsearch)
[![codecov](https://codecov.io/gh/disaster37/terraform-provider-elasticsearch/branch/7.x/graph/badge.svg)](https://codecov.io/gh/disaster37/terraform-provider-elasticsearch/branch/7.x)

This is a terraform provider that lets you provision elasticsearch resources, compatible with v6 and v7 of elasticsearch.
For Elasticsearch 7, you need to use branch and release 7.x
For Elasticsearch 6, you need to use branch and release 6.x

We fork this project for the following items:
- use official golang SDK to consume Elasticsearch API: https://github.com/elastic/go-elasticsearch
Expand Down Expand Up @@ -45,6 +50,8 @@ provider "elasticsearch" {
- **password**: (optional) The password to connect on it.
- **insecure**: (optional) To disable the certificate check.
- **cacert_file**: (optional) The CA contend to use if you use custom PKI.
- **retry**: (optional) The number of time you should to retry connexion befaore exist with error. Default to `6`.
- **wait_before_retry**: (optional) The number of time in second we wait before each connexion retry. Default to `10`.

___

Expand Down Expand Up @@ -423,7 +430,7 @@ See LICENSE.
## Contributing

1. Fork it ( https://github.com/disaster37/terraform-provider-elasticsearch/fork )
2. Go to develop branch (`git checkout develop`)
2. Go to the right branch (7.x for Elasticsearch 7 or 6.x for Elasticsearch 6) (`git checkout 7.x`)
3. Create your feature branch (`git checkout -b my-new-feature`)
4. Add feature, add acceptance test and tets your code (`ELASTICSEARCH_URLS=http://127.0.0.1:9200 ELASTICSEARCH_USERNAME=elastic ELASTICSEARCH_PASSWORD=changeme make testacc`)
5. Commit your changes (`git commit -am 'Add some feature'`)
Expand Down
35 changes: 6 additions & 29 deletions es/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
log "github.com/sirupsen/logrus"
)

// diffSuppressIndexTemplate permit to compare template in current state vs from API
func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool {
var oo, no map[string]interface{}
if err := json.Unmarshal([]byte(old), &oo); err != nil {
Expand All @@ -35,7 +36,8 @@ func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool
return reflect.DeepEqual(oo[d.Id()], parseAllDotProperties(no))
}

func suppressEquivalentJson(k, old, new string, d *schema.ResourceData) bool {
// suppressEquivalentJSON permit to compare state store as JSON string
func suppressEquivalentJSON(k, old, new string, d *schema.ResourceData) bool {
var oldObj, newObj interface{}
if err := json.Unmarshal([]byte(old), &oldObj); err != nil {
return false
Expand All @@ -46,6 +48,7 @@ func suppressEquivalentJson(k, old, new string, d *schema.ResourceData) bool {
return reflect.DeepEqual(oldObj, newObj)
}

// suppressLicense permit to compare license in current state VS API
func suppressLicense(k, old, new string, d *schema.ResourceData) bool {

oldObj := &LicenseSpec{}
Expand All @@ -66,34 +69,7 @@ func suppressLicense(k, old, new string, d *schema.ResourceData) bool {
return reflect.DeepEqual(oldObj, newObj)
}

func diffSuppressIndexLifecyclePolicy(k, old, new string, d *schema.ResourceData) bool {
var oo, no map[string]interface{}
if err := json.Unmarshal([]byte(old), &oo); err != nil {
return false
}
if err := json.Unmarshal([]byte(new), &no); err != nil {
return false
}

cleanOo := map[string]interface{}{
"policy": oo[d.Id()].(map[string]interface{})["policy"],
}

return reflect.DeepEqual(cleanOo, no)
}

func diffSuppressIngestPipeline(k, old, new string, d *schema.ResourceData) bool {
var oo, no interface{}
if err := json.Unmarshal([]byte(old), &oo); err != nil {
return false
}
if err := json.Unmarshal([]byte(new), &no); err != nil {
return false
}

return reflect.DeepEqual(oo, no)
}

// parseAllDotProperties permit to convert elasticsearch attributes with dot in sub structure
func parseAllDotProperties(data map[string]interface{}) map[string]interface{} {

result := make(map[string]interface{})
Expand All @@ -109,6 +85,7 @@ func parseAllDotProperties(data map[string]interface{}) map[string]interface{} {
return result
}

// parseDotPropertie handle the recursivity to transform attribute that contain dot in sub structure
func parseDotPropertie(key string, value interface{}, result map[string]interface{}) {
if strings.Contains(key, ".") {
listKey := strings.Split(key, ".")
Expand Down
4 changes: 4 additions & 0 deletions es/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package es permiit to handle Elasticsearch API call to manage some resources from Terraform
*/
package es
90 changes: 44 additions & 46 deletions es/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
"net/http"
"net/url"
"strings"
"time"

elastic6 "github.com/elastic/go-elasticsearch/v6"
elastic7 "github.com/elastic/go-elasticsearch/v7"
elastic "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/hashicorp/terraform-plugin-sdk/helper/pathorcontents"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

// Provider permiit to init the terraform provider
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -51,6 +53,18 @@ func Provider() terraform.ResourceProvider {
Default: false,
Description: "Disable SSL verification of API calls",
},
"retry": {
Type: schema.TypeInt,
Optional: true,
Default: 6,
Description: "Nummber time it retry connexion before failed",
},
"wait_before_retry": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
Description: "Wait time in second before retry connexion",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -69,31 +83,33 @@ func Provider() terraform.ResourceProvider {
}
}

// providerConfigure permit to initialize the rest client to access on Elasticsearch API
func providerConfigure(d *schema.ResourceData) (interface{}, error) {

var (
relevantClient interface{}
data map[string]interface{}
data map[string]interface{}
)

URLs := strings.Split(d.Get("urls").(string), ",")
insecure := d.Get("insecure").(bool)
cacertFile := d.Get("cacert_file").(string)
username := d.Get("username").(string)
password := d.Get("password").(string)
retry := d.Get("retry").(int)
waitBeforeRetry := d.Get("wait_before_retry").(int)
transport := &http.Transport{
TLSClientConfig: &tls.Config{},
}
// Checks is valid URLs
for _, rawUrl := range URLs {
_, err := url.Parse(rawUrl)
for _, rawURL := range URLs {
_, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
}

// Intialise connexion
cfg := elastic7.Config{
cfg := elastic.Config{
Addresses: URLs,
}
if username != "" && password != "" {
Expand All @@ -112,18 +128,30 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
transport.TLSClientConfig.RootCAs = caCertPool
}
cfg.Transport = transport
client, err := elastic7.NewClient(cfg)
client, err := elastic.NewClient(cfg)
if err != nil {
return nil, err
}

// Test connexion and check elastic version to use the right Version
res, err := client.API.Info(
client.API.Info.WithContext(context.Background()),
)
if err != nil {
return nil, err
nbFailed := 0
isOnline := false
var res *esapi.Response
for isOnline == false {
res, err = client.API.Info(
client.API.Info.WithContext(context.Background()),
)
if err == nil && res.IsError() == false {
isOnline = true
} else {
if nbFailed == retry {
return nil, err
}
nbFailed++
time.Sleep(time.Duration(waitBeforeRetry) * time.Second)
}
}

defer res.Body.Close()
if res.IsError() {
return nil, errors.Errorf("Error when get info about Elasticsearch client: %s", res.String())
Expand All @@ -134,39 +162,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
version := data["version"].(map[string]interface{})["number"].(string)
log.Debugf("Server: %s", version)

if version < "8.0.0" && version >= "7.0.0" {
log.Printf("[INFO] Using ES 7")
relevantClient = client
} else if version < "7.0.0" && version >= "6.0.0" {
log.Printf("[INFO] Using ES 6")

// Intialise connexion
cfg := elastic6.Config{
Addresses: URLs,
}
if username != "" && password != "" {
cfg.Username = username
cfg.Password = password
}
if insecure == true {
transport.TLSClientConfig.InsecureSkipVerify = true
}
// If a cacertFile has been specified, use that for cert validation
if cacertFile != "" {
caCert, _, _ := pathorcontents.Read(cacertFile)

caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
transport.TLSClientConfig.RootCAs = caCertPool
}
cfg.Transport = transport
relevantClient, err = elastic6.NewClient(cfg)
if err != nil {
return nil, err
}
} else if version < "6.0.0" {
return nil, errors.New("ElasticSearch is older than 6.0.0!")
if version < "7.0.0" || version >= "8.0.0" {
return nil, errors.Errorf("ElasticSearch version is not 7.x (%s), you need to use the right version of elasticsearch provider", version)
}

return relevantClient, nil
return client, nil
}
Loading

0 comments on commit 8677c52

Please sign in to comment.