@@ -29,13 +29,18 @@ const (
29
29
defaultHTTPScheme = "https"
30
30
)
31
31
32
+ // Useful for mocking
33
+ var timeNow = time .Now
34
+
32
35
// Client is an HTTP Versa client.
33
36
type Client struct {
34
- httpClient * http.Client
35
- endpoint string
36
- // TODO: add back with OAuth
37
- // token string
38
- // tokenExpiry time.Time
37
+ httpClient * http.Client
38
+ directorEndpoint string
39
+ directorAPIPort int
40
+ analyticsEndpoint string
41
+ // TODO: replace with OAuth
42
+ token string
43
+ tokenExpiry time.Time
39
44
username string
40
45
password string
41
46
authenticationMutex * sync.Mutex
@@ -49,8 +54,8 @@ type Client struct {
49
54
type ClientOptions func (* Client )
50
55
51
56
// NewClient creates a new Versa HTTP client.
52
- func NewClient (endpoint , username , password string , useHTTP bool , options ... ClientOptions ) (* Client , error ) {
53
- err := validateParams (endpoint , username , password )
57
+ func NewClient (directorEndpoint , analyticsEndpoint , username , password string , useHTTP bool , options ... ClientOptions ) (* Client , error ) {
58
+ err := validateParams (directorEndpoint , analyticsEndpoint , username , password )
54
59
if err != nil {
55
60
return nil , err
56
61
}
@@ -70,14 +75,21 @@ func NewClient(endpoint, username, password string, useHTTP bool, options ...Cli
70
75
scheme = "http"
71
76
}
72
77
73
- endpointURL := url.URL {
78
+ directorEndpointURL := url.URL {
74
79
Scheme : scheme ,
75
- Host : endpoint ,
80
+ Host : directorEndpoint ,
81
+ }
82
+
83
+ analyticsEndpointURL := url.URL {
84
+ Scheme : scheme ,
85
+ Host : analyticsEndpoint ,
76
86
}
77
87
78
88
client := & Client {
79
89
httpClient : httpClient ,
80
- endpoint : endpointURL .String (),
90
+ directorEndpoint : directorEndpointURL .String (),
91
+ directorAPIPort : 9182 , // TODO: make configurable based on auth type
92
+ analyticsEndpoint : analyticsEndpointURL .String (),
81
93
username : username ,
82
94
password : password ,
83
95
authenticationMutex : & sync.Mutex {},
@@ -94,9 +106,12 @@ func NewClient(endpoint, username, password string, useHTTP bool, options ...Cli
94
106
return client , nil
95
107
}
96
108
97
- func validateParams (endpoint , username , password string ) error {
98
- if endpoint == "" {
99
- return fmt .Errorf ("invalid endpoint" )
109
+ func validateParams (directorEndpoint , analyticsEndpoint , username , password string ) error {
110
+ if directorEndpoint == "" {
111
+ return fmt .Errorf ("invalid director endpoint" )
112
+ }
113
+ if analyticsEndpoint == "" {
114
+ return fmt .Errorf ("invalid analytics endpoint" )
100
115
}
101
116
if username == "" {
102
117
return fmt .Errorf ("invalid username" )
@@ -169,7 +184,7 @@ func WithLookback(lookback time.Duration) ClientOptions {
169
184
// GetOrganizations retrieves a list of organizations
170
185
func (client * Client ) GetOrganizations () ([]Organization , error ) {
171
186
var organizations []Organization
172
- resp , err := get [OrganizationListResponse ](client , "/vnms/organization/orgs" , nil )
187
+ resp , err := get [OrganizationListResponse ](client , "/vnms/organization/orgs" , nil , false )
173
188
if err != nil {
174
189
return nil , fmt .Errorf ("failed to get organizations: %v" , err )
175
190
}
@@ -183,7 +198,7 @@ func (client *Client) GetOrganizations() ([]Organization, error) {
183
198
"limit" : client .maxCount ,
184
199
"offset" : strconv .Itoa (i * maxCount ),
185
200
}
186
- resp , err := get [OrganizationListResponse ](client , "/vnms/organization/orgs" , params )
201
+ resp , err := get [OrganizationListResponse ](client , "/vnms/organization/orgs" , params , false )
187
202
if err != nil {
188
203
return nil , fmt .Errorf ("failed to get organizations: %v" , err )
189
204
}
@@ -209,7 +224,7 @@ func (client *Client) GetChildAppliancesDetail(tenant string) ([]Appliance, erro
209
224
}
210
225
211
226
// Get the total count of appliances
212
- totalCount , err := get [int ](client , uri , params )
227
+ totalCount , err := get [int ](client , uri , params , false )
213
228
if err != nil {
214
229
return nil , fmt .Errorf ("failed to get appliance detail response: %v" , err )
215
230
}
@@ -223,7 +238,7 @@ func (client *Client) GetChildAppliancesDetail(tenant string) ([]Appliance, erro
223
238
for i := 0 ; i < totalPages ; i ++ {
224
239
params ["fetch" ] = "all"
225
240
params ["offset" ] = fmt .Sprintf ("%d" , i * maxCount )
226
- resp , err := get [[]Appliance ](client , uri , params )
241
+ resp , err := get [[]Appliance ](client , uri , params , false )
227
242
if err != nil {
228
243
return nil , fmt .Errorf ("failed to get appliance detail response: %v" , err )
229
244
}
@@ -238,7 +253,7 @@ func (client *Client) GetChildAppliancesDetail(tenant string) ([]Appliance, erro
238
253
239
254
// GetDirectorStatus retrieves the director status
240
255
func (client * Client ) GetDirectorStatus () (* DirectorStatus , error ) {
241
- resp , err := get [DirectorStatus ](client , "/vnms/dashboard/vdStatus" , nil )
256
+ resp , err := get [DirectorStatus ](client , "/vnms/dashboard/vdStatus" , nil , false )
242
257
if err != nil {
243
258
return nil , fmt .Errorf ("failed to get director status: %v" , err )
244
259
}
@@ -303,7 +318,7 @@ func (client *Client) GetSLAMetrics() ([]SLAMetrics, error) {
303
318
"pduLossRatio" ,
304
319
})
305
320
306
- resp , err := get [SLAMetricsResponse ](client , analyticsURL , nil )
321
+ resp , err := get [SLAMetricsResponse ](client , analyticsURL , nil , true )
307
322
if err != nil {
308
323
return nil , fmt .Errorf ("failed to get SLA metrics: %v" , err )
309
324
}
0 commit comments