-
Notifications
You must be signed in to change notification settings - Fork 113
/
client.go
36 lines (29 loc) · 893 Bytes
/
client.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
package elasticsearch
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/elastic/elastic-package/internal/stack"
"github.com/elastic/go-elasticsearch/v7"
)
// Client method creates new instance of the Elasticsearch client.
func Client() (*elasticsearch.Client, error) {
host := os.Getenv(stack.ElasticsearchHostEnv)
if host == "" {
return nil, undefinedEnvError(stack.ElasticsearchHostEnv)
}
username := os.Getenv(stack.ElasticsearchUsernameEnv)
password := os.Getenv(stack.ElasticsearchPasswordEnv)
client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{host},
Username: username,
Password: password,
})
if err != nil {
return nil, errors.Wrap(err, "creating Elasticsearch client failed")
}
return client, nil
}
func undefinedEnvError(envName string) error {
return fmt.Errorf("undefined environment variable: %s", envName)
}