Skip to content

Commit 004d050

Browse files
xuzhang3Nicholas M. Iodice
andauthored
Add UserAgent for HTTP headers (#305)
* Update azure-pipeline.yml for Azure Pipelines * sync with upstream * Add userAgent for http headers Co-authored-by: Nicholas M. Iodice <niiodice@microsoft.com>
1 parent e3d32f7 commit 004d050

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

azuredevops/provider.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ func Provider() *schema.Provider {
5555

5656
func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
5757
return func(d *schema.ResourceData) (interface{}, error) {
58-
client, err := config.GetAzdoClient(d.Get("personal_access_token").(string), d.Get("org_service_url").(string))
58+
terraformVersion := p.TerraformVersion
59+
if terraformVersion == "" {
60+
// Terraform 0.12 introduced this field to the protocol
61+
// We can therefore assume that if it's missing it's 0.10 or 0.11
62+
terraformVersion = "0.11+compatible"
63+
}
64+
65+
client, err := config.GetAzdoClient(d.Get("personal_access_token").(string), d.Get("org_service_url").(string), terraformVersion)
66+
5967
return client, err
6068
}
6169
}

azuredevops/utils/config/config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"os"
78
"strings"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/httpclient"
911
"github.com/microsoft/azure-devops-go-api/azuredevops"
1012
"github.com/microsoft/azure-devops-go-api/azuredevops/build"
1113
"github.com/microsoft/azure-devops-go-api/azuredevops/core"
@@ -15,6 +17,7 @@ import (
1517
"github.com/microsoft/azure-devops-go-api/azuredevops/operations"
1618
"github.com/microsoft/azure-devops-go-api/azuredevops/serviceendpoint"
1719
"github.com/microsoft/azure-devops-go-api/azuredevops/taskagent"
20+
"github.com/microsoft/terraform-provider-azuredevops/version"
1821
)
1922

2023
// AggregatedClient aggregates all of the underlying clients into a single data
@@ -37,7 +40,7 @@ type AggregatedClient struct {
3740
}
3841

3942
// GetAzdoClient builds and provides a connection to the Azure DevOps API
40-
func GetAzdoClient(azdoPAT string, organizationURL string) (*AggregatedClient, error) {
43+
func GetAzdoClient(azdoPAT string, organizationURL string, tfVersion string) (*AggregatedClient, error) {
4144
ctx := context.Background()
4245

4346
if strings.EqualFold(azdoPAT, "") {
@@ -49,6 +52,7 @@ func GetAzdoClient(azdoPAT string, organizationURL string) (*AggregatedClient, e
4952
}
5053

5154
connection := azuredevops.NewPatConnection(organizationURL, azdoPAT)
55+
setUserAgent(connection, tfVersion)
5256

5357
// client for these APIs (includes CRUD for AzDO projects...):
5458
// https://docs.microsoft.com/en-us/rest/api/azure/devops/core/?view=azure-devops-rest-5.1
@@ -121,3 +125,17 @@ func GetAzdoClient(azdoPAT string, organizationURL string) (*AggregatedClient, e
121125
log.Printf("getAzdoClient(): Created core, build, operations, and serviceendpoint clients successfully!")
122126
return aggregatedClient, nil
123127
}
128+
129+
// setUserAgent set UserAgent for http headers
130+
func setUserAgent(connection *azuredevops.Connection, tfVersion string) {
131+
tfUserAgent := httpclient.TerraformUserAgent(tfVersion)
132+
providerUserAgent := fmt.Sprintf("%s terraform-provider-azuredevops/%s", tfUserAgent, version.ProviderVersion)
133+
connection.UserAgent = strings.TrimSpace(fmt.Sprintf("%s %s", connection.UserAgent, providerUserAgent))
134+
135+
// append the CloudShell version to the user agent if it exists
136+
if azureAgent := os.Getenv("AZURE_HTTP_USER_AGENT"); azureAgent != "" {
137+
connection.UserAgent = fmt.Sprintf("%s %s", connection.UserAgent, azureAgent)
138+
}
139+
140+
log.Printf("[DEBUG] AzureRM Client User Agent: %s\n", connection.UserAgent)
141+
}

version/version.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package version
2+
3+
var (
4+
// ProviderVersion is set during the release process to the release version of the binary
5+
ProviderVersion = "dev"
6+
)

0 commit comments

Comments
 (0)