-
Notifications
You must be signed in to change notification settings - Fork 183
/
account.go
175 lines (151 loc) · 6.1 KB
/
account.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package storage
import (
"context"
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-06-01/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config"
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/iam"
)
var (
// globals used by tests
testAccountName string
testAccountGroupName string
)
func getStorageAccountsClient() storage.AccountsClient {
storageAccountsClient := storage.NewAccountsClient(config.SubscriptionID())
auth, _ := iam.GetResourceManagementAuthorizer()
storageAccountsClient.Authorizer = auth
storageAccountsClient.AddToUserAgent(config.UserAgent())
return storageAccountsClient
}
func getUsageClient() storage.UsageClient {
usageClient := storage.NewUsageClient(config.SubscriptionID())
auth, _ := iam.GetResourceManagementAuthorizer()
usageClient.Authorizer = auth
usageClient.AddToUserAgent(config.UserAgent())
return usageClient
}
func getAccountPrimaryKey(ctx context.Context, accountName, accountGroupName string) string {
response, err := GetAccountKeys(ctx, accountName, accountGroupName)
if err != nil {
log.Fatalf("failed to list keys: %v", err)
}
return *(((*response.Keys)[0]).Value)
}
// CreateStorageAccount starts creation of a new storage account and waits for
// the account to be created.
func CreateStorageAccount(ctx context.Context, accountName, accountGroupName string) (storage.Account, error) {
var s storage.Account
storageAccountsClient := getStorageAccountsClient()
result, err := storageAccountsClient.CheckNameAvailability(
ctx,
storage.AccountCheckNameAvailabilityParameters{
Name: to.StringPtr(accountName),
Type: to.StringPtr("Microsoft.Storage/storageAccounts"),
})
if err != nil {
return s, fmt.Errorf("storage account check-name-availability failed: %v\n", err)
}
if *result.NameAvailable != true {
return s, fmt.Errorf(
"storage account name [%s] not available: %v\nserver message: %v\n",
accountName, err, *result.Message)
}
future, err := storageAccountsClient.Create(
ctx,
accountGroupName,
accountName,
storage.AccountCreateParameters{
Sku: &storage.Sku{
Name: storage.StandardLRS},
Kind: storage.Storage,
Location: to.StringPtr(config.DefaultLocation()),
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{},
})
if err != nil {
return s, fmt.Errorf("failed to start creating storage account: %v\n", err)
}
err = future.WaitForCompletionRef(ctx, storageAccountsClient.Client)
if err != nil {
return s, fmt.Errorf("failed to finish creating storage account: %v\n", err)
}
return future.Result(storageAccountsClient)
}
// GetStorageAccount gets details on the specified storage account
func GetStorageAccount(ctx context.Context, accountName, accountGroupName string) (storage.Account, error) {
storageAccountsClient := getStorageAccountsClient()
return storageAccountsClient.GetProperties(ctx, accountGroupName, accountName)
}
// DeleteStorageAccount deletes an existing storate account
func DeleteStorageAccount(ctx context.Context, accountName, accountGroupName string) (autorest.Response, error) {
storageAccountsClient := getStorageAccountsClient()
return storageAccountsClient.Delete(ctx, accountGroupName, accountName)
}
// CheckAccountNameAvailability checks if the storage account name is available.
// Storage account names must be unique across Azure and meet other requirements.
func CheckAccountNameAvailability(ctx context.Context, accountName string) (storage.CheckNameAvailabilityResult, error) {
storageAccountsClient := getStorageAccountsClient()
result, err := storageAccountsClient.CheckNameAvailability(
ctx,
storage.AccountCheckNameAvailabilityParameters{
Name: to.StringPtr(accountName),
Type: to.StringPtr("Microsoft.Storage/storageAccounts"),
})
return result, err
}
// ListAccountsByResourceGroup lists storage accounts by resource group.
func ListAccountsByResourceGroup(ctx context.Context, groupName string) (storage.AccountListResult, error) {
storageAccountsClient := getStorageAccountsClient()
return storageAccountsClient.ListByResourceGroup(ctx, groupName)
}
// ListAccountsBySubscription lists storage accounts by subscription.
func ListAccountsBySubscription(ctx context.Context) (storage.AccountListResult, error) {
storageAccountsClient := getStorageAccountsClient()
return storageAccountsClient.List(ctx)
}
// GetAccountKeys gets the storage account keys
func GetAccountKeys(ctx context.Context, accountName, accountGroupName string) (storage.AccountListKeysResult, error) {
accountsClient := getStorageAccountsClient()
return accountsClient.ListKeys(ctx, accountGroupName, accountName)
}
// RegenerateAccountKey regenerates the selected storage account key. `key` can be 0 or 1.
func RegenerateAccountKey(ctx context.Context, accountName, accountGroupName string, key int) (storage.AccountListKeysResult, error) {
var list storage.AccountListKeysResult
oldKeys, err := GetAccountKeys(ctx, accountName, accountGroupName)
if err != nil {
return list, err
}
accountsClient := getStorageAccountsClient()
return accountsClient.RegenerateKey(
ctx,
accountGroupName,
accountName,
storage.AccountRegenerateKeyParameters{
KeyName: (*oldKeys.Keys)[key].KeyName,
})
}
// UpdateAccount updates a storage account by adding tags
func UpdateAccount(ctx context.Context, accountName, accountGroupName string) (storage.Account, error) {
accountsClient := getStorageAccountsClient()
return accountsClient.Update(
ctx,
accountGroupName,
accountName,
storage.AccountUpdateParameters{
Tags: map[string]*string{
"who rocks": to.StringPtr("golang"),
"where": to.StringPtr("on azure")},
})
}
// ListUsage gets the usage count and limits for the resources in the subscription
func ListUsage(ctx context.Context) (storage.UsageListResult, error) {
usageClient := getUsageClient()
return usageClient.List(ctx)
}