forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_nic.go
238 lines (203 loc) · 6.64 KB
/
client_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
package ovirtclient
import (
ovirtsdk "github.com/ovirt/go-ovirt"
)
// 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 string,
vnicProfileID string,
name string,
optional OptionalNICParameters,
retries ...RetryStrategy,
) (NIC, error)
// UpdateNIC allows updating the NIC.
UpdateNIC(
vmid string,
nicID string,
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 string, id string, retries ...RetryStrategy) (NIC, error)
// ListNICs lists all NICs attached to the VM specified in vmid.
ListNICs(vmid string, retries ...RetryStrategy) ([]NIC, error)
// RemoveNIC removes the network interface specified.
RemoveNIC(vmid string, id string, retries ...RetryStrategy) error
}
// OptionalNICParameters is an interface that declares the source of optional parameters for NIC creation.
type OptionalNICParameters interface{}
// BuildableNICParameters is a modifiable version of OptionalNICParameters. You can use CreateNICParams() to create a
// new copy, or implement your own.
type BuildableNICParameters interface {
OptionalNICParameters
}
// CreateNICParams returns a buildable structure of OptionalNICParameters.
func CreateNICParams() BuildableNICParameters {
return &nicParams{}
}
type nicParams struct{}
// 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() *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 string) (BuildableUpdateNICParameters, error)
// MustWithVNICPRofileID is identical to WithVNICProfileID, but panics instead of returning an error.
MustWithVNICProfileID(id string) BuildableUpdateNICParameters
}
// UpdateNICParams creates a buildable UpdateNICParameters.
func UpdateNICParams() BuildableUpdateNICParameters {
return &updateNICParams{}
}
type updateNICParams struct {
name *string
vnicProfileID *string
}
func (u *updateNICParams) Name() *string {
return u.name
}
func (u *updateNICParams) VNICProfileID() *string {
return u.vnicProfileID
}
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 string) (BuildableUpdateNICParameters, error) {
u.vnicProfileID = &id
return u, nil
}
func (u *updateNICParams) MustWithVNICProfileID(id string) BuildableUpdateNICParameters {
b, err := u.WithVNICProfileID(id)
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() string
// 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() string
// VNICProfileID returns the ID of the VNIC profile in use by the NIC.
VNICProfileID() 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")
}
return &nic{
cli,
id,
name,
vmid,
vnicProfileID,
}, nil
}
type nic struct {
client Client
id string
name string
vmid string
vnicProfileID 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() string {
return n.vnicProfileID
}
func (n nic) ID() string {
return n.id
}
func (n nic) Name() string {
return n.name
}
func (n nic) VMID() string {
return n.vmid
}
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,
}
}
func (n nic) withVNICProfileID(vnicProfileID string) *nic {
return &nic{
client: n.client,
id: n.id,
name: n.name,
vmid: n.vmid,
vnicProfileID: vnicProfileID,
}
}