From 53dbfa4d673791da982f3ce736a8da36ae17849b Mon Sep 17 00:00:00 2001 From: Justin Edelson Date: Thu, 28 Jul 2022 10:54:18 -0400 Subject: [PATCH] use Provider.UserAgent to generate the user agent header also updating goreleaser configuration to capture version and commit in a way that is easily accessible by the provider configure code --- .goreleaser.yml | 2 +- pagerduty/provider.go | 54 +++++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 8097aab79..d2fac0931 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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}}' tags: - timetzdata goos: diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 12737ca7e..63d5b759a 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -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{ @@ -97,13 +101,26 @@ func Provider() *schema.Provider { } p.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) { - terraformVersion := p.TerraformVersion - 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 @@ -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 -}