-
Notifications
You must be signed in to change notification settings - Fork 29
/
vpc_peering_connection.go
185 lines (159 loc) · 5.66 KB
/
vpc_peering_connection.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
package aiven
import (
"context"
"encoding/json"
)
type (
// VPCPeeringConnectionsHandler is the client that interacts with the VPC
// Peering Connections API on Aiven.
VPCPeeringConnectionsHandler struct {
client *Client
}
// CreateVPCPeeringConnectionRequest holds the parameters to create a new
// peering connection.
CreateVPCPeeringConnectionRequest struct {
PeerCloudAccount string `json:"peer_cloud_account"`
PeerVPC string `json:"peer_vpc"`
PeerRegion *string `json:"peer_region,omitempty"`
PeerAzureAppId string `json:"peer_azure_app_id,omitempty"`
PeerAzureTenantId string `json:"peer_azure_tenant_id,omitempty"`
PeerResourceGroup string `json:"peer_resource_group,omitempty"`
UserPeerNetworkCIDRs []string `json:"user_peer_network_cidrs,omitempty"`
}
)
// Create the given VPC on Aiven.
// when CreateVPCPeeringConnectionRequest.PeerRegion == nil the PeerVPC must be in the
// same region as the project VPC (vpcID)
func (h *VPCPeeringConnectionsHandler) Create(
ctx context.Context,
project string,
vpcID string,
req CreateVPCPeeringConnectionRequest,
) (*VPCPeeringConnection, error) {
path := buildPath("project", project, "vpcs", vpcID, "peering-connections")
rsp, err := h.client.doPostRequest(ctx, path, req)
if err != nil {
return nil, err
}
var response *VPCPeeringConnection
if err := json.Unmarshal(rsp, &response); err != nil {
return nil, err
}
// if region was not set in the request omit it from the response
if req.PeerRegion == nil {
response.PeerRegion = nil
}
return response, nil
}
// GetVPCPeering Connection from Aiven.
// if peerRegion == nil the peered VPC is assumed to be in the same region as the project VPC (vpcID)
func (h *VPCPeeringConnectionsHandler) GetVPCPeering(
ctx context.Context,
project string,
vpcID string,
peerCloudAccount string,
peerVPC string,
peerRegion *string,
) (*VPCPeeringConnection, error) {
// There's no API call for getting individual peering connection. Get the VPC
// info and filter from there
vpc, err := h.client.VPCs.Get(ctx, project, vpcID)
if err != nil {
return nil, err
}
for _, pc := range vpc.PeeringConnections {
if (peerRegion == nil || eqStrPointers(pc.PeerRegion, peerRegion)) && pc.PeerCloudAccount == peerCloudAccount && pc.PeerVPC == peerVPC {
return pc, nil
}
}
err = Error{Message: "Peering connection not found", Status: 404}
return nil, err
}
// GetVPCPeeringWithResourceGroup retrieves a VPC peering connection
func (h *VPCPeeringConnectionsHandler) GetVPCPeeringWithResourceGroup(
ctx context.Context,
project string,
vpcID string,
peerCloudAccount string,
peerVPC string,
peerRegion *string,
peerResourceGroup *string,
) (*VPCPeeringConnection, error) {
// There's no API call for getting individual peering connection. Get the VPC
// info and filter from there
vpc, err := h.client.VPCs.Get(ctx, project, vpcID)
if err != nil {
return nil, err
}
for _, pc := range vpc.PeeringConnections {
found := pc.PeerCloudAccount == peerCloudAccount &&
pc.PeerVPC == peerVPC &&
// Not given or equal
(peerRegion == nil || eqStrPointers(pc.PeerRegion, peerRegion)) &&
(peerResourceGroup == nil || eqStrPointers(pc.PeerResourceGroup, peerResourceGroup))
if found {
return pc, nil
}
}
err = Error{Message: "Peering connection not found", Status: 404}
return nil, err
}
// Get a VPC Peering Connection from Aiven.
func (h *VPCPeeringConnectionsHandler) Get(
ctx context.Context,
project string,
vpcID string,
peerCloudAccount string,
peerVPC string,
) (*VPCPeeringConnection, error) {
return h.GetVPCPeering(ctx, project, vpcID, peerCloudAccount, peerVPC, nil)
}
// DeleteVPCPeering Connection from Aiven.
// If peerRegion == nil the peering VPC must be in the same region as project VPC (vpcID)
func (h *VPCPeeringConnectionsHandler) DeleteVPCPeering(ctx context.Context, project, vpcID, peerCloudAccount, peerVPC string, peerRegion *string) error {
pathElements := []string{"project", project, "vpcs", vpcID, "peering-connections", "peer-accounts", peerCloudAccount, "peer-vpcs", peerVPC}
if peerRegion != nil {
pathElements = append(pathElements, "peer-regions", *peerRegion)
}
bts, err := h.client.doDeleteRequest(ctx, buildPath(pathElements...), nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// DeleteVPCPeeringWithResourceGroup deletes a VPC peering connection
func (h *VPCPeeringConnectionsHandler) DeleteVPCPeeringWithResourceGroup(ctx context.Context, project, vpcID, peerCloudAccount, peerVPC, peerResourceGroup string, peerRegion *string) error {
pathElements := []string{"project", project,
"vpcs", vpcID,
"peering-connections", "peer-accounts", peerCloudAccount,
"peer-resource-groups", peerResourceGroup,
"peer-vpcs", peerVPC,
}
if peerRegion != nil {
pathElements = append(pathElements, "peer-regions", *peerRegion)
}
bts, err := h.client.doDeleteRequest(ctx, buildPath(pathElements...), nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// Delete the given VPC Peering Connection from Aiven.
func (h *VPCPeeringConnectionsHandler) Delete(ctx context.Context, project, vpcID, peerCloudAccount, peerVPC string) error {
return h.DeleteVPCPeering(ctx, project, vpcID, peerCloudAccount, peerVPC, nil)
}
// List all VPC peering connections for a given VPC.
func (h *VPCPeeringConnectionsHandler) List(ctx context.Context, project, vpcID string) ([]*VPCPeeringConnection, error) {
vpc, err := h.client.VPCs.Get(ctx, project, vpcID)
if err != nil {
return nil, err
}
return vpc.PeeringConnections, nil
}
func eqStrPointers(a, b *string) bool {
if a != nil && b != nil {
return *a == *b
}
// one or both of them nil
return a == b
}