/
group.go
124 lines (112 loc) · 4.05 KB
/
group.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
package resource
import (
"context"
"fmt"
"log"
"strings"
"sync"
"connectors/goazure/config"
"connectors/goazure/iam"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/go-autorest/autorest/to"
)
func getGroupsClient() resources.GroupsClient {
groupsClient := resources.NewGroupsClient(config.SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer()
if err != nil {
log.Fatalf("failed to initialize authorizer: %v\n", err)
}
groupsClient.Authorizer = a
groupsClient.AddToUserAgent(config.UserAgent())
return groupsClient
}
func getGroupsClientWithAuthFile() resources.GroupsClient {
groupsClient := resources.NewGroupsClient(config.SubscriptionID())
// requires env var AZURE_AUTH_LOCATION set to output of
// `az ad sp create-for-rbac --sdk-auth`
a, err := auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
log.Fatalf("failed to initialize authorizer: %v\n", err)
}
groupsClient.Authorizer = a
groupsClient.AddToUserAgent(config.UserAgent())
return groupsClient
}
// CreateGroup creates a new resource group named by env var
func CreateGroup(ctx context.Context, groupName string) (resources.Group, error) {
groupsClient := getGroupsClient()
log.Println(fmt.Sprintf("creating resource group '%s' on location: %v", groupName, config.Location()))
return groupsClient.CreateOrUpdate(
ctx,
groupName,
resources.Group{
Location: to.StringPtr(config.Location()),
})
}
// CreateGroupWithAuthFile creates a new resource group. The client authorizer
// is set up based on an auth file created using the Azure CLI.
func CreateGroupWithAuthFile(ctx context.Context, groupName string) (resources.Group, error) {
groupsClient := getGroupsClientWithAuthFile()
log.Println(fmt.Sprintf("creating resource group '%s' on location: %v", groupName, config.Location()))
return groupsClient.CreateOrUpdate(
ctx,
groupName,
resources.Group{
Location: to.StringPtr(config.Location()),
})
}
// DeleteGroup removes the resource group named by env var
func DeleteGroup(ctx context.Context, groupName string) (result resources.GroupsDeleteFuture, err error) {
groupsClient := getGroupsClient()
return groupsClient.Delete(ctx, groupName)
}
// ListGroups gets an interator that gets all resource groups in the subscription
func ListGroups(ctx context.Context) (resources.GroupListResultIterator, error) {
groupsClient := getGroupsClient()
return groupsClient.ListComplete(ctx, "", nil)
}
// GetGroup gets info on the resource group in use
func GetGroup(ctx context.Context) (resources.Group, error) {
groupsClient := getGroupsClient()
return groupsClient.Get(ctx, config.GroupName())
}
// DeleteAllGroupsWithPrefix deletes all rescource groups that start with a certain prefix
func DeleteAllGroupsWithPrefix(ctx context.Context, prefix string) (futures []resources.GroupsDeleteFuture, groups []string) {
if config.KeepResources() {
log.Println("keeping resource groups")
return
}
for list, err := ListGroups(ctx); list.NotDone(); err = list.NextWithContext(ctx) {
if err != nil {
log.Fatalf("got error: %s", err)
}
rgName := *list.Value().Name
if strings.HasPrefix(rgName, prefix) {
fmt.Printf("deleting group '%s'\n", rgName)
future, err := DeleteGroup(ctx, rgName)
if err != nil {
log.Fatalf("got error: %s", err)
}
futures = append(futures, future)
groups = append(groups, rgName)
}
}
return
}
// WaitForDeleteCompletion concurrently waits for delete group operations to finish
func WaitForDeleteCompletion(ctx context.Context, wg *sync.WaitGroup, futures []resources.GroupsDeleteFuture, groups []string) {
for i, f := range futures {
wg.Add(1)
go func(ctx context.Context, future resources.GroupsDeleteFuture, rg string) {
err := future.WaitForCompletionRef(ctx, getGroupsClient().Client)
if err != nil {
log.Fatalf("got error: %s", err)
} else {
fmt.Printf("finished deleting group '%s'\n", rg)
}
wg.Done()
}(ctx, f, groups[i])
}
}