-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2831 from cloudflare/allow-ua-operator-appending
Allow setting the User Agent Operator suffix
- Loading branch information
Showing
8 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:internal | ||
provider: updated user agent string to now be `terraform-provider-cloudflare/<version> <plugin> <operator suffix>` | ||
``` | ||
|
||
```release-note:enhancement | ||
provider: allow defining a user agent operator suffix through the schema field (`user_agent_operator_suffix`) and via the environment variable (`CLOUDFLARE_USER_AGENT_OPERATOR_SUFFIX`) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils | ||
|
||
import ( | ||
"runtime/debug" | ||
"strings" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
) | ||
|
||
// FindGoModuleVersion digs into the build information and extracts the version | ||
// of a module for use without the prefixed `v` (should it exist). | ||
func FindGoModuleVersion(modulePath string) *string { | ||
info, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
// shouldn't ever happen but just in case we aren't using modules | ||
return nil | ||
} | ||
|
||
for _, mod := range info.Deps { | ||
if mod.Path != modulePath { | ||
continue | ||
} | ||
|
||
version := mod.Version | ||
if strings.HasPrefix(version, "v") { | ||
version = strings.TrimPrefix(version, "v") | ||
} | ||
|
||
return cloudflare.StringPtr(version) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type UserAgentBuilderParams struct { | ||
// Version of `terraform-provider-cloudflare`. | ||
ProviderVersion *string | ||
|
||
// Version of `terraform-plugin-*` libraries that we rely on for the internal | ||
// operations. | ||
PluginVersion *string | ||
|
||
// Which plugin is in use. Currently only available options are | ||
// `terraform-plugin-sdk` and `terraform-plugin-framework`. | ||
PluginType *string | ||
|
||
// Version of Terraform that is initiating the operation. Mutually exclusive | ||
// with `OperatorSuffix`. | ||
TerraformVersion *string | ||
|
||
// Customised operation suffix to append to the user agent for identifying | ||
// traffic. Mutually exclusive with `TerraformVersion`. | ||
OperatorSuffix *string | ||
} | ||
|
||
func (p *UserAgentBuilderParams) String() string { | ||
var ua string | ||
if p.ProviderVersion != nil { | ||
ua += fmt.Sprintf("terraform-provider-cloudflare/%s", *p.ProviderVersion) | ||
} | ||
|
||
if p.PluginType != nil { | ||
ua += fmt.Sprintf(" %s", *p.PluginType) | ||
} | ||
|
||
if p.PluginVersion != nil { | ||
ua += fmt.Sprintf("/%s", *p.PluginVersion) | ||
} | ||
|
||
// Operator suffix and Terraform version are mutually exclusive and we should | ||
// only ever see one of them. | ||
if p.OperatorSuffix != nil { | ||
ua += fmt.Sprintf(" %s", *p.OperatorSuffix) | ||
} else if p.TerraformVersion != nil { | ||
ua += fmt.Sprintf(" terraform/%s", *p.TerraformVersion) | ||
} | ||
|
||
return ua | ||
} | ||
|
||
// BuildUserAgent takes the `UserAgentBuilderParams` and contextually builds | ||
// a HTTP user agent for making API calls. | ||
func BuildUserAgent(params UserAgentBuilderParams) string { | ||
return params.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package utils | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
) | ||
|
||
func TestUserAgentBuilding(t *testing.T) { | ||
tests := []struct { | ||
input UserAgentBuilderParams | ||
expect string | ||
}{ | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0")}, expect: "terraform-provider-cloudflare/1.0"}, | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0"), PluginType: cloudflare.StringPtr("terraform-plugin-foo")}, expect: "terraform-provider-cloudflare/1.0 terraform-plugin-foo"}, | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0"), PluginType: cloudflare.StringPtr("terraform-plugin-foo"), PluginVersion: cloudflare.StringPtr("1.2.3")}, expect: "terraform-provider-cloudflare/1.0 terraform-plugin-foo/1.2.3"}, | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0"), PluginType: cloudflare.StringPtr("terraform-plugin-foo"), PluginVersion: cloudflare.StringPtr("1.2.3"), TerraformVersion: cloudflare.StringPtr("9.9.9")}, expect: "terraform-provider-cloudflare/1.0 terraform-plugin-foo/1.2.3 terraform/9.9.9"}, | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0"), OperatorSuffix: cloudflare.StringPtr("example/v88")}, expect: "terraform-provider-cloudflare/1.0 example/v88"}, | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0"), OperatorSuffix: cloudflare.StringPtr("example/v88"), TerraformVersion: cloudflare.StringPtr("1.2.3")}, expect: "terraform-provider-cloudflare/1.0 example/v88"}, | ||
{input: UserAgentBuilderParams{ProviderVersion: cloudflare.StringPtr("1.0"), TerraformVersion: cloudflare.StringPtr("1.2.3")}, expect: "terraform-provider-cloudflare/1.0 terraform/1.2.3"}, | ||
} | ||
|
||
for _, tc := range tests { | ||
got := BuildUserAgent(tc.input) | ||
if !reflect.DeepEqual(tc.expect, got) { | ||
t.Fatalf("expected: %v, got: %v", tc.expect, got) | ||
} | ||
} | ||
} |