Skip to content

Commit

Permalink
Adds Terraform User Agent capability (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjaroiswebdev authored Dec 15, 2023
1 parent f23264c commit e5fc19b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
18 changes: 17 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ type Client struct {
// PagerDuty API. You can use either *http.Client here, or your own
// implementation.
HTTPClient HTTPClient

userAgent string
}

// NewClient creates an API client using an account/user API token
Expand Down Expand Up @@ -322,6 +324,14 @@ func WithAPIEndpoint(endpoint string) ClientOptions {
}
}

// WithTerraformProvider configures the client to be used as the PagerDuty
// Terraform provider
func WithTerraformProvider(version string) ClientOptions {
return func(c *Client) {
c.userAgent = fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, version)
}
}

// WithV2EventsAPIEndpoint allows for a custom V2 Events API endpoint to be passed into the client
func WithV2EventsAPIEndpoint(endpoint string) ClientOptions {
return func(c *Client) {
Expand Down Expand Up @@ -498,7 +508,13 @@ func (c *Client) prepRequest(req *http.Request, authRequired bool, headers map[s
}
}

req.Header.Set("User-Agent", userAgentHeader)
var userAgent string
if c.userAgent != "" {
userAgent = c.userAgent
} else {
userAgent = userAgentHeader
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", contentTypeHeader)
}

Expand Down
49 changes: 49 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -687,6 +688,54 @@ func TestClient_Do(t *testing.T) {
}
}

func TestClient_UserAgentDefault(t *testing.T) {
setup()
defer teardown()

defaultUserAgent := userAgentHeader
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
userAgentHeader := r.Header.Get("User-Agent")
if userAgentHeader != defaultUserAgent {
t.Fatalf("want %q, but got %q", defaultUserAgent, userAgentHeader)
}
w.WriteHeader(http.StatusOK)
})

client := defaultTestClient(server.URL, "foo")

_, err := client.do(context.Background(), "GET", "/foo", nil, nil)
if err != nil {
t.Fatal(err)
}
}

func TestClient_UserAgentOverwrite(t *testing.T) {
setup()
defer teardown()

terraformVersion := "terraform-version-for-testing"
newUserAgent := fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, terraformVersion)
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
userAgentHeader := r.Header.Get("User-Agent")
if userAgentHeader != newUserAgent {
t.Fatalf("want %q, but got %q", newUserAgent, userAgentHeader)
}
w.WriteHeader(http.StatusOK)
})

client := NewClient("foo",
WithAPIEndpoint(server.URL),
WithTerraformProvider(terraformVersion),
)

_, err := client.do(context.Background(), "GET", "/foo", nil, nil)
if err != nil {
t.Fatal(err)
}
}

func TestNullAPIErrorObject_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit e5fc19b

Please sign in to comment.