|
| 1 | +package cloudflare |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/pulumi/pulumi/sdk/v3/go/common/resource" |
| 7 | + |
| 8 | + "github.com/pulumi/pulumi-cloudflare/provider/v6/pkg/version" |
| 9 | +) |
| 10 | + |
| 11 | +// userAgentOperatorSuffixKey is the Pulumi name of upstream's `user_agent_operator_suffix` provider |
| 12 | +// config attribute. PreConfigureCallback receives provider config keyed by Pulumi names, not |
| 13 | +// Terraform names. |
| 14 | +const userAgentOperatorSuffixKey = resource.PropertyKey("userAgentOperatorSuffix") |
| 15 | + |
| 16 | +// userAgentOperatorSuffixEnvVar is documented by upstream's schema but never actually read by its |
| 17 | +// Configure implementation in terraform-provider-cloudflare v5, so we honor it ourselves. |
| 18 | +const userAgentOperatorSuffixEnvVar = "CLOUDFLARE_USER_AGENT_OPERATOR_SUFFIX" |
| 19 | + |
| 20 | +// pulumiUserAgentOperatorSuffix is the identifier we send to Cloudflare by default. |
| 21 | +func pulumiUserAgentOperatorSuffix() string { |
| 22 | + // version.Version is populated by -ldflags at build time, so it is only ever empty in `go test` |
| 23 | + // and `go run` builds. |
| 24 | + if version.Version == "" { |
| 25 | + return "pulumi" |
| 26 | + } |
| 27 | + return "pulumi/" + version.Version |
| 28 | +} |
| 29 | + |
| 30 | +// setUserAgentOperatorSuffix makes Cloudflare API requests identify themselves as coming from |
| 31 | +// Pulumi, so that Cloudflare can attribute traffic to the provider rather than to Terraform. |
| 32 | +// |
| 33 | +// Upstream composes the User-Agent as |
| 34 | +// |
| 35 | +// terraform-provider-cloudflare/<v> terraform-plugin-framework/<v> <suffix> |
| 36 | +// |
| 37 | +// where <suffix> is the operator suffix if one is set, and `terraform/<version>` otherwise. The |
| 38 | +// latter is meaningless under Pulumi - the bridge synthesizes a fake Terraform version - so we |
| 39 | +// always prefer the suffix. |
| 40 | +// |
| 41 | +// Precedence is explicit config, then CLOUDFLARE_USER_AGENT_OPERATOR_SUFFIX, then pulumi/<version>. |
| 42 | +// An explicitly empty value in either removes the key entirely, which opts out by restoring |
| 43 | +// upstream's `terraform/<version>` default. |
| 44 | +// |
| 45 | +// Note that the bridge skips PreConfigureCallback altogether when the provider config contains |
| 46 | +// unknowns, in which case no suffix is injected and upstream falls back to `terraform/<version>`. |
| 47 | +func setUserAgentOperatorSuffix(vars resource.PropertyMap) { |
| 48 | + if v, ok := vars[userAgentOperatorSuffixKey]; ok { |
| 49 | + if v.IsString() && v.StringValue() == "" { |
| 50 | + delete(vars, userAgentOperatorSuffixKey) |
| 51 | + } |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + if v, ok := os.LookupEnv(userAgentOperatorSuffixEnvVar); ok { |
| 56 | + if v == "" { |
| 57 | + return |
| 58 | + } |
| 59 | + vars[userAgentOperatorSuffixKey] = resource.NewStringProperty(v) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + vars[userAgentOperatorSuffixKey] = resource.NewStringProperty(pulumiUserAgentOperatorSuffix()) |
| 64 | +} |
0 commit comments