-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.go
67 lines (56 loc) · 1.55 KB
/
provider.go
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
67
package main
import (
"context"
"crypto/tls"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/opensearch-project/opensearch-go/v2"
"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
)
func Provider() *schema.Provider {
provider := &schema.Provider{
Schema: map[string]*schema.Schema{
backendAddressField: {
Type: schema.TypeString,
Optional: true,
Default: backendAddress,
},
},
ResourcesMap: map[string]*schema.Resource{
"sample_marvel_character": resourceMarvelCharacter(),
},
DataSourcesMap: map[string]*schema.Resource{
"sample_marvel_character": datasourceMarvelCharacter(),
},
ConfigureContextFunc: providerConfigure,
}
return provider
}
func providerConfigure(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
backendAddress := data.Get(backendAddressField).(string)
backendClient, _ := opensearch.NewClient(
opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{backendAddress},
},
)
pingRequest := opensearchapi.PingRequest{
Pretty: true,
Human: true,
ErrorTrace: true,
}
_, err := pingRequest.Do(ctx, backendClient)
if err != nil {
var diags diag.Diagnostics
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure connecting with the backend",
Detail: "Reason: " + err.Error(),
})
return nil, diags
}
return backendClient, nil
}