Skip to content

Commit

Permalink
use Provider.UserAgent to generate the user agent header
Browse files Browse the repository at this point in the history
also updating goreleaser configuration to capture version and commit in a way that is easily accessible by the provider configure code
  • Loading branch information
jedelson-pagerduty committed Jul 28, 2022
1 parent e251ef8 commit 53dbfa4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
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}}'
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
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
}

0 comments on commit 53dbfa4

Please sign in to comment.