-
Notifications
You must be signed in to change notification settings - Fork 16
/
nic.go
164 lines (148 loc) · 4.6 KB
/
nic.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
package profitbricks
import (
"github.com/ionos-cloud/sdk-go/v5"
"net/http"
)
// Nic object
type Nic struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Properties *NicProperties `json:"properties,omitempty"`
Entities *NicEntities `json:"entities,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// NicProperties object
type NicProperties struct {
Name string `json:"name,omitempty"`
Mac string `json:"mac,omitempty"`
Ips []string `json:"ips,omitempty"`
Dhcp *bool `json:"dhcp,omitempty"`
Lan int `json:"lan,omitempty"`
FirewallActive *bool `json:"firewallActive,omitempty"`
Nat *bool `json:"nat,omitempty"`
}
// NicEntities object
type NicEntities struct {
FirewallRules *FirewallRules `json:"firewallrules,omitempty"`
}
// Nics object
type Nics struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Nic `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// ListNics returns a Nics struct collection
func (c *Client) ListNics(dcid, srvid string) (*Nics, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.NicApi.DatacentersServersNicsGet(ctx, dcid, srvid).Execute()
ret := Nics{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := nicsPath(dcid, srvid)
ret := &Nics{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
// CreateNic creates a nic on a server
func (c *Client) CreateNic(dcid string, srvid string, nic Nic) (*Nic, error) {
input := ionoscloud.Nic{}
if errConvert := convertToCore(&nic, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.NicApi.DatacentersServersNicsPost(ctx, dcid, srvid).Nic(input).Execute()
ret := Nic{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := nicsPath(dcid, srvid)
ret := &Nic{}
err := c.Post(url, nic, ret, http.StatusAccepted)
return ret, err
*/
}
// GetNic pulls data for the nic where id = srvid returns a Instance struct
func (c *Client) GetNic(dcid, srvid, nicid string) (*Nic, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.NicApi.DatacentersServersNicsFindById(ctx, dcid, srvid, nicid).Execute()
ret := Nic{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := nicPath(dcid, srvid, nicid)
ret := &Nic{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
// UpdateNic partial update of nic properties
func (c *Client) UpdateNic(dcid string, srvid string, nicid string, obj NicProperties) (*Nic, error) {
input := ionoscloud.NicProperties{}
if errConvert := convertToCore(&obj, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.NicApi.DatacentersServersNicsPatch(ctx, dcid, srvid, nicid).Nic(input).Execute()
ret := Nic{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := nicPath(dcid, srvid, nicid)
ret := &Nic{}
err := c.Patch(url, obj, ret, http.StatusAccepted)
return ret, err
*/
}
// DeleteNic deletes the nic where id=nicid and returns a Resp struct
func (c *Client) DeleteNic(dcid, srvid, nicid string) (*http.Header, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
_, apiResponse, err := c.CoreSdk.NicApi.DatacentersServersNicsDelete(ctx, dcid, srvid, nicid).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/*
url := nicPath(dcid, srvid, nicid)
ret := &http.Header{}
err := c.Delete(url, ret, http.StatusAccepted)
return ret, err
*/
}