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
4647type ApiClient struct {
47- es * elasticsearch.Client
48+ es * elasticsearch.Client
49+ version string
4850}
4951
5052func 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
98101func 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