-
Notifications
You must be signed in to change notification settings - Fork 5
/
privatenetworks.go
148 lines (135 loc) · 4.21 KB
/
privatenetworks.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
package oneandone
import (
"net/http"
)
type PrivateNetwork struct {
Identity
descField
NetworkAddress string `json:"network_address,omitempty"`
SubnetMask string `json:"subnet_mask,omitempty"`
State string `json:"state,omitempty"`
SiteId string `json:"site_id,omitempty"`
CreationDate string `json:"creation_date,omitempty"`
Servers []Identity `json:"servers,omitempty"`
Datacenter *Datacenter `json:"datacenter,omitempty"`
ApiPtr
}
type PrivateNetworkRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
DatacenterId string `json:"datacenter_id,omitempty"`
NetworkAddress string `json:"network_address,omitempty"`
SubnetMask string `json:"subnet_mask,omitempty"`
}
// GET /private_networks
func (api *API) ListPrivateNetworks(args ...interface{}) ([]PrivateNetwork, error) {
url, err := processQueryParams(createUrl(api, privateNetworkPathSegment), args...)
if err != nil {
return nil, err
}
result := []PrivateNetwork{}
err = api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
for index, _ := range result {
result[index].api = api
}
return result, nil
}
// POST /private_networks
func (api *API) CreatePrivateNetwork(request *PrivateNetworkRequest) (string, *PrivateNetwork, error) {
result := new(PrivateNetwork)
url := createUrl(api, privateNetworkPathSegment)
err := api.Client.Post(url, &request, &result, http.StatusAccepted)
if err != nil {
return "", nil, err
}
result.api = api
return result.Id, result, nil
}
// GET /private_networks/{id}
func (api *API) GetPrivateNetwork(pn_id string) (*PrivateNetwork, error) {
result := new(PrivateNetwork)
url := createUrl(api, privateNetworkPathSegment, pn_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// PUT /private_networks/{id}
func (api *API) UpdatePrivateNetwork(pn_id string, request *PrivateNetworkRequest) (*PrivateNetwork, error) {
result := new(PrivateNetwork)
url := createUrl(api, privateNetworkPathSegment, pn_id)
err := api.Client.Put(url, &request, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// DELETE /private_networks/{id}
func (api *API) DeletePrivateNetwork(pn_id string) (*PrivateNetwork, error) {
result := new(PrivateNetwork)
url := createUrl(api, privateNetworkPathSegment, pn_id)
err := api.Client.Delete(url, nil, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /private_networks/{id}/servers
func (api *API) ListPrivateNetworkServers(pn_id string) ([]Identity, error) {
result := []Identity{}
url := createUrl(api, privateNetworkPathSegment, pn_id, "servers")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// POST /private_networks/{id}/servers
func (api *API) AttachPrivateNetworkServers(pn_id string, sids []string) (*PrivateNetwork, error) {
result := new(PrivateNetwork)
req := servers{
Servers: sids,
}
url := createUrl(api, privateNetworkPathSegment, pn_id, "servers")
err := api.Client.Post(url, &req, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /private_networks/{id}/servers/{id}
func (api *API) GetPrivateNetworkServer(pn_id string, server_id string) (*Identity, error) {
result := new(Identity)
url := createUrl(api, privateNetworkPathSegment, pn_id, "servers", server_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// DELETE /private_networks/{id}/servers/{id}
func (api *API) DetachPrivateNetworkServer(pn_id string, pns_id string) (*PrivateNetwork, error) {
result := new(PrivateNetwork)
url := createUrl(api, privateNetworkPathSegment, pn_id, "servers", pns_id)
err := api.Client.Delete(url, nil, &result, http.StatusAccepted)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
func (pn *PrivateNetwork) GetState() (string, error) {
in, err := pn.api.GetPrivateNetwork(pn.Id)
if in == nil {
return "", err
}
return in.State, err
}