-
Notifications
You must be signed in to change notification settings - Fork 20
/
nic.go
324 lines (276 loc) · 8.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package ovirtclient
import (
ovirtsdk "github.com/ovirt/go-ovirt"
)
// NICID is the ID for a network interface.
type NICID string
// NICClient defines the methods related to dealing with network interfaces.
type NICClient interface {
// CreateNIC adds a new NIC to a VM specified in vmid.
CreateNIC(
vmid VMID,
vnicProfileID VNICProfileID,
name string,
optional OptionalNICParameters,
retries ...RetryStrategy,
) (NIC, error)
// UpdateNIC allows updating the NIC.
UpdateNIC(
vmid VMID,
nicID NICID,
params UpdateNICParameters,
retries ...RetryStrategy,
) (NIC, error)
// GetNIC returns one specific NIC with the ID specified in id, attached to a VM with the ID specified in vmid.
GetNIC(vmid VMID, id NICID, retries ...RetryStrategy) (NIC, error)
// ListNICs lists all NICs attached to the VM specified in vmid.
ListNICs(vmid VMID, retries ...RetryStrategy) ([]NIC, error)
// RemoveNIC removes the network interface specified.
RemoveNIC(vmid VMID, id NICID, retries ...RetryStrategy) error
}
// OptionalNICParameters is an interface that declares the source of optional parameters for NIC creation.
type OptionalNICParameters interface {
// represent mac_address for NIC
Mac() string
}
// BuildableNICParameters is a modifiable version of OptionalNICParameters. You can use CreateNICParams() to create a
// new copy, or implement your own.
type BuildableNICParameters interface {
OptionalNICParameters
// WithMac sets macAddress for the NIC.
WithMac(mac string) (BuildableNICParameters, error)
// MustWithMac is the same as WithMac, but panics instead of returning an error.
MustWithMac(mac string) BuildableNICParameters
}
// CreateNICParams returns a buildable structure of OptionalNICParameters.
func CreateNICParams() BuildableNICParameters {
return &nicParams{}
}
type nicParams struct {
mac string
}
func (c *nicParams) Mac() string {
return c.mac
}
func (c *nicParams) WithMac(mac string) (BuildableNICParameters, error) {
c.mac = mac
return c, nil
}
func (c *nicParams) MustWithMac(mac string) BuildableNICParameters {
builder, err := c.WithMac(mac)
if err != nil {
panic(err)
}
return builder
}
// UpdateNICParameters is an interface that declares methods of changeable parameters for NIC's. Each
// method can return nil to leave an attribute unchanged, or a new value for the attribute.
type UpdateNICParameters interface {
// Name potentially returns a changed name for a NIC.
Name() *string
// VNICProfileID potentially returns a change VNIC profile for a NIC.
VNICProfileID() *VNICProfileID
// Mac potentially returns a change MacAddress for a nic
Mac() *string
}
// BuildableUpdateNICParameters is a buildable version of UpdateNICParameters.
type BuildableUpdateNICParameters interface {
UpdateNICParameters
// WithName sets the name of a NIC for the UpdateNIC method.
WithName(name string) (BuildableUpdateNICParameters, error)
// MustWithName is identical to WithName, but panics instead of returning an error.
MustWithName(name string) BuildableUpdateNICParameters
// WithVNICProfileID sets the VNIC profile ID of a NIC for the UpdateNIC method.
WithVNICProfileID(id VNICProfileID) (BuildableUpdateNICParameters, error)
// MustWithVNICProfileID is identical to WithVNICProfileID, but panics instead of returning an error.
MustWithVNICProfileID(id VNICProfileID) BuildableUpdateNICParameters
// WithMac sets MaAddress of a NIC for the UpdateNIC method.
WithMac(mac string) (BuildableUpdateNICParameters, error)
// MustWithMac is identical to WithMac, but panics instead of returning an error.
MustWithMac(mac string) BuildableUpdateNICParameters
}
// UpdateNICParams creates a buildable UpdateNICParameters.
func UpdateNICParams() BuildableUpdateNICParameters {
return &updateNICParams{}
}
type updateNICParams struct {
name *string
vnicProfileID *VNICProfileID
mac *string
}
func (u *updateNICParams) Name() *string {
return u.name
}
func (u *updateNICParams) VNICProfileID() *VNICProfileID {
return u.vnicProfileID
}
func (u *updateNICParams) Mac() *string {
return u.mac
}
func (u *updateNICParams) WithName(name string) (BuildableUpdateNICParameters, error) {
u.name = &name
return u, nil
}
func (u *updateNICParams) MustWithName(name string) BuildableUpdateNICParameters {
b, err := u.WithName(name)
if err != nil {
panic(err)
}
return b
}
func (u *updateNICParams) WithVNICProfileID(id VNICProfileID) (BuildableUpdateNICParameters, error) {
u.vnicProfileID = &id
return u, nil
}
func (u *updateNICParams) MustWithVNICProfileID(id VNICProfileID) BuildableUpdateNICParameters {
b, err := u.WithVNICProfileID(id)
if err != nil {
panic(err)
}
return b
}
func (u *updateNICParams) WithMac(mac string) (BuildableUpdateNICParameters, error) {
u.mac = &mac
return u, nil
}
func (u *updateNICParams) MustWithMac(mac string) BuildableUpdateNICParameters {
b, err := u.WithMac(mac)
if err != nil {
panic(err)
}
return b
}
// NICData is the core of NIC which only provides data-access functions.
type NICData interface {
// ID is the identifier for this network interface.
ID() NICID
// Name is the user-given name of the network interface.
Name() string
// VMID is the identified of the VM this NIC is attached to. May be nil if the NIC is not attached.
VMID() VMID
// VNICProfileID returns the ID of the VNIC profile in use by the NIC.
VNICProfileID() VNICProfileID
// Mac returns a MacAddress for a nic
Mac() string
}
// NIC represents a network interface.
type NIC interface {
NICData
// GetVM fetches an up to date copy of the virtual machine this NIC is attached to. This involves an API call and
// may be slow.
GetVM(retries ...RetryStrategy) (VM, error)
// GetVNICProfile retrieves the VNIC profile associated with this NIC. This involves an API call and may be slow.
GetVNICProfile(retries ...RetryStrategy) (VNICProfile, error)
// Update updates the NIC with the specified parameters. It returns the updated NIC as a response. You can use
// UpdateNICParams() to obtain a buildable parameter structure.
Update(params UpdateNICParameters, retries ...RetryStrategy) (NIC, error)
// Remove removes the current network interface. This involves an API call and may be slow.
Remove(retries ...RetryStrategy) error
}
func convertSDKNIC(sdkObject *ovirtsdk.Nic, cli Client) (NIC, error) {
id, ok := sdkObject.Id()
if !ok {
return nil, newFieldNotFound("id", "NIC")
}
name, ok := sdkObject.Name()
if !ok {
return nil, newFieldNotFound("name", "NIC")
}
vm, ok := sdkObject.Vm()
if !ok {
return nil, newFieldNotFound("vm", "NIC")
}
vmid, ok := vm.Id()
if !ok {
return nil, newFieldNotFound("VM in NIC", "ID")
}
vnicProfile, ok := sdkObject.VnicProfile()
if !ok {
return nil, newFieldNotFound("VM", "vNIC Profile")
}
vnicProfileID, ok := vnicProfile.Id()
if !ok {
return nil, newFieldNotFound("vNIC Profile on VM", "ID")
}
mac, ok := sdkObject.Mac()
if !ok {
return nil, newFieldNotFound("mac", "NIC")
}
macAddr, ok := mac.Address()
if !ok {
return nil, newFieldNotFound("address", "mac")
}
return &nic{
cli,
NICID(id),
name,
VMID(vmid),
VNICProfileID(vnicProfileID),
macAddr,
}, nil
}
type nic struct {
client Client
id NICID
name string
vmid VMID
vnicProfileID VNICProfileID
mac string
}
func (n nic) Update(params UpdateNICParameters, retries ...RetryStrategy) (NIC, error) {
return n.client.UpdateNIC(n.vmid, n.id, params, retries...)
}
func (n nic) GetVM(retries ...RetryStrategy) (VM, error) {
return n.client.GetVM(n.vmid, retries...)
}
func (n nic) GetVNICProfile(retries ...RetryStrategy) (VNICProfile, error) {
return n.client.GetVNICProfile(n.vnicProfileID, retries...)
}
func (n nic) VNICProfileID() VNICProfileID {
return n.vnicProfileID
}
func (n nic) ID() NICID {
return n.id
}
func (n nic) Name() string {
return n.name
}
func (n nic) VMID() VMID {
return n.vmid
}
func (n nic) Mac() string {
return n.mac
}
func (n nic) Remove(retries ...RetryStrategy) error {
return n.client.RemoveNIC(n.vmid, n.id, retries...)
}
func (n nic) withName(name string) *nic {
return &nic{
client: n.client,
id: n.id,
name: name,
vmid: n.vmid,
vnicProfileID: n.vnicProfileID,
mac: n.mac,
}
}
func (n nic) withVNICProfileID(vnicProfileID VNICProfileID) *nic {
return &nic{
client: n.client,
id: n.id,
name: n.name,
vmid: n.vmid,
vnicProfileID: vnicProfileID,
mac: n.mac,
}
}
func (n nic) withMac(mac string) *nic {
return &nic{
client: n.client,
id: n.id,
name: n.name,
vmid: n.vmid,
vnicProfileID: n.vnicProfileID,
mac: mac,
}
}