Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom UserAgent #451

Merged
merged 4 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions gitlab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitlab
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"

Expand All @@ -12,12 +13,13 @@ import (

// Config is per-provider, specifies where to connect to gitlab
type Config struct {
Token string
BaseURL string
Insecure bool
CACertFile string
ClientCert string
ClientKey string
Token string
BaseURL string
Insecure bool
CACertFile string
ClientCert string
ClientKey string
TerraformVersion string
}

// Client returns a *gitlab.Client to interact with the configured gitlab instance
Expand Down Expand Up @@ -61,6 +63,10 @@ func (c *Config) Client() (interface{}, error) {
Transport: logging.NewTransport("GitLab", t),
},
),
func(client *gitlab.Client) error {
client.UserAgent = fmt.Sprintf("terraform-provider-gitlab/%s", c.TerraformVersion)
return nil
},
}

if c.BaseURL != "" {
Expand Down
15 changes: 8 additions & 7 deletions gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ func init() {

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Token: d.Get("token").(string),
BaseURL: d.Get("base_url").(string),
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
ClientCert: d.Get("client_cert").(string),
ClientKey: d.Get("client_key").(string),
Token: d.Get("token").(string),
BaseURL: d.Get("base_url").(string),
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
ClientCert: d.Get("client_cert").(string),
ClientKey: d.Get("client_key").(string),
TerraformVersion: "TODO", // TODO: How to get Terraform version?
bishtawi marked this conversation as resolved.
Show resolved Hide resolved
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client := provider.Client()
client.UserAgent = httpclient.TerraformUserAgent(p.TerraformVersion)
return client

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Personally I am not a fan of the providerConfigure function customizing the client as that seems to be the responsibility of the Client() function but the code is indeed simpler.

return config.Client()
Expand All @@ -132,7 +133,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
func validateApiURLVersion(value interface{}, key string) (ws []string, es []error) {
v := value.(string)
if strings.HasSuffix(v, "/api/v3") || strings.HasSuffix(v, "/api/v3/") {
es = append(es, fmt.Errorf("terraform-gitlab-provider does not support v3 api; please upgrade to /api/v4 in %s", v))
es = append(es, fmt.Errorf("terraform-provider-gitlab does not support v3 api; please upgrade to /api/v4 in %s", v))
roidelapluie marked this conversation as resolved.
Show resolved Hide resolved
}
return
}