-
Notifications
You must be signed in to change notification settings - Fork 3
/
peering.go
178 lines (142 loc) · 4.19 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
package chart
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/AndreZiviani/aws-fuzzy/internal/peering"
"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 ConfigResult struct {
Configuration ConfigConfiguration `json:"configuration"`
Tags []ConfigTags `json:"tags"`
}
type ConfigConfiguration struct {
RequesterVpc ConfigVpc `json:"requesterVpcInfo"`
AccepterVpc ConfigVpc `json:"accepterVpcInfo"`
PeeringId string `json:"vpcPeeringConnectionId"`
}
type ConfigVpc struct {
VpcId string `json:"vpcId"`
OwnerId string `json:"ownerId"`
}
type ConfigTags struct {
Value string `json:"value"`
Key string `json:"key"`
}
type PeeringConnection struct {
Requester string
RequesterVpc string
Accepter string
AccepterVpc 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()
peerings, err := peering.Peering(ctx, p.Profile, p.Account)
if err != nil {
return err
}
tmp := strings.Join(peerings[:], ",")
tmp = fmt.Sprintf("[%s]", tmp)
o := []ConfigResult{}
_ = json.Unmarshal([]byte(tmp), &o)
graph := NewGraph()
nodes, links, categories := mapResult(o)
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(result []ConfigResult) ([]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]string)
for _, peering := range result {
dnodes[peering.Configuration.RequesterVpc.VpcId] = peering.Configuration.RequesterVpc.OwnerId
dnodes[peering.Configuration.AccepterVpc.VpcId] = peering.Configuration.AccepterVpc.OwnerId
links = append(links, opts.GraphLink{
Source: peering.Configuration.RequesterVpc.VpcId,
Target: peering.Configuration.AccepterVpc.VpcId,
//Value: peering.Configuration.PeeringId,
})
}
for k, v := range dnodes {
categories = append(categories,
&opts.GraphCategory{
Name: v,
Label: &opts.Label{Show: true, Color: "auto"},
})
nodes = append(nodes,
opts.GraphNode{
Name: k,
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",
},
}),
)
}