Skip to content

Commit

Permalink
Upgrade to terraform SDK2 (#2)
Browse files Browse the repository at this point in the history
* Upgrade to elasticsearch 7.12

* Basic license continue to exist

* Fix compare index template

* Fix compare index template

* Fix compare index template

* Fix compare index template

* Fix compare index template

* Fix compare index template

* Fix compare index template

* Upgrade terraform SDK and use golang 1.16

Signed-off-by: disaster37 <[email protected]>

* Fix some codes

Signed-off-by: disaster37 <[email protected]>

* Fix logs

Signed-off-by: disaster37 <[email protected]>

* Fix test with add default property

Signed-off-by: disaster37 <[email protected]>

* Format main.go

Signed-off-by: disaster37 <[email protected]>

Co-authored-by: admin <[email protected]>
  • Loading branch information
disaster37 and lordlezehaf authored Sep 2, 2021
1 parent cc45045 commit b7de448
Show file tree
Hide file tree
Showing 36 changed files with 864 additions and 438 deletions.
37 changes: 4 additions & 33 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ orbs:
jobs:
test:
docker:
- image: circleci/golang:1.12
- image: circleci/golang:1.16
environment:
- GO111MODULE: "on"
- ELASTICSEARCH_URLS: "http://es:9200"
- ELASTICSEARCH_USERNAME: "elastic"
- ELASTICSEARCH_PASSWORD: "changeme"
- image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
- image: docker.elastic.co/elasticsearch/elasticsearch:7.12.1
name: es
environment:
- cluster.name: "test"
Expand All @@ -38,30 +37,11 @@ jobs:
flags: unittest
build:
docker:
- image: circleci/golang:1.12
environment:
- GO111MODULE: "on"
- image: circleci/golang:1.16
working_directory: /go/src/github.com/disaster37/terraform-provider-elasticsearch
steps:
- checkout
- run: make build
release:
docker:
- image: circleci/golang:1.12
environment:
- GO111MODULE: "on"
- GITHUB_TOKEN: $GITHUB_TOKEN
- CIRCLE_PROJECT_USERNAME: $CIRCLE_PROJECT_USERNAME
- CIRCLE_PROJECT_REPONAME: $CIRCLE_PROJECT_REPONAME
- CIRCLE_TAG: $CIRCLE_TAG
working_directory: /go/src/github.com/disaster37/terraform-provider-elasticsearch
steps:
- checkout
- run: go get github.com/mitchellh/gox
- run: go get github.com/tcnksm/ghr
- run: gox -os="freebsd darwin linux windows" -arch "386 amd64" -parallel=1 -output "dist/terraform-provider-elasticsearch_${CIRCLE_TAG}_{{.OS}}_{{.Arch}}"
- run: CGO_ENABLED=0 gox -os="freebsd darwin linux windows" -arch "386 amd64" -parallel=1 -output "dist/terraform-provider-elasticsearch_${CIRCLE_TAG}_{{.OS}}_{{.Arch}}_static"
- run: ghr -t $GITHUB_TOKEN -u $CIRCLE_PROJECT_USERNAME -r $CIRCLE_PROJECT_REPONAME $CIRCLE_TAG dist/
workflows:
version: 2
build-workflow:
Expand All @@ -75,13 +55,4 @@ workflows:
- test
filters:
tags:
only: /.*/
#- release:
# context: Github
# requires:
# - build
# filters:
# tags:
# only: /.*/
# branches:
# ignore: /.*/
only: /.*/
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.16
-
name: Import GPG key
id: import_gpg
Expand Down
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
# vendor/

dist/
coverage.txt
coverage.txt

# terraform
.terraform.lock.hcl
*.tfstate
*.tfstate.*
.terraform
registry/
12 changes: 12 additions & 0 deletions .theia/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug current file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,15 @@ endif
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
start-pods: clean-pods
kubectl run elasticsearch --image docker.elastic.co/elasticsearch/elasticsearch:7.5.1 --port "9200" --expose --env "cluster.name=test" --env "discovery.type=single-node" --env "ELASTIC_PASSWORD=changeme" --env "xpack.security.enabled=true" --env "ES_JAVA_OPTS=-Xms512m -Xmx512m" --env "path.repo=/tmp" --limits "cpu=500m,memory=1024Mi"

clean-pods:
kubectl delete --ignore-not-found pod/elasticsearch
kubectl delete --ignore-not-found service/elasticsearch

local-build:
mkdir -p registry/registry.terraform.io/disaster37/elasticsearch/1.0.0/linux_amd64
go build -o registry/registry.terraform.io/disaster37/elasticsearch/1.0.0/linux_amd64/terraform-provider-elasticsearch

.PHONY: build gen sweep test testacc fmt fmtcheck lint tools test-compile website website-lint website-test trial-license start-pods clean-pods local-build
8 changes: 7 additions & 1 deletion es/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -33,6 +33,12 @@ func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool
no["aliases"] = make(map[string]interface{})
}

ob, _ := json.Marshal(oo[d.Id()])
nb, _ := json.Marshal(parseAllDotProperties(no))

log.Debugf("Old: %s", string(ob))
log.Debugf("New: %s", string(nb))

return reflect.DeepEqual(oo[d.Id()], parseAllDotProperties(no))
}

Expand Down
42 changes: 37 additions & 5 deletions es/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"

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/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

// Provider permiit to init the terraform provider
func Provider() terraform.ResourceProvider {
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"urls": {
Expand Down Expand Up @@ -121,7 +122,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
// If a cacertFile has been specified, use that for cert validation
if cacertFile != "" {
caCert, _, _ := pathorcontents.Read(cacertFile)
caCert, _, _ := read(cacertFile)

caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
Expand Down Expand Up @@ -168,3 +169,34 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {

return client, nil
}

// If the argument is a path, Read loads it and returns the contents,
// otherwise the argument is assumed to be the desired contents and is simply
// returned.
//
// The boolean second return value can be called `wasPath` - it indicates if a
// path was detected and a file loaded.
func read(poc string) (string, bool, error) {
if len(poc) == 0 {
return poc, false, nil
}

path := poc
if path[0] == '~' {
var err error
path, err = homedir.Expand(path)
if err != nil {
return path, true, err
}
}

if _, err := os.Stat(path); err == nil {
contents, err := ioutil.ReadFile(path)
if err != nil {
return string(contents), true, err
}
return string(contents), true, nil
}

return poc, false, nil
}
13 changes: 6 additions & 7 deletions es/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
Expand All @@ -20,25 +19,25 @@ func init() {
logrus.SetLevel(logrus.DebugLevel)

// Init provider
testAccProvider = Provider().(*schema.Provider)
testAccProvider = Provider()
configureFunc := testAccProvider.ConfigureFunc
testAccProvider.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
return configureFunc(d)
}
testAccProviders = map[string]terraform.ResourceProvider{
testAccProviders = map[string]*schema.Provider{
"elasticsearch": testAccProvider,
}

}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
var _ *schema.Provider = Provider()
}

func testAccPreCheck(t *testing.T) {
Expand Down
14 changes: 11 additions & 3 deletions es/resource_elasticsearch_index_lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"

elastic "github.com/elastic/go-elasticsearch/v7"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -101,11 +101,19 @@ func resourceElasticsearchIndexLifecyclePolicyRead(d *schema.ResourceData, meta
return err
}
policy := policyTemp[id].(map[string]interface{})["policy"]
policyTemp = map[string]interface{}{
"policy": policy,
}

log.Debugf("Policy : %+v", policy)
log.Debugf("Policy : %+v", policyTemp)

d.Set("name", id)
d.Set("policy", policy)

flattenPolicy, err := convertInterfaceToJsonString(policyTemp)
if err != nil {
return err
}
d.Set("policy", flattenPolicy)
return nil
}

Expand Down
12 changes: 8 additions & 4 deletions es/resource_elasticsearch_index_lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

elastic "github.com/elastic/go-elasticsearch/v7"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -120,7 +120,9 @@ resource "elasticsearch_index_lifecycle_policy" "test" {
"delete": {
"min_age": "30d",
"actions": {
"delete": {}
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
Expand Down Expand Up @@ -148,7 +150,9 @@ resource "elasticsearch_index_lifecycle_policy" "test" {
"delete": {
"min_age": "31d",
"actions": {
"delete": {}
"delete": {
"delete_searchable_snapshot": true
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion es/resource_elasticsearch_index_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"

elastic "github.com/elastic/go-elasticsearch/v7"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
Expand Down
4 changes: 2 additions & 2 deletions es/resource_elasticsearch_index_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

elastic "github.com/elastic/go-elasticsearch/v7"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
)

Expand Down
2 changes: 1 addition & 1 deletion es/resource_elasticsearch_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

elastic "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
Expand Down
8 changes: 5 additions & 3 deletions es/resource_elasticsearch_license_test.go.disable
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

elastic "github.com/elastic/go-elasticsearch/v7"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -88,7 +88,9 @@ func testCheckElasticsearchLicenseDestroy(s *terraform.State) error {
}
}

return fmt.Errorf("License still exists")
// The basic license continue to exist
//return fmt.Errorf("License still exists")
return nil
}

return nil
Expand Down
Loading

0 comments on commit b7de448

Please sign in to comment.