Skip to content

Commit b7de448

Browse files
Upgrade to terraform SDK2 (#2)
* 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 <linuxworkgroup@hotmail.com> * Fix some codes Signed-off-by: disaster37 <linuxworkgroup@hotmail.com> * Fix logs Signed-off-by: disaster37 <linuxworkgroup@hotmail.com> * Fix test with add default property Signed-off-by: disaster37 <linuxworkgroup@hotmail.com> * Format main.go Signed-off-by: disaster37 <linuxworkgroup@hotmail.com> Co-authored-by: admin <admin@admin.com>
1 parent cc45045 commit b7de448

36 files changed

Lines changed: 864 additions & 438 deletions

.circleci/config.yml

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ orbs:
77
jobs:
88
test:
99
docker:
10-
- image: circleci/golang:1.12
10+
- image: circleci/golang:1.16
1111
environment:
12-
- GO111MODULE: "on"
1312
- ELASTICSEARCH_URLS: "http://es:9200"
1413
- ELASTICSEARCH_USERNAME: "elastic"
1514
- ELASTICSEARCH_PASSWORD: "changeme"
16-
- image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
15+
- image: docker.elastic.co/elasticsearch/elasticsearch:7.12.1
1716
name: es
1817
environment:
1918
- cluster.name: "test"
@@ -38,30 +37,11 @@ jobs:
3837
flags: unittest
3938
build:
4039
docker:
41-
- image: circleci/golang:1.12
42-
environment:
43-
- GO111MODULE: "on"
40+
- image: circleci/golang:1.16
4441
working_directory: /go/src/github.com/disaster37/terraform-provider-elasticsearch
4542
steps:
4643
- checkout
4744
- run: make build
48-
release:
49-
docker:
50-
- image: circleci/golang:1.12
51-
environment:
52-
- GO111MODULE: "on"
53-
- GITHUB_TOKEN: $GITHUB_TOKEN
54-
- CIRCLE_PROJECT_USERNAME: $CIRCLE_PROJECT_USERNAME
55-
- CIRCLE_PROJECT_REPONAME: $CIRCLE_PROJECT_REPONAME
56-
- CIRCLE_TAG: $CIRCLE_TAG
57-
working_directory: /go/src/github.com/disaster37/terraform-provider-elasticsearch
58-
steps:
59-
- checkout
60-
- run: go get github.com/mitchellh/gox
61-
- run: go get github.com/tcnksm/ghr
62-
- run: gox -os="freebsd darwin linux windows" -arch "386 amd64" -parallel=1 -output "dist/terraform-provider-elasticsearch_${CIRCLE_TAG}_{{.OS}}_{{.Arch}}"
63-
- 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"
64-
- run: ghr -t $GITHUB_TOKEN -u $CIRCLE_PROJECT_USERNAME -r $CIRCLE_PROJECT_REPONAME $CIRCLE_TAG dist/
6545
workflows:
6646
version: 2
6747
build-workflow:
@@ -75,13 +55,4 @@ workflows:
7555
- test
7656
filters:
7757
tags:
78-
only: /.*/
79-
#- release:
80-
# context: Github
81-
# requires:
82-
# - build
83-
# filters:
84-
# tags:
85-
# only: /.*/
86-
# branches:
87-
# ignore: /.*/
58+
only: /.*/

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
name: Set up Go
2929
uses: actions/setup-go@v2
3030
with:
31-
go-version: 1.14
31+
go-version: 1.16
3232
-
3333
name: Import GPG key
3434
id: import_gpg

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@
1515
# vendor/
1616

1717
dist/
18-
coverage.txt
18+
coverage.txt
19+
20+
# terraform
21+
.terraform.lock.hcl
22+
*.tfstate
23+
*.tfstate.*
24+
.terraform
25+
registry/

.theia/launch.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug current file",
6+
"type": "go",
7+
"request": "launch",
8+
"mode": "auto",
9+
"program": "${fileDirname}"
10+
}
11+
]
12+
}

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,15 @@ endif
9292
trial-license:
9393
curl -XPOST -u ${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD} ${ELASTICSEARCH_URLS}/_license/start_trial?acknowledge=true
9494

95-
.PHONY: build gen sweep test testacc fmt fmtcheck lint tools test-compile website website-lint website-test trial-license
95+
start-pods: clean-pods
96+
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"
97+
98+
clean-pods:
99+
kubectl delete --ignore-not-found pod/elasticsearch
100+
kubectl delete --ignore-not-found service/elasticsearch
101+
102+
local-build:
103+
mkdir -p registry/registry.terraform.io/disaster37/elasticsearch/1.0.0/linux_amd64
104+
go build -o registry/registry.terraform.io/disaster37/elasticsearch/1.0.0/linux_amd64/terraform-provider-elasticsearch
105+
106+
.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

es/diff_suppress_funcs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"reflect"
66
"strings"
77

8-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
log "github.com/sirupsen/logrus"
1010
)
1111

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

36+
ob, _ := json.Marshal(oo[d.Id()])
37+
nb, _ := json.Marshal(parseAllDotProperties(no))
38+
39+
log.Debugf("Old: %s", string(ob))
40+
log.Debugf("New: %s", string(nb))
41+
3642
return reflect.DeepEqual(oo[d.Id()], parseAllDotProperties(no))
3743
}
3844

es/provider.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"encoding/json"
8+
"io/ioutil"
89
"net/http"
910
"net/url"
11+
"os"
1012
"strings"
1113
"time"
1214

1315
elastic "github.com/elastic/go-elasticsearch/v7"
1416
"github.com/elastic/go-elasticsearch/v7/esapi"
15-
"github.com/hashicorp/terraform-plugin-sdk/helper/pathorcontents"
16-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
17-
"github.com/hashicorp/terraform-plugin-sdk/terraform"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
"github.com/mitchellh/go-homedir"
1819
"github.com/pkg/errors"
1920
log "github.com/sirupsen/logrus"
2021
)
2122

2223
// Provider permiit to init the terraform provider
23-
func Provider() terraform.ResourceProvider {
24+
func Provider() *schema.Provider {
2425
return &schema.Provider{
2526
Schema: map[string]*schema.Schema{
2627
"urls": {
@@ -121,7 +122,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
121122
}
122123
// If a cacertFile has been specified, use that for cert validation
123124
if cacertFile != "" {
124-
caCert, _, _ := pathorcontents.Read(cacertFile)
125+
caCert, _, _ := read(cacertFile)
125126

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

169170
return client, nil
170171
}
172+
173+
// If the argument is a path, Read loads it and returns the contents,
174+
// otherwise the argument is assumed to be the desired contents and is simply
175+
// returned.
176+
//
177+
// The boolean second return value can be called `wasPath` - it indicates if a
178+
// path was detected and a file loaded.
179+
func read(poc string) (string, bool, error) {
180+
if len(poc) == 0 {
181+
return poc, false, nil
182+
}
183+
184+
path := poc
185+
if path[0] == '~' {
186+
var err error
187+
path, err = homedir.Expand(path)
188+
if err != nil {
189+
return path, true, err
190+
}
191+
}
192+
193+
if _, err := os.Stat(path); err == nil {
194+
contents, err := ioutil.ReadFile(path)
195+
if err != nil {
196+
return string(contents), true, err
197+
}
198+
return string(contents), true, nil
199+
}
200+
201+
return poc, false, nil
202+
}

es/provider_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"os"
55
"testing"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8-
"github.com/hashicorp/terraform-plugin-sdk/terraform"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
98
"github.com/sirupsen/logrus"
109
prefixed "github.com/x-cray/logrus-prefixed-formatter"
1110
)
1211

13-
var testAccProviders map[string]terraform.ResourceProvider
12+
var testAccProviders map[string]*schema.Provider
1413
var testAccProvider *schema.Provider
1514

1615
func init() {
@@ -20,25 +19,25 @@ func init() {
2019
logrus.SetLevel(logrus.DebugLevel)
2120

2221
// Init provider
23-
testAccProvider = Provider().(*schema.Provider)
22+
testAccProvider = Provider()
2423
configureFunc := testAccProvider.ConfigureFunc
2524
testAccProvider.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
2625
return configureFunc(d)
2726
}
28-
testAccProviders = map[string]terraform.ResourceProvider{
27+
testAccProviders = map[string]*schema.Provider{
2928
"elasticsearch": testAccProvider,
3029
}
3130

3231
}
3332

3433
func TestProvider(t *testing.T) {
35-
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
34+
if err := Provider().InternalValidate(); err != nil {
3635
t.Fatalf("err: %s", err)
3736
}
3837
}
3938

4039
func TestProvider_impl(t *testing.T) {
41-
var _ terraform.ResourceProvider = Provider()
40+
var _ *schema.Provider = Provider()
4241
}
4342

4443
func testAccPreCheck(t *testing.T) {

es/resource_elasticsearch_index_lifecycle_policy.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515

1616
elastic "github.com/elastic/go-elasticsearch/v7"
17-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1818
"github.com/pkg/errors"
1919
log "github.com/sirupsen/logrus"
2020
)
@@ -101,11 +101,19 @@ func resourceElasticsearchIndexLifecyclePolicyRead(d *schema.ResourceData, meta
101101
return err
102102
}
103103
policy := policyTemp[id].(map[string]interface{})["policy"]
104+
policyTemp = map[string]interface{}{
105+
"policy": policy,
106+
}
104107

105-
log.Debugf("Policy : %+v", policy)
108+
log.Debugf("Policy : %+v", policyTemp)
106109

107110
d.Set("name", id)
108-
d.Set("policy", policy)
111+
112+
flattenPolicy, err := convertInterfaceToJsonString(policyTemp)
113+
if err != nil {
114+
return err
115+
}
116+
d.Set("policy", flattenPolicy)
109117
return nil
110118
}
111119

es/resource_elasticsearch_index_lifecycle_policy_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"testing"
77

88
elastic "github.com/elastic/go-elasticsearch/v7"
9-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10-
"github.com/hashicorp/terraform-plugin-sdk/terraform"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1111
"github.com/pkg/errors"
1212
)
1313

@@ -120,7 +120,9 @@ resource "elasticsearch_index_lifecycle_policy" "test" {
120120
"delete": {
121121
"min_age": "30d",
122122
"actions": {
123-
"delete": {}
123+
"delete": {
124+
"delete_searchable_snapshot": true
125+
}
124126
}
125127
}
126128
}
@@ -148,7 +150,9 @@ resource "elasticsearch_index_lifecycle_policy" "test" {
148150
"delete": {
149151
"min_age": "31d",
150152
"actions": {
151-
"delete": {}
153+
"delete": {
154+
"delete_searchable_snapshot": true
155+
}
152156
}
153157
}
154158
}

0 commit comments

Comments
 (0)