-
Notifications
You must be signed in to change notification settings - Fork 3
/
peering.go
256 lines (206 loc) · 6.39 KB
/
peering.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package chart
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/AndreZiviani/aws-fuzzy/internal/peering"
"github.com/AndreZiviani/aws-fuzzy/internal/sso"
"github.com/AndreZiviani/aws-fuzzy/internal/tracing"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
opentracing "github.com/opentracing/opentracing-go"
)
type VPCResult struct {
VpcId string `json:"resourceId"`
Tags []ConfigTags `json:"tags"`
OwnerId string `json:"configuration.ownerId"`
}
type PeeringResult struct {
Configuration PeeringConfiguration `json:"configuration"`
Tags []ConfigTags `json:"tags"`
}
type PeeringConfiguration struct {
RequesterVpc ConfigVpc `json:"requesterVpcInfo"`
AccepterVpc ConfigVpc `json:"accepterVpcInfo"`
PeeringId string `json:"vpcPeeringConnectionId"`
Status string `json:"status"`
}
type PeeringStatus struct {
Code string `json:"code"`
Message string `json:"message"`
}
type ConfigVpc struct {
VpcId string `json:"vpcId"`
OwnerId string `json:"ownerId"`
Region string `json:"region"`
}
type ConfigTags struct {
Value string `json:"value"`
Key string `json:"key"`
}
type PeeringConnection struct {
Requester string
RequesterVpc string
Accepter string
AccepterVpc string
}
type Node struct {
Id string
Name string
Account string
}
func (p *Peering) Execute(args []string) error {
ctx := context.Background()
closer, err := tracing.InitTracing()
if err != nil {
fmt.Printf("failed to initialize tracing, %s\n", err)
}
defer closer.Close()
tracer := opentracing.GlobalTracer()
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, tracer, "chart")
defer span.Finish()
peeringsJson, vpcsJson, err := peering.Peering(ctx, p.Profile, p.Account, p.Region)
if err != nil {
return err
}
tmp := strings.Join(peeringsJson[:], ",")
tmp = fmt.Sprintf("[%s]", tmp)
peerings := []PeeringResult{}
_ = json.Unmarshal([]byte(tmp), &peerings)
tmp = strings.Join(vpcsJson[:], ",")
tmp = fmt.Sprintf("[%s]", tmp)
vpcs := []VPCResult{}
_ = json.Unmarshal([]byte(tmp), &vpcs)
graph := NewGraph()
nodes, links, categories := mapResult(peerings, vpcs)
AddToGraph(graph, nodes, links, categories)
page := components.NewPage()
page.AddCharts(graph)
f, err := os.Create("graph.html")
if err != nil {
panic(err)
}
page.Render(io.MultiWriter(f))
return nil
}
func NewGraph() *charts.Graph {
graph := charts.NewGraph()
graph.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Width: "100%", Height: "95vh",
AssetsHost: "https://cdn.jsdelivr.net/npm/echarts@4/dist/", //use updated upstream js
}),
charts.WithTitleOpts(opts.Title{Title: "Peering Connections"}),
charts.WithToolboxOpts(opts.Toolbox{
Show: true,
Feature: &opts.ToolBoxFeature{
SaveAsImage: &opts.ToolBoxFeatureSaveAsImage{
Show: true,
Type: "png",
Title: "Download as PNG",
},
},
}),
charts.WithLegendOpts(opts.Legend{Show: true}),
charts.WithTooltipOpts(opts.Tooltip{Show: true}),
)
return graph
}
func mapResult(peerings []PeeringResult, vpcs []VPCResult) ([]opts.GraphNode, []opts.GraphLink, []*opts.GraphCategory) {
categories := make([]*opts.GraphCategory, 0)
categories = append(categories, &opts.GraphCategory{}) // workaround bug
links := make([]opts.GraphLink, 0)
nodes := make([]opts.GraphNode, 0)
dnodes := make(map[string]Node)
login := sso.Login{}
login.LoadProfiles()
var requesterAccountName, accepterAccountName string
var peeringStatus PeeringStatus
for _, peering := range peerings {
_ = json.Unmarshal([]byte(peering.Configuration.Status), &peeringStatus)
if peeringStatus.Code != "active" {
continue
}
requesterAccount, err := login.GetProfileFromID(peering.Configuration.RequesterVpc.OwnerId)
if err == nil {
requesterAccountName = fmt.Sprintf("%s\n(%s)", requesterAccount.Name, peering.Configuration.RequesterVpc.Region)
} else {
requesterAccountName = fmt.Sprintf("%s\n(%s)", peering.Configuration.RequesterVpc.OwnerId, peering.Configuration.RequesterVpc.Region)
}
accepterAccount, err := login.GetProfileFromID(peering.Configuration.AccepterVpc.OwnerId)
if err == nil {
accepterAccountName = fmt.Sprintf("%s\n(%s)", accepterAccount.Name, peering.Configuration.AccepterVpc.Region)
} else {
accepterAccountName = fmt.Sprintf("%s\n(%s)", peering.Configuration.AccepterVpc.OwnerId, peering.Configuration.AccepterVpc.Region)
}
requesterVpcId := peering.Configuration.RequesterVpc.VpcId
requesterVpcName := getVpcName(vpcs, requesterVpcId)
accepterVpcId := peering.Configuration.AccepterVpc.VpcId
accepterVpcName := getVpcName(vpcs, accepterVpcId)
dnodes[requesterVpcId] = Node{Id: requesterVpcId, Account: requesterAccountName, Name: requesterVpcName}
dnodes[accepterVpcId] = Node{Id: accepterVpcId, Account: accepterAccountName, Name: accepterVpcName}
links = append(links, opts.GraphLink{
Source: requesterVpcName,
Target: accepterVpcName,
})
}
for _, v := range dnodes {
categories = append(categories,
&opts.GraphCategory{
Name: v.Account,
Label: &opts.Label{Show: true, Color: "auto"},
})
nodes = append(nodes,
opts.GraphNode{
Name: v.Name,
Category: len(categories) - 1,
},
)
}
return nodes, links, categories
}
func AddToGraph(graph *charts.Graph, nodes []opts.GraphNode, links []opts.GraphLink, categories []*opts.GraphCategory) {
graph.AddSeries("graph", nodes, links).
SetSeriesOptions(
charts.WithGraphChartOpts(
opts.GraphChart{
Force: &opts.GraphForce{Repulsion: 100},
Layout: "force",
Roam: true,
Categories: categories,
FocusNodeAdjacency: true,
}),
charts.WithLabelOpts(opts.Label{Show: true, Position: "right", Color: "Black"}),
charts.WithEmphasisOpts(opts.Emphasis{
Label: &opts.Label{
Show: true,
Color: "black",
Position: "right",
},
}),
)
}
func getVpcName(vpcs []VPCResult, key string) string {
for _, vpc := range vpcs {
if vpc.VpcId == key {
for _, tag := range vpc.Tags {
if tag.Key == "Name" {
return fmt.Sprintf("%s\n(%s)", tag.Value, key)
}
}
}
}
return key
}
func getTagName(tags []ConfigTags, key string) string {
for _, tag := range tags {
if tag.Key == "Name" {
return tag.Value
}
}
return key
}