-
Notifications
You must be signed in to change notification settings - Fork 16
/
lan.go
226 lines (205 loc) · 6.44 KB
/
lan.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package profitbricks
import (
"context"
"github.com/ionos-cloud/sdk-go/v5"
"net/http"
)
// Lan object
type Lan struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Properties LanProperties `json:"properties,omitempty"`
Entities *LanEntities `json:"entities,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// LanProperties object
type LanProperties struct {
Name string `json:"name,omitempty"`
Public bool `json:"public,omitempty"`
IPFailover *[]IPFailover `json:"ipFailover,omitempty"`
PCC string `json:"pcc,omitempty"`
}
// LanEntities object
type LanEntities struct {
Nics *LanNics `json:"nics,omitempty"`
}
// IPFailover object
type IPFailover struct {
NicUUID string `json:"nicUuid,omitempty"`
IP string `json:"ip,omitempty"`
}
// LanNics object
type LanNics struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Nic `json:"items,omitempty"`
}
// Lans object
type Lans struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Lan `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// ListLans returns a Collection for lans in the Datacenter
func (c *Client) ListLans(dcid string) (*Lans, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LanApi.DatacentersLansGet(ctx, dcid).Execute()
ret := Lans{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := lansPath(dcid)
ret := &Lans{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
// CreateLan creates a lan in the datacenter
// from a jason []byte and returns a Instance struct
func (c *Client) CreateLan(dcid string, request Lan) (*Lan, error) {
input := ionoscloud.LanPost{}
if errConvert := convertToCore(&request, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LanApi.DatacentersLansPost(ctx, dcid).Lan(input).Execute()
ret := Lan{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := lansPath(dcid)
ret := &Lan{}
err := c.Post(url, request, ret, http.StatusAccepted)
return ret, err
*/
}
// CreateLanAndWait creates a lan, waits for the request to finish and returns a refreshed lan
// Note that an error does not necessarily means that the resource has not been created.
// If err & res are not nil, a resource with res.ID exists, but an error occurred either while waiting for
// the request or when refreshing the resource.
func (c *Client) CreateLanAndWait(ctx context.Context, dcid string, request Lan) (res *Lan, err error) {
res, err = c.CreateLan(dcid, request)
if err != nil {
return
}
if err = c.WaitTillProvisionedOrCanceled(ctx, res.Headers.Get("location")); err != nil {
return
}
var lan *Lan
if lan, err = c.GetLan(dcid, res.ID); err != nil {
return
} else {
return lan, err
}
}
// GetLan pulls data for the lan where id = lanid returns an Instance struct
func (c *Client) GetLan(dcid, lanid string) (*Lan, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LanApi.DatacentersLansFindById(ctx, dcid, lanid).Execute()
ret := Lan{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := lanPath(dcid, lanid)
ret := &Lan{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
// UpdateLan does a partial update to a lan using json from []byte json returns a Instance struct
func (c *Client) UpdateLan(dcid string, lanid string, obj LanProperties) (*Lan, error) {
input := ionoscloud.LanProperties{}
if errConvert := convertToCore(&obj, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.LanApi.DatacentersLansPatch(ctx, dcid, lanid).Lan(input).Execute()
ret := Lan{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := lanPath(dcid, lanid)
ret := &Lan{}
err := c.Patch(url, obj, ret, http.StatusAccepted)
return ret, err
*/
}
// UpdateLanAndWait creates a lan, waits for the request to finish and returns a refreshed lan
// Note that an error does not necessarily means that the resource has not been updated.
// If err & res are not nil, a resource with res.ID exists, but an error occurred either while waiting for
// the request or when refreshing the resource.
func (c *Client) UpdateLanAndWait(ctx context.Context, dcid, lanid string, props LanProperties) (res *Lan, err error) {
res, err = c.UpdateLan(dcid, lanid, props)
if err != nil {
return
}
if err = c.WaitTillProvisionedOrCanceled(ctx, res.Headers.Get("location")); err != nil {
return
}
var lan *Lan
if lan, err = c.GetLan(dcid, res.ID); err != nil {
return
} else {
return lan, err
}
}
// DeleteLan deletes a lan where id == lanid
func (c *Client) DeleteLan(dcid, lanid string) (*http.Header, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
_, apiResponse, err := c.CoreSdk.LanApi.DatacentersLansDelete(ctx, dcid, lanid).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/*
url := lanPath(dcid, lanid)
ret := &http.Header{}
err := c.Delete(url, ret, http.StatusAccepted)
return ret, err
*/
}
// DeleteLanAndWait deletes given lan and waits for the request to finish
func (c *Client) DeleteLanAndWait(ctx context.Context, dcid, lanid string) error {
rsp, err := c.DeleteLan(dcid, lanid)
if err != nil {
return err
}
return c.WaitTillProvisionedOrCanceled(ctx, rsp.Get("location"))
}