/
builder.go
122 lines (102 loc) · 4.03 KB
/
builder.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
package clients
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-azure-helpers/authentication"
"github.com/hashicorp/go-azure-helpers/sender"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
)
type ClientBuilder struct {
AuthConfig *authentication.Config
DisableCorrelationRequestID bool
DisableTerraformPartnerID bool
PartnerId string
SkipProviderRegistration bool
StorageUseAzureAD bool
TerraformVersion string
Features features.UserFeatures
}
const azureStackEnvironmentError = `
The AzureRM Provider supports the different Azure Public Clouds - including China, Germany,
Public and US Government - however it does not support Azure Stack due to differences in
API and feature availability.
Terraform instead offers a separate "azurestack" provider which supports the functionality
and API's available in Azure Stack via Azure Stack Profiles.
`
func Build(ctx context.Context, builder ClientBuilder) (*Client, error) {
// point folks towards the separate Azure Stack Provider when using Azure Stack
if strings.EqualFold(builder.AuthConfig.Environment, "AZURESTACKCLOUD") {
return nil, fmt.Errorf(azureStackEnvironmentError)
}
env, err := authentication.DetermineEnvironment(builder.AuthConfig.Environment)
if err != nil {
return nil, err
}
if features.EnhancedValidationEnabled() {
// e.g. https://management.azure.com/ but we need management.azure.com
endpoint := strings.TrimPrefix(env.ResourceManagerEndpoint, "https://")
endpoint = strings.TrimSuffix(endpoint, "/")
location.CacheSupportedLocations(ctx, endpoint)
}
// client declarations:
account, err := NewResourceManagerAccount(ctx, *builder.AuthConfig, *env)
if err != nil {
return nil, fmt.Errorf("Error building account: %+v", err)
}
client := Client{
Account: account,
}
oauthConfig, err := builder.AuthConfig.BuildOAuthConfig(env.ActiveDirectoryEndpoint)
if err != nil {
return nil, err
}
// OAuthConfigForTenant returns a pointer, which can be nil.
if oauthConfig == nil {
return nil, fmt.Errorf("Unable to configure OAuthConfig for tenant %s", builder.AuthConfig.TenantID)
}
sender := sender.BuildSender("AzureRM")
// Resource Manager endpoints
endpoint := env.ResourceManagerEndpoint
auth, err := builder.AuthConfig.GetAuthorizationToken(sender, oauthConfig, env.TokenAudience)
if err != nil {
return nil, err
}
// Graph Endpoints
graphEndpoint := env.GraphEndpoint
graphAuth, err := builder.AuthConfig.GetAuthorizationToken(sender, oauthConfig, graphEndpoint)
if err != nil {
return nil, err
}
// Storage Endpoints
storageAuth, err := builder.AuthConfig.GetAuthorizationToken(sender, oauthConfig, env.ResourceIdentifiers.Storage)
if err != nil {
return nil, err
}
// Key Vault Endpoints
keyVaultAuth := builder.AuthConfig.BearerAuthorizerCallback(sender, oauthConfig)
o := &common.ClientOptions{
SubscriptionId: builder.AuthConfig.SubscriptionID,
TenantID: builder.AuthConfig.TenantID,
PartnerId: builder.PartnerId,
TerraformVersion: builder.TerraformVersion,
GraphAuthorizer: graphAuth,
GraphEndpoint: graphEndpoint,
KeyVaultAuthorizer: keyVaultAuth,
ResourceManagerAuthorizer: auth,
ResourceManagerEndpoint: endpoint,
StorageAuthorizer: storageAuth,
SkipProviderReg: builder.SkipProviderRegistration,
DisableCorrelationRequestID: builder.DisableCorrelationRequestID,
DisableTerraformPartnerID: builder.DisableTerraformPartnerID,
Environment: *env,
Features: builder.Features,
StorageUseAzureAD: builder.StorageUseAzureAD,
}
if err := client.Build(ctx, o); err != nil {
return nil, fmt.Errorf("Error building Client: %+v", err)
}
return &client, nil
}