Skip to content

Commit 35628f7

Browse files
authored
Merge pull request #31 from olksdr/feat/add-ua-header
Set User-Agent header in the Elasticsearch client
2 parents 1fe77bf + 47dae21 commit 35628f7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

internal/clients/api_client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"log"
9+
"net/http"
910
"os"
1011
"strings"
1112

@@ -44,13 +45,15 @@ func (c *CompositeId) String() string {
4445
}
4546

4647
type ApiClient struct {
47-
es *elasticsearch.Client
48+
es *elasticsearch.Client
49+
version string
4850
}
4951

5052
func NewApiClientFunc(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
5153
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
5254
var diags diag.Diagnostics
5355
config := elasticsearch.Config{}
56+
config.Header = http.Header{"User-Agent": []string{fmt.Sprintf("elasticstack-terraform-provider/%s", version)}}
5457

5558
if v, ok := d.GetOk("elasticsearch"); ok {
5659
// if defined we must have only one entry
@@ -91,14 +94,17 @@ func NewApiClientFunc(version string, p *schema.Provider) func(context.Context,
9194
})
9295
}
9396

94-
return &ApiClient{es}, diags
97+
return &ApiClient{es, version}, diags
9598
}
9699
}
97100

98101
func NewApiClient(d *schema.ResourceData, meta interface{}) (*ApiClient, error) {
102+
defaultClient := meta.(*ApiClient)
99103
// if the config provided let's use it
100104
if esConn, ok := d.GetOk("elasticsearch_connection"); ok {
101105
config := elasticsearch.Config{}
106+
config.Header = http.Header{"User-Agent": []string{fmt.Sprintf("elasticstack-terraform-provider/%s", defaultClient.version)}}
107+
102108
// there is always only 1 connection per resource
103109
conn := esConn.([]interface{})[0].(map[string]interface{})
104110

@@ -120,9 +126,9 @@ func NewApiClient(d *schema.ResourceData, meta interface{}) (*ApiClient, error)
120126
if err != nil {
121127
return nil, fmt.Errorf("Unable to create Elasticsearch client")
122128
}
123-
return &ApiClient{es}, nil
129+
return &ApiClient{es, defaultClient.version}, nil
124130
} else { // or return the default client
125-
return meta.(*ApiClient), nil
131+
return defaultClient, nil
126132
}
127133
}
128134

0 commit comments

Comments
 (0)