forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_nic_create.go
66 lines (60 loc) · 1.42 KB
/
client_nic_create.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
package ovirtclient
import (
"fmt"
ovirtsdk "github.com/ovirt/go-ovirt"
)
func (o *oVirtClient) CreateNIC(
vmid string,
vnicProfileID string,
name string,
_ OptionalNICParameters,
retries ...RetryStrategy,
) (result NIC, err error) {
if err := validateNICCreationParameters(vmid, name); err != nil {
return nil, err
}
retries = defaultRetries(retries, defaultReadTimeouts())
err = retry(
fmt.Sprintf("creating NIC for VM %s", vmid),
o.logger,
retries,
func() error {
nicBuilder := ovirtsdk.NewNicBuilder()
nicBuilder.Name(name)
nicBuilder.VnicProfile(ovirtsdk.NewVnicProfileBuilder().Id(vnicProfileID).MustBuild())
nic := nicBuilder.MustBuild()
response, err := o.conn.SystemService().VmsService().VmService(vmid).NicsService().Add().Nic(nic).Send()
if err != nil {
return err
}
sdkObject, ok := response.Nic()
if !ok {
return newError(
ENotFound,
"no NIC returned creating NIC for VM ID %s",
vmid,
)
}
result, err = convertSDKNIC(sdkObject, o)
if err != nil {
return wrap(
err,
EBug,
"failed to convert newly created NIC for VM %s",
vmid,
)
}
return nil
},
)
return result, err
}
func validateNICCreationParameters(vmid string, name string) error {
if vmid == "" {
return newError(EBadArgument, "VM ID cannot be empty")
}
if name == "" {
return newError(EBadArgument, "NIC name cannot be empty")
}
return nil
}