forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
136 lines (120 loc) · 3.04 KB
/
client.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
package ovirtclient
import (
"context"
"crypto/tls"
"math/rand"
"net/http"
"sync"
ovirtsdk4 "github.com/ovirt/go-ovirt"
)
// Client is a simplified client for the oVirt API.
//
//goland:noinspection GoDeprecation
type Client interface {
// GetURL returns the oVirt engine base URL.
GetURL() string
// Reconnect triggers the client to reauthenticate against the oVirt Engine.
Reconnect() (err error)
// WithContext creates a subclient with the specified context applied.
WithContext(ctx context.Context) Client
// GetContext returns the current context of the client. May be nil.
GetContext() context.Context
AffinityGroupClient
DiskClient
DiskAttachmentClient
VMClient
NICClient
VNICProfileClient
NetworkClient
DatacenterClient
ClusterClient
StorageDomainClient
HostClient
TemplateClient
TemplateDiskClient
TestConnectionClient
TagClient
FeatureClient
InstanceTypeClient
GraphicsConsoleClient
}
// ClientWithLegacySupport is an extension of Client that also offers the ability to retrieve the underlying
// SDK connection or a configured HTTP client.
type ClientWithLegacySupport interface {
// GetSDKClient returns a configured oVirt SDK client for the use cases that are not covered by goVirt.
GetSDKClient() *ovirtsdk4.Connection
// GetHTTPClient returns a configured HTTP client for the oVirt engine. This can be used to send manual
// HTTP requests to the oVirt engine.
GetHTTPClient() http.Client
Client
}
type oVirtClient struct {
reconnectLock *sync.Mutex
conn *ovirtsdk4.Connection
ctx context.Context
httpClient http.Client
logger Logger
url string
username string
password string
tlsConfig *tls.Config
extraSettings ExtraSettings
nonSecureRandom *rand.Rand
verify func(connection Client) error
}
func (o *oVirtClient) WithContext(ctx context.Context) Client {
return &oVirtClient{
o.reconnectLock,
o.conn,
ctx,
o.httpClient,
o.logger.WithContext(ctx),
o.url,
o.username,
o.password,
o.tlsConfig,
o.extraSettings,
o.nonSecureRandom,
o.verify,
}
}
func (o *oVirtClient) GetContext() context.Context {
return o.ctx
}
func (o *oVirtClient) Reconnect() error {
o.reconnectLock.Lock()
defer o.reconnectLock.Unlock()
connBuilder := ovirtsdk4.NewConnectionBuilder().
URL(o.url).
Username(o.username).
Password(o.password).
TLSConfig(o.tlsConfig)
if err := processExtraSettings(o.extraSettings, connBuilder); err != nil {
return err
}
conn, err := connBuilder.Build()
if err != nil {
return wrap(err, EUnidentified, "failed to create underlying oVirt connection")
}
if o.conn == nil {
o.conn = conn
} else {
// Replace the structure under the pointer to make all instances update.
*o.conn = *conn
}
if o.verify != nil {
if err := o.verify(o); err != nil {
return err
}
}
return nil
}
func (o *oVirtClient) GetSDKClient() *ovirtsdk4.Connection {
return o.conn
}
func (o *oVirtClient) GetHTTPClient() http.Client {
return o.httpClient
}
func (o *oVirtClient) GetURL() string {
return o.url
}