forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpc.go
120 lines (101 loc) · 3.77 KB
/
vpc.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
// Copyright (c) 2018 Aiven, Helsinki, Finland. https://aiven.io/
package aiven
import (
"encoding/json"
"errors"
)
type (
// VPCPeeringConnection holds parameters associated with a VPC peering connection
VPCPeeringConnection struct {
CreateTime *string `json:"create_time"`
PeerCloudAccount string `json:"peer_cloud_account"`
PeerVPC string `json:"peer_vpc"`
PeerRegion *string `json:"peer_region"`
State string `json:"state"`
UpdateTime string `json:"update_time"`
StateInfo *map[string]interface{} `json:"state_info"`
PeerAzureAppId string `json:"peer_azure_app_id,omitempty"`
PeerAzureTenantId string `json:"peer_azure_tenant_id,omitempty"`
UserPeerNetworkCIDRs []string `json:"user_peer_network_cidrs,omitempty"`
VPCPeeringConnectionType string `json:"vpc_peering_connection_type,omitempty"`
PeerResourceGroup string `json:"peer_resource_group,omitempty"`
}
// VPC holds parameters associated with a Virtual Private Cloud
VPC struct {
CloudName string `json:"cloud_name"`
CreateTime *string `json:"create_time"`
NetworkCIDR string `json:"network_cidr"`
ProjectVPCID string `json:"project_vpc_id"`
State string `json:"state"`
UpdateTime string `json:"update_time"`
PeeringConnections []*VPCPeeringConnection `json:"peering_connections"`
}
// VPCsHandler is the client that interacts with the VPCs API on Aiven.
VPCsHandler struct {
client *Client
}
// CreateVPCRequest holds the parameters to create a new VPC.
CreateVPCRequest struct {
CloudName string `json:"cloud_name"`
NetworkCIDR string `json:"network_cidr"`
PeeringConnections []*VPCPeeringConnection `json:"peering_connections"`
}
// VPCListResponse represents the response from Aiven for listing VPCs.
VPCListResponse struct {
APIResponse
VPCs []*VPC `json:"vpcs"`
}
)
// Create the given VPC on Aiven.
func (h *VPCsHandler) Create(project string, req CreateVPCRequest) (*VPC, error) {
path := buildPath("project", project, "vpcs")
if req.PeeringConnections == nil {
req.PeeringConnections = []*VPCPeeringConnection{}
}
rsp, err := h.client.doPostRequest(path, req)
if err != nil {
return nil, err
}
return parseVPCResponse(rsp)
}
// Get a specific VPC from Aiven.
func (h *VPCsHandler) Get(project, vpcID string) (*VPC, error) {
path := buildPath("project", project, "vpcs", vpcID)
rsp, err := h.client.doGetRequest(path, nil)
if err != nil {
return nil, err
}
return parseVPCResponse(rsp)
}
// Delete the given VPC from Aiven.
func (h *VPCsHandler) Delete(project, vpcID string) error {
path := buildPath("project", project, "vpcs", vpcID)
bts, err := h.client.doDeleteRequest(path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// List all VPCs for a given project.
func (h *VPCsHandler) List(project string) ([]*VPC, error) {
path := buildPath("project", project, "vpcs")
rsp, err := h.client.doGetRequest(path, nil)
if err != nil {
return nil, err
}
var response *VPCListResponse
if err := json.Unmarshal(rsp, &response); err != nil {
return nil, err
}
if len(response.Errors) != 0 {
return nil, errors.New(response.Message)
}
return response.VPCs, nil
}
func parseVPCResponse(rsp []byte) (*VPC, error) {
var response *VPC
if err := json.Unmarshal(rsp, &response); err != nil {
return nil, err
}
return response, nil
}