forked from kyma-project/cloud-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteTablesStore.go
127 lines (102 loc) · 3.55 KB
/
routeTablesStore.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
package mock
import (
"context"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/elliotchance/pie/v2"
"k8s.io/utils/ptr"
"sync"
)
type RouteTableConfig interface {
AddRouteTable(routeTableId, vpcId *string, tags []ec2types.Tag, associations []ec2types.RouteTableAssociation) ec2types.RouteTable
GetRoute(ctc context.Context, vpcId, routeTableId, vpcPeeringConnectionId, destinationCidrBlock string) *ec2types.Route
GetRouteCount(ctx context.Context, vpcId, vpcPeeringConnectionId, destinationCidrBlock string) int
}
type routeTableEntry struct {
routeTable ec2types.RouteTable
}
type routeTablesStore struct {
m sync.Mutex
items []*routeTableEntry
}
func (s *routeTablesStore) AddRouteTable(routeTableId, vpcId *string, tags []ec2types.Tag, associations []ec2types.RouteTableAssociation) ec2types.RouteTable {
s.m.Lock()
defer s.m.Unlock()
filtered := pie.Filter(s.items, func(entry *routeTableEntry) bool {
return *entry.routeTable.RouteTableId == *routeTableId
})
entry := pie.First(filtered)
if entry == nil {
entry = &routeTableEntry{routeTable: ec2types.RouteTable{
RouteTableId: routeTableId,
Routes: nil,
Tags: tags,
VpcId: vpcId,
Associations: associations,
}}
s.items = append(s.items, entry)
}
return entry.routeTable
}
func (s *routeTablesStore) DescribeRouteTables(ctc context.Context, vpcId string) ([]ec2types.RouteTable, error) {
s.m.Lock()
defer s.m.Unlock()
filtered := pie.Filter(s.items, func(e *routeTableEntry) bool {
return *e.routeTable.VpcId == vpcId
})
return pie.Map(filtered, func(e *routeTableEntry) ec2types.RouteTable { return e.routeTable }), nil
}
func (s *routeTablesStore) GetRoute(ctc context.Context, vpcId, routeTableId, vpcPeeringConnectionId, destinationCidrBlock string) *ec2types.Route {
s.m.Lock()
defer s.m.Unlock()
for _, e := range s.items {
if *e.routeTable.VpcId == vpcId && *e.routeTable.RouteTableId == routeTableId {
for _, r := range e.routeTable.Routes {
if *r.DestinationCidrBlock == destinationCidrBlock && *r.VpcPeeringConnectionId == vpcPeeringConnectionId {
return &r
}
}
}
}
return nil
}
func (s *routeTablesStore) CreateRoute(ctx context.Context, routeTableId, destinationCidrBlock, vpcPeeringConnectionId *string) error {
s.m.Lock()
defer s.m.Unlock()
filtered := pie.Filter(s.items, func(r *routeTableEntry) bool {
return *r.routeTable.RouteTableId == *routeTableId
})
entry := pie.First(filtered)
entry.routeTable.Routes = append(entry.routeTable.Routes, ec2types.Route{
DestinationCidrBlock: destinationCidrBlock,
VpcPeeringConnectionId: vpcPeeringConnectionId,
})
return nil
}
func (s *routeTablesStore) DeleteRoute(ctx context.Context, routeTableId, destinationCidrBlock *string) error {
s.m.Lock()
defer s.m.Unlock()
filtered := pie.Filter(s.items, func(r *routeTableEntry) bool {
return *r.routeTable.RouteTableId == *routeTableId
})
entry := pie.First(filtered)
entry.routeTable.Routes = pie.Filter(entry.routeTable.Routes, func(r ec2types.Route) bool {
return !ptr.Equal(r.DestinationCidrBlock, destinationCidrBlock)
})
return nil
}
func (s *routeTablesStore) GetRouteCount(ctx context.Context, vpcId, vpcPeeringConnectionId, destinationCidrBlock string) int {
tables, err := s.DescribeRouteTables(ctx, vpcId)
if err != nil {
return -1
}
cnt := 0
for _, t := range tables {
for _, r := range t.Routes {
if ptr.Deref(r.VpcPeeringConnectionId, "") == vpcPeeringConnectionId &&
ptr.Deref(r.DestinationCidrBlock, "") == destinationCidrBlock {
cnt++
}
}
}
return cnt
}