-
Notifications
You must be signed in to change notification settings - Fork 14
/
cloud_server_vpc.go
192 lines (172 loc) · 5.99 KB
/
cloud_server_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
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
186
187
188
189
190
191
192
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"net/http"
"strings"
)
const (
vpcPath = "/vpc-networks"
)
var _ VPCNetworkService = (*cloudServerVPCNetworkResource)(nil)
type cloudServerVPCNetworkResource struct {
client *Client
}
func (cs *cloudServerService) VPCNetworks() *cloudServerVPCNetworkResource {
return &cloudServerVPCNetworkResource{client: cs.client}
}
type VPCNetworkService interface {
List(ctx context.Context) ([]*VPCNetwork, error)
Get(ctx context.Context, vpcID string) (*VPCNetwork, error)
Update(ctx context.Context, vpcID string, uvpl *UpdateVPCPayload) (*VPCNetwork, error)
Create(ctx context.Context, cvpl *CreateVPCPayload) (*VPCNetwork, error)
Delete(ctx context.Context, vpcID string) error
}
type VPCNetwork struct {
ID string `json:"id"`
Name string `json:"name"`
TenantID string `json:"tenant_id"`
AdminStateUp bool `json:"admin_state_up"`
MTU int `json:"mtu"`
Status string `json:"status"`
Subnets []Subnet `json:"subnets"`
Shared bool `json:"shared"`
AvailabilityZoneHints []string `json:"availability_zone_hints"`
AvailabilityZones []string `json:"availability_zones"`
IPv4AddressScope string `json:"ipv4_address_scope"`
IPv6AddressScope string `json:"ipv6_address_scope"`
RouterExternal bool `json:"router:external"`
Description string `json:"description"`
PortSecurityEnabled bool `json:"port_security_enabled"`
QosPolicyID string `json:"qos_policy_id"`
Tags []string `json:"tags"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
RevisionNumber int `json:"revision_number"`
IsDefault bool `json:"is_default"`
}
type Subnet struct {
ID string `json:"id"`
Name string `json:"name"`
TenantID string `json:"tenant_id"`
NetworkID string `json:"network_id"`
IPVersion int `json:"ip_version"`
SubnetPoolID string `json:"subnet_pool_id"`
EnableDHCP bool `json:"enable_dhcp"`
IPv6RaMode string `json:"ipv6_ra_mode"`
IPv6AddressMode string `json:"ipv6_address_mode"`
GatewayIP string `json:"gateway_ip"`
CIDR string `json:"cidr"`
AllocationPools []AllocationPool `json:"allocation_pools"`
HostRoutes []HostRoute `json:"host_routes"`
DNSNameServers []string `json:"dns_nameservers"`
Description string `json:"description"`
ServiceTypes []string `json:"service_types"`
Tags []string `json:"tags"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
RevisionNumber int `json:"revision_number"`
ProjectID string `json:"project_id"`
}
type HostRoute struct {
Destination string `json:"destination"`
NextHop string `json:"nexthop"`
}
type AllocationPool struct {
Start string `json:"start"`
End string `json:"end"`
}
type CreateVPCPayload struct {
Name string `json:"name"`
CIDR string `json:"cidr,omitempty"`
Description string `json:"description,omitempty"`
IsDefault bool `json:"is_default,omitempty"`
}
type UpdateVPCPayload struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CIDR string `json:"cidr,omitempty"`
IsDefault bool `json:"is_default,omitempty"`
}
func (v cloudServerVPCNetworkResource) resourcePath() string {
return vpcPath
}
func (v cloudServerVPCNetworkResource) itemPath(id string) string {
return strings.Join([]string{vpcPath, id}, "/")
}
func (v cloudServerVPCNetworkResource) List(ctx context.Context) ([]*VPCNetwork, error) {
req, err := v.client.NewRequest(ctx, http.MethodGet, serverServiceName, v.resourcePath(), nil)
if err != nil {
return nil, err
}
var data []*VPCNetwork
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (v cloudServerVPCNetworkResource) Get(ctx context.Context, vpcID string) (*VPCNetwork, error) {
req, err := v.client.NewRequest(ctx, http.MethodGet, serverServiceName, v.itemPath(vpcID), nil)
if err != nil {
return nil, err
}
var data *VPCNetwork
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (v cloudServerVPCNetworkResource) Update(ctx context.Context, vpcID string, uvpl *UpdateVPCPayload) (*VPCNetwork, error) {
req, err := v.client.NewRequest(ctx, http.MethodPut, serverServiceName, v.itemPath(vpcID), uvpl)
if err != nil {
return nil, err
}
var data *VPCNetwork
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (v cloudServerVPCNetworkResource) Create(ctx context.Context, cvpl *CreateVPCPayload) (*VPCNetwork, error) {
req, err := v.client.NewRequest(ctx, http.MethodPost, serverServiceName, v.resourcePath(), cvpl)
if err != nil {
return nil, err
}
var data *VPCNetwork
resp, err := v.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func (v cloudServerVPCNetworkResource) Delete(ctx context.Context, vpcID string) error {
req, err := v.client.NewRequest(ctx, http.MethodDelete, serverServiceName, v.itemPath(vpcID), nil)
if err != nil {
return err
}
resp, err := v.client.Do(ctx, req)
if err != nil {
return err
}
return resp.Body.Close()
}