Skip to content

Commit

Permalink
provider: introduce FindGoModuleVersion helper for module versions
Browse files Browse the repository at this point in the history
Before this change, we relied on the plugin libraries to expose their version. Unfortunately, this was incorrectly set in SDKv2[1] and non-existent in the framework[2].

The recommended approach is to instead dig into the build information via`runtime/debug.ReadBuildInfo()`[3] which is what we introduce here in a slightly safer and consistent way.

[1]: hashicorp/terraform-plugin-sdk#1257
[2]: hashicorp/terraform-plugin-framework#855
[3]: https://pkg.go.dev/runtime/debug#ReadBuildInfo

Signed-off-by: Jacob Bednarz <[email protected]>
  • Loading branch information
jacobbednarz committed Oct 12, 2023
1 parent b68b789 commit 39e9d6a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 2 additions & 0 deletions internal/framework/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,11 @@ func (p *CloudflareProvider) Configure(ctx context.Context, req provider.Configu

options = append(options, cloudflare.Debug(logging.IsDebugOrHigher()))

pluginVersion := utils.FindGoModuleVersion("github.com/hashicorp/terraform-plugin-framework")
userAgentParams := utils.UserAgentBuilderParams{
ProviderVersion: &p.version,
PluginType: cloudflare.StringPtr("terraform-plugin-framework"),
PluginVersion: pluginVersion,
}
if !data.UserAgentOperatorSuffix.IsNull() {
userAgentParams.OperatorSuffix = cloudflare.StringPtr(data.UserAgentOperatorSuffix.String())
Expand Down
4 changes: 2 additions & 2 deletions internal/sdkv2provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
)

func init() {
Expand Down Expand Up @@ -383,10 +382,11 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema

options = append(options, cloudflare.Debug(logging.IsDebugOrHigher()))

pluginVersion := utils.FindGoModuleVersion("github.com/hashicorp/terraform-plugin-sdk/v2")
userAgentParams := utils.UserAgentBuilderParams{
ProviderVersion: cloudflare.StringPtr(version),
PluginType: cloudflare.StringPtr("terraform-plugin-sdk"),
PluginVersion: cloudflare.StringPtr(meta.SDKVersionString()),
PluginVersion: pluginVersion,
}
if v, ok := d.GetOk(consts.UserAgentOperatorSuffixSchemaKey); ok {
userAgentParams.OperatorSuffix = cloudflare.StringPtr(v.(string))
Expand Down
33 changes: 33 additions & 0 deletions internal/utils/get_go_module_version.go
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
}
23 changes: 19 additions & 4 deletions internal/utils/user_agent_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ import (
)

type UserAgentBuilderParams struct {
ProviderVersion *string
PluginVersion *string
PluginType *string
// 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
OperatorSuffix *string

// Customised operation suffix to append to the user agent for identifying
// traffic. Mutually exclusive with `TerraformVersion`.
OperatorSuffix *string
}

func (p *UserAgentBuilderParams) String() string {
Expand Down Expand Up @@ -37,6 +50,8 @@ func (p *UserAgentBuilderParams) String() string {
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()
}

0 comments on commit 39e9d6a

Please sign in to comment.