@@ -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+ }
0 commit comments