/
client_options.go
124 lines (101 loc) · 4.1 KB
/
client_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package common
import (
"fmt"
"os"
"strings"
"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-helpers/sender"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/version"
)
type Authorizers struct {
BatchManagement auth.Authorizer
KeyVault auth.Authorizer
ManagedHSM auth.Authorizer
ResourceManager auth.Authorizer
Storage auth.Authorizer
Synapse auth.Authorizer
// Some data-plane APIs require a token scoped for a specific endpoint
AuthorizerFunc ApiAuthorizerFunc
}
type ApiAuthorizerFunc func(api environments.Api) (auth.Authorizer, error)
type ClientOptions struct {
Authorizers *Authorizers
AuthConfig *auth.Credentials
Environment environments.Environment
Features features.UserFeatures
SubscriptionId string
TenantId string
PartnerId string
TerraformVersion string
CustomCorrelationRequestID string
DisableCorrelationRequestID bool
DisableTerraformPartnerID bool
SkipProviderReg bool
StorageUseAzureAD bool
ResourceManagerEndpoint string
// Legacy authorizers for go-autorest
BatchManagementAuthorizer autorest.Authorizer
KeyVaultAuthorizer autorest.Authorizer
ManagedHSMAuthorizer autorest.Authorizer
ResourceManagerAuthorizer autorest.Authorizer
SynapseAuthorizer autorest.Authorizer
}
// Configure set up a resourcemanager.Client using an auth.Authorizer from hashicorp/go-azure-sdk
func (o ClientOptions) Configure(c client.BaseClient, authorizer auth.Authorizer) {
c.SetAuthorizer(authorizer)
c.SetUserAgent(userAgent(c.GetUserAgent(), o.TerraformVersion, o.PartnerId, o.DisableTerraformPartnerID))
if !o.DisableCorrelationRequestID {
id := o.CustomCorrelationRequestID
if id == "" {
id = correlationRequestID()
}
c.AppendRequestMiddleware(correlationRequestIDMiddleware(id))
}
c.AppendRequestMiddleware(requestLoggerMiddleware("AzureRM"))
c.AppendResponseMiddleware(responseLoggerMiddleware("AzureRM"))
}
// ConfigureClient sets up an autorest.Client using an autorest.Authorizer
func (o ClientOptions) ConfigureClient(c *autorest.Client, authorizer autorest.Authorizer) {
c.UserAgent = userAgent(c.UserAgent, o.TerraformVersion, o.PartnerId, o.DisableTerraformPartnerID)
c.Authorizer = authorizer
c.Sender = sender.BuildSender("AzureRM")
c.SkipResourceProviderRegistration = o.SkipProviderReg
if !o.DisableCorrelationRequestID {
id := o.CustomCorrelationRequestID
if id == "" {
id = correlationRequestID()
}
c.RequestInspector = withCorrelationRequestID(id)
}
}
func userAgent(userAgent, tfVersion, partnerID string, disableTerraformPartnerID bool) string {
tfUserAgent := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io) Terraform Plugin SDK/%s", tfVersion, meta.SDKVersionString())
providerUserAgent := fmt.Sprintf("%s terraform-provider-azurerm/%s", tfUserAgent, version.ProviderVersion)
if features.FourPointOhBeta() {
providerUserAgent = fmt.Sprintf("%s terraform-provider-azurerm/%s+4.0-beta", tfUserAgent, version.ProviderVersion)
}
userAgent = strings.TrimSpace(fmt.Sprintf("%s %s", userAgent, providerUserAgent))
// append the CloudShell version to the user agent if it exists
if azureAgent := os.Getenv("AZURE_HTTP_USER_AGENT"); azureAgent != "" {
userAgent = fmt.Sprintf("%s %s", userAgent, azureAgent)
}
// only one pid can be interpreted currently
// hence, send partner ID if present, otherwise send Terraform GUID
// unless users have opted out
if partnerID == "" && !disableTerraformPartnerID {
// Microsoft’s Terraform Partner ID is this specific GUID
partnerID = "222c6c49-1b0a-5959-a213-6608f9eb8820"
}
if partnerID != "" {
// Tolerate partnerID UUIDs without the "pid-" prefix
userAgent = fmt.Sprintf("%s pid-%s", userAgent, strings.TrimPrefix(partnerID, "pid-"))
}
return userAgent
}