Skip to content

Commit

Permalink
chore: add user agent (#178)
Browse files Browse the repository at this point in the history
* chore: add user agent

* refactor: pass version in a simpler way
  • Loading branch information
nunogois authored Aug 6, 2024
1 parent 753d28c commit 4656689
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type UnleashProvider struct {
version string
}

const UserAgent = "Terraform-Provider-Unleash"

// ScaffoldingProviderMofunc (p *UnleashProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {del describes the provider data model.
type UnleashConfiguration struct {
BaseUrl types.String `tfsdk:"base_url"`
Expand All @@ -45,7 +47,7 @@ func (p *UnleashProvider) Metadata(ctx context.Context, req provider.MetadataReq
resp.Version = p.version
}

func unleashClient(ctx context.Context, config *UnleashConfiguration, diagnostics *diag.Diagnostics) *unleash.APIClient {
func unleashClient(ctx context.Context, provider *UnleashProvider, config *UnleashConfiguration, diagnostics *diag.Diagnostics) *unleash.APIClient {
base_url := strings.TrimSuffix(configValue(config.BaseUrl, "UNLEASH_URL"), "/")
authorization := configValue(config.Authorization, "AUTH_TOKEN", "UNLEASH_AUTH_TOKEN")
mustHave("base_url", base_url, diagnostics)
Expand All @@ -65,6 +67,7 @@ func unleashClient(ctx context.Context, config *UnleashConfiguration, diagnostic
},
}
unleashConfig.AddDefaultHeader("Authorization", authorization)
unleashConfig.UserAgent = fmt.Sprintf("%s/%s", UserAgent, provider.version)

logLevel := strings.ToLower(os.Getenv("TF_LOG"))
isDebug := logLevel == "debug" || logLevel == "trace"
Expand Down Expand Up @@ -159,7 +162,7 @@ func (p *UnleashProvider) Configure(ctx context.Context, req provider.ConfigureR
}

// Configuration values are now available.
client := unleashClient(ctx, &config, &resp.Diagnostics)
client := unleashClient(ctx, p, &config, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Unable to prepare client")
return
Expand Down Expand Up @@ -199,6 +202,6 @@ func (p *UnleashProvider) DataSources(ctx context.Context) []func() datasource.D

func New(version string) func() provider.Provider {
return func() provider.Provider {
return &UnleashProvider{}
return &UnleashProvider{version}
}
}

0 comments on commit 4656689

Please sign in to comment.