From f537ddc86d8a85e3f90d0a1db6aff1da8d3594d2 Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Tue, 20 Aug 2024 17:18:38 -0400 Subject: [PATCH] hubclient: inject useragent version Signed-off-by: Nick Santos --- .github/workflows/ci.yaml | 19 +++++++++++++++++++ internal/pkg/hubclient/client.go | 25 +++++++++++++++++++------ internal/provider/provider.go | 7 ++++++- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..10a9585 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,19 @@ +name: CI + +on: + pull_request: + branches: + - 'main' + push: + branches: + - 'main' + +jobs: + build: + name: Build and Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + - run: | + go vet ./... diff --git a/internal/pkg/hubclient/client.go b/internal/pkg/hubclient/client.go index 48f78f0..3d7c394 100644 --- a/internal/pkg/hubclient/client.go +++ b/internal/pkg/hubclient/client.go @@ -25,19 +25,33 @@ type Client struct { BaseURL string auth Auth HTTPClient *http.Client + userAgent string +} + +type Config struct { + Host string + Username string + Password string + UserAgentVersion string } // Create the API client, providing the authentication. -func NewClient(host string, username string, password string) *Client { +func NewClient(config Config) *Client { + version := config.UserAgentVersion + if version == "" { + version = "dev" + } + return &Client{ - BaseURL: host, + BaseURL: config.Host, auth: Auth{ - Username: username, - Password: password, + Username: config.Username, + Password: config.Password, }, HTTPClient: &http.Client{ Timeout: time.Minute, }, + userAgent: fmt.Sprintf("terraform-provider-docker/%s", version), } } @@ -80,8 +94,7 @@ func (c *Client) sendRequest(ctx context.Context, method string, url string, bod } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "application/json; charset=utf-8") - // TODO: put correct client version, or omit completely - req.Header.Set("User-Agent", "terraform-provider-dockerhub/v0.1.0") + req.Header.Set("User-Agent", c.userAgent) req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.Token)) req = req.WithContext(ctx) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 97f5c6e..7097902 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -166,7 +166,12 @@ func (p *DockerHubProvider) Configure(ctx context.Context, req provider.Configur tflog.Debug(ctx, "Creating Docker Hub client") - client := hubclient.NewClient(host, username, password) + client := hubclient.NewClient(hubclient.Config{ + Host: host, + Username: username, + Password: password, + UserAgentVersion: p.version, + }) resp.DataSourceData = client resp.ResourceData = client }