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

add custom header to every request with the calling function name #577

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 26 additions & 5 deletions pagerduty/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"net/http"
"runtime"
"strings"
"sync"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
Expand Down Expand Up @@ -53,6 +55,8 @@ func (c *Config) Client() (*pagerduty.Client, error) {

// Return the previously-configured client if available.
if c.client != nil {
// Set caller name to x-terraform-function custom header if client was cached
c.client.SetXTerraformFunctionHeader(extractCallerName(2))
return c.client, nil
}

Expand All @@ -71,11 +75,12 @@ func (c *Config) Client() (*pagerduty.Client, error) {
}

config := &pagerduty.Config{
BaseURL: apiUrl,
Debug: logging.IsDebugOrHigher(),
HTTPClient: httpClient,
Token: c.Token,
UserAgent: c.UserAgent,
BaseURL: apiUrl,
Debug: logging.IsDebugOrHigher(),
HTTPClient: httpClient,
Token: c.Token,
UserAgent: c.UserAgent,
XTerraformFunctionHeader: extractCallerName(2),
}

client, err := pagerduty.NewClient(config)
Expand Down Expand Up @@ -135,3 +140,19 @@ func (c *Config) SlackClient() (*pagerduty.Client, error) {

return c.slackClient, nil
}

func extractCallerName(skip int) string {
var callerName string

pc, _, _, ok := runtime.Caller(skip)
details := runtime.FuncForPC(pc)
if ok && details != nil {
name := details.Name()
idx := strings.LastIndex(name, ".")
if idx > 0 {
callerName = name[idx+1:]
}
}

return callerName
}
34 changes: 34 additions & 0 deletions pagerduty/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,37 @@ func TestConfigCustomAppUrl(t *testing.T) {
t.Fatalf("error: expected the client to not fail: %v", err)
}
}

func TestConfigXTerraformFunctionHeader(t *testing.T) {
config := Config{
Token: "foo",
AppUrl: "https://app.domain.tld",
SkipCredsValidation: true,
}

client, err := config.Client()
if err != nil {
t.Fatalf("error: expected the client to not fail: %v", err)
}
want := "TestConfigXTerraformFunctionHeader"
if got := client.Config.XTerraformFunctionHeader; got != want {
t.Fatalf("error: expected %q as value for \"x-terraform-function-header\", but got %q", want, got)
}
}

func TestExtractCallerName(t *testing.T) {
cases := []struct {
skip int
want string
}{
{0, "extractCallerName"},
{1, "TestExtractCallerName"},
{2, "tRunner"},
}

for _, c := range cases {
if got := extractCallerName(c.skip); got != c.want {
t.Fatalf("error: expected %q, but got %q", c.want, got)
}
}
}