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

use Provider.UserAgent to generate the user agent header #552

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.version={{.Version}} -X github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.commit={{.Commit}}'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.version and main.commit could be removed here -- those variables don't exist so the current (before this change) configuration is kind of a no-op.

tags:
- timetzdata
goos:
Expand Down
54 changes: 24 additions & 30 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package pagerduty
import (
"fmt"
"log"
"runtime"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

var (
version = "dev"
commit = "none"
)

// Provider represents a resource provider in Terraform
func Provider() *schema.Provider {
p := &schema.Provider{
Expand Down Expand Up @@ -97,13 +101,26 @@ func Provider() *schema.Provider {
}

p.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
terraformVersion := p.TerraformVersion
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the only thing that was happening in ConfigureFunc was normalizing the Terraform version. That should not be necessary when using Provider.UserAgent so I just moved the body of providerConfigure here. I didn't see a reason to have a separate function.

if terraformVersion == "" {
// Terraform 0.12 introduced this field to the protocol
// We can therefore assume that if it's missing it's 0.10 or 0.11
terraformVersion = "0.11+compatible"
var ServiceRegion = strings.ToLower(d.Get("service_region").(string))

if ServiceRegion == "us" || ServiceRegion == "" {
ServiceRegion = ""
} else {
ServiceRegion = ServiceRegion + "."
}
return providerConfigure(d, terraformVersion)

config := Config{
ApiUrl: "https://api." + ServiceRegion + "pagerduty.com",
AppUrl: "https://app." + ServiceRegion + "pagerduty.com",
SkipCredsValidation: d.Get("skip_credentials_validation").(bool),
Token: d.Get("token").(string),
UserToken: d.Get("user_token").(string),
UserAgent: p.UserAgent("terraform-provider-pagerduty", version),
ApiUrlOverride: d.Get("api_url_override").(string),
}

log.Println("[INFO] Initializing PagerDuty client")
return &config, nil
}

return p
Expand All @@ -129,26 +146,3 @@ func handleNotFoundError(err error, d *schema.ResourceData) error {
}
return genError(err, d)
}

func providerConfigure(data *schema.ResourceData, terraformVersion string) (interface{}, error) {
var ServiceRegion = strings.ToLower(data.Get("service_region").(string))

if ServiceRegion == "us" || ServiceRegion == "" {
ServiceRegion = ""
} else {
ServiceRegion = ServiceRegion + "."
}

config := Config{
ApiUrl: "https://api." + ServiceRegion + "pagerduty.com",
AppUrl: "https://app." + ServiceRegion + "pagerduty.com",
SkipCredsValidation: data.Get("skip_credentials_validation").(bool),
Token: data.Get("token").(string),
UserToken: data.Get("user_token").(string),
UserAgent: fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, terraformVersion),
ApiUrlOverride: data.Get("api_url_override").(string),
}

log.Println("[INFO] Initializing PagerDuty client")
return &config, nil
}