-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovider.go
More file actions
66 lines (59 loc) · 1.61 KB
/
provider.go
File metadata and controls
66 lines (59 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package provider
import (
"crypto/tls"
"fmt"
"net/http"
"github.com/elastic/go-elasticsearch/v8"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_ENDPOINT", nil),
Description: "The URL for the Elasticsearch instance.",
},
"token": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_API_KEY", nil),
Description: "The token for API authentication.",
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Skip server certificate verification",
},
},
ResourcesMap: map[string]*schema.Resource{
"elkaliases_index_aliases": resourceElkaliasesIndexAliases(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
url := d.Get("url").(string)
token := d.Get("token").(string)
insecure := d.Get("insecure").(bool)
cfg := elasticsearch.Config{
Addresses: []string{url},
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
},
},
Header: http.Header{
"Authorization": []string{fmt.Sprintf("ApiKey %s", token)},
},
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("error creating Elasticsearch client: %s", err)
}
return es, nil
}