-
Notifications
You must be signed in to change notification settings - Fork 499
/
vpn_gateways.go
146 lines (129 loc) · 4.02 KB
/
vpn_gateways.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
package compute
import (
"github.com/mitchellh/mapstructure"
"go.uber.org/zap"
"google.golang.org/api/compute/v1"
)
type VpnGateway struct {
_ interface{} `neo:"raw:MERGE (a:GCPProject {project_id: $project_id}) MERGE (a) - [:Resource] -> (n)"`
ID uint `gorm:"primarykey"`
ProjectID string `neo:"unique"`
CreationTimestamp string
Description string
ResourceID uint64 `neo:"unique"`
Kind string
LabelFingerprint string
Labels []*VpnGatewayLabel `gorm:"constraint:OnDelete:CASCADE;"`
Name string
Network string
Region string
SelfLink string
VpnInterfaces []*VpnGatewayVpnGatewayInterface `gorm:"constraint:OnDelete:CASCADE;"`
}
func (VpnGateway) TableName() string {
return "gcp_compute_vpn_gateways"
}
type VpnGatewayLabel struct {
ID uint `gorm:"primarykey"`
VpnGatewayID uint `neo:"ignore"`
ProjectID string `gorm:"-"`
Key string
Value string
}
func (VpnGatewayLabel) TableName() string {
return "gcp_compute_vpn_gateway_label"
}
type VpnGatewayVpnGatewayInterface struct {
ID uint `gorm:"primarykey"`
VpnGatewayID uint `neo:"ignore"`
ProjectID string `gorm:"-"`
ResourceID int64
IpAddress string
}
func (VpnGatewayVpnGatewayInterface) TableName() string {
return "gcp_compute_vpn_gateway_interfaces"
}
func (c *Client) transformVpnGatewayVpnGatewayLabels(values map[string]string) []*VpnGatewayLabel {
var tValues []*VpnGatewayLabel
for k, v := range values {
tValues = append(tValues, &VpnGatewayLabel{
ProjectID: c.projectID,
Key: k,
Value: v,
})
}
return tValues
}
func (c *Client) transformVpnGatewayVpnGatewayInterface(value *compute.VpnGatewayVpnGatewayInterface) *VpnGatewayVpnGatewayInterface {
return &VpnGatewayVpnGatewayInterface{
ProjectID: c.projectID,
ResourceID: value.Id,
IpAddress: value.IpAddress,
}
}
func (c *Client) transformVpnGatewayVpnGatewayInterfaces(values []*compute.VpnGatewayVpnGatewayInterface) []*VpnGatewayVpnGatewayInterface {
var tValues []*VpnGatewayVpnGatewayInterface
for _, v := range values {
tValues = append(tValues, c.transformVpnGatewayVpnGatewayInterface(v))
}
return tValues
}
func (c *Client) transformVpnGateway(value *compute.VpnGateway) *VpnGateway {
return &VpnGateway{
ProjectID: c.projectID,
CreationTimestamp: value.CreationTimestamp,
Description: value.Description,
ResourceID: value.Id,
Kind: value.Kind,
LabelFingerprint: value.LabelFingerprint,
Labels: c.transformVpnGatewayVpnGatewayLabels(value.Labels),
Name: value.Name,
Network: value.Network,
Region: value.Region,
SelfLink: value.SelfLink,
VpnInterfaces: c.transformVpnGatewayVpnGatewayInterfaces(value.VpnInterfaces),
}
}
func (c *Client) transformVpnGateways(values []*compute.VpnGateway) []*VpnGateway {
var tValues []*VpnGateway
for _, v := range values {
tValues = append(tValues, c.transformVpnGateway(v))
}
return tValues
}
type VpnGatewayConfig struct {
Filter string
}
var VPNGatewayTables = []interface{}{
&VpnGateway{},
&VpnGatewayVpnGatewayInterface{},
&VpnGatewayLabel{},
}
func (c *Client) vpnGateways(gConfig interface{}) error {
var config VpnGatewayConfig
err := mapstructure.Decode(gConfig, &config)
if err != nil {
return err
}
c.db.Where("project_id", c.projectID).Delete(VPNGatewayTables...)
nextPageToken := ""
for {
call := c.svc.VpnGateways.AggregatedList(c.projectID)
call.PageToken(nextPageToken)
output, err := call.Do()
if err != nil {
return err
}
var tValues []*VpnGateway
for _, items := range output.Items {
tValues = append(tValues, c.transformVpnGateways(items.VpnGateways)...)
}
c.db.ChunkedCreate(tValues)
c.log.Info("Fetched resources", zap.String("resource", "compute.vpn_gateways"), zap.Int("count", len(tValues)))
if output.NextPageToken == "" {
break
}
nextPageToken = output.NextPageToken
}
return nil
}