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

hubclient: inject useragent version #16

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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