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 User-Agent header to all API calls #566

Merged
merged 19 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions .changes/v3.0.0/544-notes.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Dropped support for VCD 9.5 [GH-544]
* Added support for VCD 10.2 [GH-544]
2 changes: 1 addition & 1 deletion .changes/v3.0.0/550-features.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* Add support for NSX-T Org VDC [GH-550]
* Added support for NSX-T Org VDC [GH-550]
2 changes: 1 addition & 1 deletion .changes/v3.0.0/553-features.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* **New Resource**: `vcd_vm_sizing_policy` VDC VM sizing policy [GH-553]
* **New Data Source**: `vcd_vm_sizing_policy` VDC VM sizing policy [GH-553]
* **New Data Source**: `vcd_vm_sizing_policy` VDC VM sizing policy [GH-553]
1 change: 1 addition & 0 deletions .changes/v3.0.0/566-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Provider will send HTTP User-Agent while performing API calls [GH-566]
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ matrix:
allow_failures:
- go: tip
include:
- go: "1.14.x"
- go: "1.15.x"
env: GOFLAGS=-mod=vendor GO111MODULE=on
script:
- make test
- make vet
- make static
- make website-test
- go: "1.14.x"
- go: "1.15.x"
env: GO111MODULE=on
script:
- make test
Expand Down
10 changes: 6 additions & 4 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
TEST?=$$(go list ./... |grep -v 'vendor')
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
GIT_DESCRIBE=$(shell git describe)

PKG_NAME=vcd

default: build

# builds the plugin
# builds the plugin injecting output of `git describe` to BuildVersion variable
build: fmtcheck
go install
go install -ldflags="-X 'github.com/vmware/terraform-provider-vcd/v3/vcd.BuildVersion=$(GIT_DESCRIBE)'"

# builds the plugin with race detector enabled
# builds the plugin with race detector enabled and injecting output of `git describe` to BuildVersion variable
buildrace: fmtcheck
go install --race
go install --race -ldflags="-X 'github.com/vmware/terraform-provider-vcd/v3/vcd.BuildVersion=$(GIT_DESCRIBE)'"

# creates a .zip archive of the code
dist:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ require (
github.com/vmware/go-vcloud-director/v2 v2.9.0-beta.1
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
)

replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.9.0-alpha.5.0.20201005065001-e21c68e8c19c
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Didainius/go-vcloud-director/v2 v2.9.0-alpha.5.0.20201005065001-e21c68e8c19c h1:6an0YFpYUxRI/ZM5gb3QtTaE1VFWlX/zgRiIq9H6w+Q=
github.com/Didainius/go-vcloud-director/v2 v2.9.0-alpha.5.0.20201005065001-e21c68e8c19c/go.mod h1:czvTQZlB4/WsOsL7rMVCb+SYAPJhx/dYoS/Sk7rc/O0=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
Expand Down Expand Up @@ -213,8 +215,6 @@ github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvc
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU=
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmware/go-vcloud-director/v2 v2.9.0-beta.1 h1:vsHMjOyBba5iHXFrEL4+Gj/kfy9xwVc1nZ5a979G/LA=
github.com/vmware/go-vcloud-director/v2 v2.9.0-beta.1/go.mod h1:czvTQZlB4/WsOsL7rMVCb+SYAPJhx/dYoS/Sk7rc/O0=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
Expand All @@ -228,7 +228,6 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -252,7 +251,6 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb h1:TR699M2v0qoKTOHxeLgp6zPqaQNs74f01a/ob9W0qko=
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
30 changes: 21 additions & 9 deletions vcd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"

Expand All @@ -15,6 +16,13 @@ import (
"github.com/vmware/go-vcloud-director/v2/govcd"
)

func init() {
separator := os.Getenv("VCD_IMPORT_SEPARATOR")
if separator != "" {
ImportSeparator = separator
}
}

type Config struct {
User string
Password string
Expand Down Expand Up @@ -424,10 +432,14 @@ func (c *Config) Client() (*VCDClient, error) {
return nil, fmt.Errorf("something went wrong while retrieving URL: %s", err)
}

userAgent := buildUserAgent(BuildVersion, c.SysOrg)

vcdClient := &VCDClient{
VCDClient: govcd.NewVCDClient(*authUrl, c.InsecureFlag,
govcd.WithMaxRetryTimeout(c.MaxRetryTimeout),
govcd.WithSamlAdfs(c.UseSamlAdfs, c.CustomAdfsRptId)),
govcd.WithSamlAdfs(c.UseSamlAdfs, c.CustomAdfsRptId),
govcd.WithHttpUserAgent(userAgent),
),
SysOrg: c.SysOrg,
Org: c.Org,
Vdc: c.Vdc,
Expand All @@ -445,9 +457,8 @@ func (c *Config) Client() (*VCDClient, error) {
return vcdClient, nil
}

// Returns the name of the function that called the
// current function.
// It is used for tracing
// callFuncName returns the name of the function that called the current function. It is used for
// tracing
func callFuncName() string {
fpcs := make([]uintptr, 1)
n := runtime.Callers(3, fpcs)
Expand All @@ -460,9 +471,10 @@ func callFuncName() string {
return ""
}

func init() {
separator := os.Getenv("VCD_IMPORT_SEPARATOR")
if separator != "" {
ImportSeparator = separator
}
// buildUserAgent helps to construct HTTP User-Agent header
func buildUserAgent(version, sysOrg string) string {
userAgent := fmt.Sprintf("terraform-provider-vcd/%s (%s/%s; isProvider:%t)",
version, runtime.GOOS, runtime.GOARCH, strings.ToLower(sysOrg) == "system")

return userAgent
}
7 changes: 6 additions & 1 deletion vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ func setTestEnv() {
// This function is called before any other test
func TestMain(m *testing.M) {

// Set BuildVersion to have consistent User-Agent for tests:
// [e.g. terraform-provider-vcd/test (darwin/amd64; isProvider:true)]
BuildVersion = "test"

// Enable custom flags
flag.Parse()
setTestEnv()
Expand Down Expand Up @@ -807,7 +811,8 @@ func getTestVCDFromJson(testConfig TestConfig) (*govcd.VCDClient, error) {
return &govcd.VCDClient{}, fmt.Errorf("could not parse Url: %s", err)
}
vcdClient := govcd.NewVCDClient(*configUrl, true,
govcd.WithSamlAdfs(testConfig.Provider.UseSamlAdfs, testConfig.Provider.CustomAdfsRptId))
govcd.WithSamlAdfs(testConfig.Provider.UseSamlAdfs, testConfig.Provider.CustomAdfsRptId),
govcd.WithHttpUserAgent(buildUserAgent("test", testConfig.Provider.SysOrg)))
return vcdClient, nil
}

Expand Down
4 changes: 4 additions & 0 deletions vcd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/vmware/go-vcloud-director/v2/util"
)

// BuildVersion holds version which is meant to be injected at build time using ldflags
// (e.g. 'go build -ldflags="-X 'github.com/vmware/terraform-provider-vcd/v3/vcd.BuildVersion=v1.0.0'"')
var BuildVersion = "unset"

// DataSources is a public function which allows to filter and access all defined data sources
// When 'nameRegexp' is not empty - it will return only those matching the regexp
// When 'includeDeprecated' is false - it will skip out the resources which have a DeprecationMessage set
Expand Down
28 changes: 28 additions & 0 deletions vcd/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package vcd

import (
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -85,3 +86,30 @@ func minIfLess(min, value int) int {

return value
}

// TestAccClientUserAgent ensures that client initialization config.Client() used by provider initializes
// go-vcloud-director client by having User-Agent set
func TestAccClientUserAgent(t *testing.T) {
clientConfig := Config{
User: testConfig.Provider.User,
Password: testConfig.Provider.Password,
Token: testConfig.Provider.Token,
SysOrg: testConfig.Provider.SysOrg,
Org: testConfig.VCD.Org,
Vdc: testConfig.VCD.Vdc,
Href: testConfig.Provider.Url,
MaxRetryTimeout: testConfig.Provider.MaxRetryTimeout,
InsecureFlag: testConfig.Provider.AllowInsecure,
}

vcdClient, err := clientConfig.Client()
if err != nil {
t.Fatal("error initializing go-vcloud-director client: " + err.Error())
}

expectedHeaderPrefix := "terraform-provider-vcd/"
if !strings.HasPrefix(vcdClient.VCDClient.Client.UserAgent, expectedHeaderPrefix) {
t.Fatalf("Expected User-Agent header in go-vcloud-director to be '%s', got '%s'",
expectedHeaderPrefix, vcdClient.VCDClient.Client.UserAgent)
}
}
16 changes: 15 additions & 1 deletion vendor/github.com/vmware/go-vcloud-director/v2/govcd/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading