This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
/
groups.go
84 lines (69 loc) · 2.43 KB
/
groups.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
package azureutil
import (
"context"
"fmt"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/go-autorest/autorest/to"
)
// GetGroupsClient get new groups client
func GetGroupsClient(tenantID string, clientID string, clientSecret string, subscriptionID string) resources.GroupsClient {
// used to authenticate with azure
os.Setenv("AZURE_TENANT_ID", tenantID)
os.Setenv("AZURE_CLIENT_ID", clientID)
os.Setenv("AZURE_CLIENT_SECRET", clientSecret)
groupsClient := resources.NewGroupsClient(subscriptionID)
// create an authorizer from env vars or Azure Managed Service Idenity
authorizer, err := auth.NewAuthorizerFromEnvironment()
if err != nil {
log.Fatalf("failed to initialize authorizer: %v\n", err)
}
groupsClient.Authorizer = authorizer
return groupsClient
}
// TODO: add GetGroup function
// CheckForGroup verify group exists
func CheckForGroup(ctx context.Context, groupsClient resources.GroupsClient, resourceName string) bool {
// generate resource group name based on resource name
resourceGroupName := resourceName + "-group"
result, err := groupsClient.CheckExistence(ctx, resourceGroupName)
if err != nil {
log.Fatalf("error checking for group: %v", err)
}
if result.StatusCode == 404 {
return false
}
return true
}
// CreateGroup will create a new group with the provided name
func CreateGroup(ctx context.Context, groupsClient resources.GroupsClient, resourceName string, location string) (resources.Group, error) {
// generate resource group name based on resource name
resourceGroupName := resourceName + "-group"
log.Println(fmt.Sprintf("creating resource group '%s' on location: %v", resourceGroupName, location))
grp, err := groupsClient.CreateOrUpdate(
ctx,
resourceGroupName,
resources.Group{
Location: to.StringPtr(location),
},
)
if err != nil {
return grp, fmt.Errorf("cannot create resource group: %v", err)
}
return grp, nil
}
// DeleteGroup will delete an azure resource group
func DeleteGroup(ctx context.Context, groupsClient resources.GroupsClient, resourceName string) error {
// generate resource group name based on resource name
resourceGroupName := resourceName + "-group"
_, err := groupsClient.Delete(
ctx,
resourceGroupName,
)
if err != nil {
return fmt.Errorf("error while deleting resource group %s, error message: %v", resourceGroupName, err)
}
return nil
}