-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (81 loc) · 2.55 KB
/
main.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
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2019-09-18 18:26:03.958204026 -0500 CDT m=+0.000175229
package azure_resources
import (
"context"
"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
)
var authorizer autorest.Authorizer
type Resource interface {
GetProperties() ([]byte, error)
}
func GetAllByGroupName(subscriptionID, groupName string) ([]Resource, error) {
client := resources.NewClient(subscriptionID)
client.Authorizer = authorizer
results, err := client.ListByResourceGroup(context.Background(), groupName, "", "", nil)
if err != nil {
return nil, err
}
var resources []Resource
for _, resource := range results.Values() {
switch *resource.Type {
case "Microsoft.Compute/virtualMachines":
r := &ComputeVirtualMachines{
subscriptionID: subscriptionID,
groupName: groupName,
resourceName: resource.Name,
authorizer: authorizer,
}
resources = append(resources, r)
case "Microsoft.Compute/virtualMachines/Extensions":
r := &ComputeVirtualMachineExtensions{
subscriptionID: subscriptionID,
groupName: groupName,
resourceName: resource.Name,
authorizer: authorizer,
}
resources = append(resources, r)
case "Microsoft.Network/publicIPAddresses":
r := &NetworkPublicIPAddresses{
subscriptionID: subscriptionID,
groupName: groupName,
resourceName: resource.Name,
authorizer: authorizer,
}
resources = append(resources, r)
case "Microsoft.Compute/disks":
r := &ComputeDisks{
subscriptionID: subscriptionID,
groupName: groupName,
resourceName: resource.Name,
authorizer: authorizer,
}
resources = append(resources, r)
case "Microsoft.Network/virtualNetworks":
r := &NetworkVirtualNetworks{
subscriptionID: subscriptionID,
groupName: groupName,
resourceName: resource.Name,
authorizer: authorizer,
}
resources = append(resources, r)
}
}
return resources, nil
}
func SetAuthorizer(tenantID, clientID, clientSecret string) error {
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return err
}
spToken, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
return err
}
authorizer = autorest.NewBearerAuthorizer(spToken)
return nil
}