Debian Codegraph @

testhelpers/client.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package testhelpers
2
3 import (
4 "context"
5 "fmt"
6 "os"
7 "testing"
8 "time"
9
10 "github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
11 "github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage"
12 "github.com/Azure/go-autorest/autorest"
13 "github.com/Azure/go-autorest/autorest/azure"
14 "github.com/hashicorp/go-azure-helpers/authentication"
15 )
16
17 type Client struct {
18 ResourceGroupsClient resources.GroupsClient
19 StorageClient storage.AccountsClient
20
21 auth *autorest.Authorizer
22 Environment azure.Environment
23 }
24
25 func toPointeredString(input string) *string {
26 return &input
27 }
28
29 type TestResources struct {
30 ResourceGroup string
31 StorageAccountName string
32 StorageAccountKey string
33 }
34
35 func (client Client) BuildTestResources(ctx context.Context, resourceGroup, name string, kind storage.Kind) (*TestResources, error) {
36 return client.buildTestResources(ctx, resourceGroup, name, kind, false, "")
37 }
38 func (client Client) BuildTestResourcesWithHns(ctx context.Context, resourceGroup, name string, kind storage.Kind) (*TestResources, error) {
39 return client.buildTestResources(ctx, resourceGroup, name, kind, true, "")
40 }
41 func (client Client) BuildTestResourcesWithSku(ctx context.Context, resourceGroup, name string, kind storage.Kind, sku storage.SkuName) (*TestResources, error) {
42 return client.buildTestResources(ctx, resourceGroup, name, kind, false, sku)
43 }
44 func (client Client) buildTestResources(ctx context.Context, resourceGroup, name string, kind storage.Kind, enableHns bool, sku storage.SkuName) (*TestResources, error) {
45 location := toPointeredString(os.Getenv("ARM_TEST_LOCATION"))
46 _, err := client.ResourceGroupsClient.CreateOrUpdate(ctx, resourceGroup, resources.Group{
47 Location: location,
48 })
49 if err != nil {
50 return nil, fmt.Errorf("Error creating Resource Group %q: %s", resourceGroup, err)
51 }
52
53 props := storage.AccountPropertiesCreateParameters{}
•54 if kind == storage.BlobStorage {
55 props.AccessTier = storage.Hot
56 }
57 if enableHns {
58 props.IsHnsEnabled = &enableHns
59 }
60 if sku == "" {
•61 sku = storage.StandardLRS
62 }
63
64 future, err := client.StorageClient.Create(ctx, resourceGroup, name, storage.AccountCreateParameters{
65 Location: location,
66 Sku: &storage.Sku{
67 Name: sku,
68 },
69 Kind: kind,
70 AccountPropertiesCreateParameters: &props,
71 })
72
73 if err != nil {
74 return nil, fmt.Errorf("Error creating Account %q (Resource Group %q): %s", name, resourceGroup, err)
75 }
76
77 err = future.WaitForCompletionRef(ctx, client.StorageClient.Client)
78 if err != nil {
79 return nil, fmt.Errorf("Error waiting for the creation of Account %q (Resource Group %q): %s", name, resourceGroup, err)
80 }
81
82 keys, err := client.StorageClient.ListKeys(ctx, resourceGroup, name, "")
83 if err != nil {
84 return nil, fmt.Errorf("Error listing keys for Storage Account %q (Resource Group %q): %s", name, resourceGroup, err)
85 }
86
87 // sure we could poll to get around the inconsistency, but where's the fun in that
88 time.Sleep(5 * time.Second)
89
90 accountKeys := *keys.Keys
91 return &TestResources{
92 ResourceGroup: resourceGroup,
93 StorageAccountName: name,
94 StorageAccountKey: *(accountKeys[0]).Value,
95 }, nil
96 }
97
98 func (client Client) DestroyTestResources(ctx context.Context, resourceGroup, name string) error {
99 _, err := client.StorageClient.Delete(ctx, resourceGroup, name)
100 if err != nil {
101 return fmt.Errorf("Error deleting Account %q (Resource Group %q): %s", name, resourceGroup, err)
102 }
103
•104 future, err := client.ResourceGroupsClient.Delete(ctx, resourceGroup, "")
105 if err != nil {
106 return fmt.Errorf("Error deleting Resource Group %q: %s", resourceGroup, err)
107 }
108
109 err = future.WaitForCompletionRef(ctx, client.ResourceGroupsClient.Client)
110 if err != nil {
111 return fmt.Errorf("Error waiting for deletion of Resource Group %q: %s", resourceGroup, err)
112 }
113
114 return nil
115 }
116
117 func Build(t *testing.T) (*Client, error) {
118 if os.Getenv("ACCTEST") == "" {
119 t.Skip("Skipping as `ACCTEST` hasn't been set")
120 }
121
122 authClient, env, err := buildAuthClient()
123 if err != nil {
124 return nil, fmt.Errorf("Error building Auth Client: %s", err)
125 }
126
127 if env == nil {
128 return nil, fmt.Errorf("Environment was nil: %s", err)
129 }
130
131 apiClient, err := buildAPIClient(authClient, *env)
132 if err != nil {
133 return nil, fmt.Errorf("Error building API Client: %s", err)
134 }
135
136 return apiClient, nil
137 }
138
139 func buildAPIClient(config *authentication.Config, env azure.Environment) (*Client, error) {
140 oauthConfig, err := config.BuildOAuthConfig(env.ActiveDirectoryEndpoint)
141 if err != nil {
142 return nil, err
143 }
144
145 // OAuthConfigForTenant returns a pointer, which can be nil.
146 if oauthConfig == nil {
147 return nil, fmt.Errorf("Unable to configure OAuthConfig for tenant %s", config.TenantID)
148 }
149
150 sender := buildSender()
151 armAuth, err := config.GetAuthorizationToken(sender, oauthConfig, env.ResourceManagerEndpoint)
152 if err != nil {
153 return nil, err
154 }
155
156 storageAuth, err := config.GetAuthorizationToken(sender, oauthConfig, "https://storage.azure.com/")
157 if err != nil {
158 return nil, err
159 }
160
161 client := Client{
162 Environment: env,
163 auth: &storageAuth,
164 }
165
166 resourceGroupsClient := resources.NewGroupsClientWithBaseURI(env.ResourceManagerEndpoint, config.SubscriptionID)
167 resourceGroupsClient.Client = client.PrepareWithStorageResourceManagerAuth(resourceGroupsClient.Client)
168 resourceGroupsClient.Authorizer = armAuth
169 client.ResourceGroupsClient = resourceGroupsClient
170
171 storageClient := storage.NewAccountsClientWithBaseURI(env.ResourceManagerEndpoint, config.SubscriptionID)
172 storageClient.Client = client.PrepareWithStorageResourceManagerAuth(storageClient.Client)
173 storageClient.Authorizer = armAuth
174 client.StorageClient = storageClient
175
176 return &client, nil
177 }
178
179 func (client Client) PrepareWithStorageResourceManagerAuth(input autorest.Client) autorest.Client {
180 return client.PrepareWithAuthorizer(input, *client.auth)
181 }
182
183 func (client Client) PrepareWithAuthorizer(input autorest.Client, authorizer autorest.Authorizer) autorest.Client {
184 input.Authorizer = authorizer
185 input.Sender = buildSender()
186 input.SkipResourceProviderRegistration = true
187 return input
188 }
189
190 func buildAuthClient() (*authentication.Config, *azure.Environment, error) {
191 builder := &authentication.Builder{
192 SubscriptionID: os.Getenv("ARM_SUBSCRIPTION_ID"),
193 ClientID: os.Getenv("ARM_CLIENT_ID"),
194 ClientSecret: os.Getenv("ARM_CLIENT_SECRET"),
195 TenantID: os.Getenv("ARM_TENANT_ID"),
196 Environment: os.Getenv("ARM_ENVIRONMENT"),
197
198 // Feature Toggles
199 SupportsClientSecretAuth: true,
200 }
201
202 c, err := builder.Build()
203 if err != nil {
204 return nil, nil, fmt.Errorf("Error building AzureRM Client: %s", err)
205 }
206
207 env, err := authentication.DetermineEnvironment(c.Environment)
208 if err != nil {
209 return nil, nil, err
210 }
211
212 return c, env, nil
213 }