-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
128 lines (117 loc) · 3.5 KB
/
azure.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
package main
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/services/resourcegraph/mgmt/2019-04-01/resourcegraph"
"github.com/Azure/azure-sdk-for-go/services/resourcegraph/mgmt/2019-04-01/resourcegraph/resourcegraphapi"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
)
type ResourceGraphQueryRequestInput struct {
subscriptionID string
query string
facets []string
skipToken string
skip int32
}
// Client is an API Client for Azure
type Client struct {
SubscriptionID string
ResourceGraphClient resourcegraphapi.BaseClientAPI
}
// NewClient returns *Client with setting Authorizer
func NewClient(subscriptionID string) (*Client, error) {
a, err := auth.NewAuthorizerFromCLI()
if err != nil {
a, err = auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
return &Client{}, err
}
}
resourceGraphClient := resourcegraph.NewOperationsClient()
resourceGraphClient.Authorizer = a
return &Client{
SubscriptionID: subscriptionID,
ResourceGraphClient: resourceGraphClient,
}, nil
}
// FetchData is received by Resource Graph
type FetchData struct {
// Column header
Header []string
// Data
Data [][]string
}
func (data FetchData) OutputToFile(output *os.File) error {
w := csv.NewWriter(output)
if err := w.Write(data.Header); err != nil {
return err
}
w.Flush()
for _, elem := range data.Data {
w.Write(elem)
w.Flush()
}
return nil
}
func FetchResourceGraphData(c context.Context, client *Client, params ResourceGraphQueryRequestInput) (*FetchData, error) {
var fetchData FetchData
var facetRequest []resourcegraph.FacetRequest
for i := 0; i < len(params.facets); i++ {
facetRequest = append(
facetRequest,
resourcegraph.FacetRequest{
Expression: ¶ms.facets[i],
},
)
}
request := &resourcegraph.QueryRequest{
Subscriptions: &[]string{params.subscriptionID},
Query: ¶ms.query,
Options: &resourcegraph.QueryRequestOptions{ResultFormat: resourcegraph.ResultFormatTable, SkipToken: ¶ms.skipToken, Skip: ¶ms.skip},
Facets: &facetRequest,
}
queryResponse, err := client.ResourceGraphClient.Resources(c, *request)
if err != nil {
return nil, err
}
if queryResponse.SkipToken != nil {
params.skipToken = *queryResponse.SkipToken
params.skip += int32(*queryResponse.Count)
_data, err := FetchResourceGraphData(c, client, params)
if err != nil {
return nil, err
}
for _, elem := range _data.Data {
fetchData.Data = append(fetchData.Data, elem)
}
}
columns := queryResponse.Data.(map[string]interface{})["columns"]
rows := queryResponse.Data.(map[string]interface{})["rows"]
for _, column := range columns.([]interface{}) {
fetchData.Header = append(fetchData.Header, column.(map[string]interface{})["name"].(string))
}
// 取得したデータを1行ずつ処理
for _, row := range rows.([]interface{}) {
_result := []string{}
// 1行を1カラムずつ処理
for i, r := range row.([]interface{}) {
// カラムの型に応じて処理を変える
switch columns.([]interface{})[i].(map[string]interface{})["type"] {
case "integer", "string":
_result = append(_result, fmt.Sprint(r))
case "object": // object の場合は JSON 化
j, err := json.Marshal(r)
if err != nil {
return nil, err
}
_result = append(_result, string(j))
}
}
fetchData.Data = append(fetchData.Data, _result)
}
return &fetchData, nil
}