-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
132 lines (102 loc) · 2.85 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/pkg/errors"
)
// AzureSession is an object representing session for subscription
type AzureSession struct {
SubscriptionID string
Authorizer autorest.Authorizer
}
func readJSON(path string) (*map[string]interface{}, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "Can't open the file")
}
contents := make(map[string]interface{})
err = json.Unmarshal(data, &contents)
if err != nil {
err = errors.Wrap(err, "Can't unmarshal file")
}
return &contents, err
}
func newSessionFromFile() (*AzureSession, error) {
authorizer, err := auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
return nil, errors.Wrap(err, "Can't initialize authorizer")
}
authInfo, err := readJSON(os.Getenv("AZURE_AUTH_LOCATION"))
if err != nil {
return nil, errors.Wrap(err, "Can't get authinfo")
}
sess := AzureSession{
SubscriptionID: (*authInfo)["subscriptionId"].(string),
Authorizer: authorizer,
}
return &sess, nil
}
func getGroups(sess *AzureSession) ([]string, error) {
tab := make([]string, 0)
var err error
grClient := resources.NewGroupsClient(sess.SubscriptionID)
grClient.Authorizer = sess.Authorizer
for list, err := grClient.ListComplete(context.Background(), "", nil); list.NotDone(); err = list.Next() {
if err != nil {
return nil, errors.Wrap(err, "error traverising RG list")
}
rgName := *list.Value().Name
tab = append(tab, rgName)
}
return tab, err
}
func getVM(sess *AzureSession, rg string, wg *sync.WaitGroup) {
defer wg.Done()
vmClient := compute.NewVirtualMachinesClient(sess.SubscriptionID)
vmClient.Authorizer = sess.Authorizer
for vm, err := vmClient.ListComplete(context.Background(), rg); vm.NotDone(); err = vm.Next() {
if err != nil {
log.Print("got error while traverising RG list: ", err)
}
i := vm.Value()
tags := []string{}
for k, v := range i.Tags {
tags = append(tags, fmt.Sprintf("%s?%s", k, *v))
}
tagsS := strings.Join(tags, "%")
if len(i.Tags) > 0 {
fmt.Printf("%s,%s,%s,<%s>\n", rg, *i.Name, *i.ID, tagsS)
} else {
fmt.Printf("%s,%s,%s\n", rg, *i.Name, *i.ID)
}
}
}
func main() {
var wg sync.WaitGroup
sess, err := newSessionFromFile()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
groups, err := getGroups(sess)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
wg.Add(len(groups))
for _, group := range groups {
go getVM(sess, group, &wg)
}
wg.Wait()
}