forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
60 lines (51 loc) · 1.51 KB
/
client.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
package testclient
import (
"context"
"fmt"
"os"
"sync"
"github.com/hashicorp/go-azure-helpers/authentication"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/features"
)
var (
_client *clients.Client
clientLock = &sync.Mutex{}
)
func Build() (*clients.Client, error) {
clientLock.Lock()
defer clientLock.Unlock()
if _client == nil {
environment, exists := os.LookupEnv("ARM_ENVIRONMENT")
if !exists {
environment = "public"
}
builder := authentication.Builder{
SubscriptionID: os.Getenv("ARM_SUBSCRIPTION_ID"),
ClientID: os.Getenv("ARM_CLIENT_ID"),
TenantID: os.Getenv("ARM_TENANT_ID"),
ClientSecret: os.Getenv("ARM_CLIENT_SECRET"),
Environment: environment,
MetadataHost: os.Getenv("ARM_METADATA_HOST"),
// we intentionally only support Client Secret auth for tests (since those variables are used all over)
SupportsClientSecretAuth: true,
}
config, err := builder.Build()
if err != nil {
return nil, fmt.Errorf("building ARM Client: %+v", err)
}
clientBuilder := clients.ClientBuilder{
AuthConfig: config,
SkipProviderRegistration: true,
TerraformVersion: os.Getenv("TERRAFORM_CORE_VERSION"),
Features: features.Default(),
StorageUseAzureAD: false,
}
client, err := clients.Build(context.TODO(), clientBuilder)
if err != nil {
return nil, err
}
_client = client
}
return _client, nil
}