Skip to content

Commit

Permalink
hubclient: inject useragent version
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks committed Aug 20, 2024
1 parent f531b5a commit f537ddc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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 ./...
25 changes: 19 additions & 6 deletions internal/pkg/hubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f537ddc

Please sign in to comment.