@@ -3,6 +3,7 @@ package client
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"io"
8
9
"io/ioutil"
@@ -12,7 +13,7 @@ import (
12
13
)
13
14
14
15
const (
15
- // APIVersion is a version of NGINX Plus API.
16
+ // APIVersion is the default version of NGINX Plus API supported by the client .
16
17
APIVersion = 5
17
18
18
19
pathNotFoundCode = "PathNotFound"
@@ -21,8 +22,10 @@ const (
21
22
defaultServerPort = "80"
22
23
)
23
24
24
- // Default values for servers in Upstreams.
25
25
var (
26
+ supportedAPIVersions = versions {4 , 5 }
27
+
28
+ // Default values for servers in Upstreams.
26
29
defaultMaxConns = 0
27
30
defaultMaxFails = 1
28
31
defaultFailTimeout = "10s"
32
35
defaultWeight = 1
33
36
)
34
37
38
+ // ErrUnsupportedVer means that client's API version is not supported by NGINX plus API
39
+ var ErrUnsupportedVer = errors .New ("API version of the client is not supported by running NGINX Plus" )
40
+
35
41
// NginxClient lets you access NGINX Plus API.
36
42
type NginxClient struct {
43
+ version int
37
44
apiEndpoint string
38
45
httpClient * http.Client
39
46
}
@@ -378,31 +385,46 @@ type Processes struct {
378
385
Respawned int64
379
386
}
380
387
381
- // NewNginxClient creates an NginxClient.
388
+ // NewNginxClient creates an NginxClient with the latest supported version .
382
389
func NewNginxClient (httpClient * http.Client , apiEndpoint string ) (* NginxClient , error ) {
390
+ return NewNginxClientWithVersion (httpClient , apiEndpoint , APIVersion )
391
+ }
392
+
393
+ //NewNginxClientWithVersion creates an NginxClient with the given version of NGINX Plus API.
394
+ func NewNginxClientWithVersion (httpClient * http.Client , apiEndpoint string , version int ) (* NginxClient , error ) {
395
+ if ! versionSupported (version ) {
396
+ return nil , fmt .Errorf ("API version %v is not supported by the client" , version )
397
+ }
383
398
versions , err := getAPIVersions (httpClient , apiEndpoint )
384
399
if err != nil {
385
400
return nil , fmt .Errorf ("error accessing the API: %v" , err )
386
401
}
387
-
388
402
found := false
389
403
for _ , v := range * versions {
390
- if v == APIVersion {
404
+ if v == version {
391
405
found = true
392
406
break
393
407
}
394
408
}
395
-
396
409
if ! found {
397
- return nil , fmt . Errorf ( "API version %v of the client is not supported by API versions of NGINX Plus: %v" , APIVersion , * versions )
410
+ return nil , ErrUnsupportedVer
398
411
}
399
-
400
412
return & NginxClient {
401
413
apiEndpoint : apiEndpoint ,
402
414
httpClient : httpClient ,
415
+ version : version ,
403
416
}, nil
404
417
}
405
418
419
+ func versionSupported (n int ) bool {
420
+ for _ , version := range supportedAPIVersions {
421
+ if n == version {
422
+ return true
423
+ }
424
+ }
425
+ return false
426
+ }
427
+
406
428
func getAPIVersions (httpClient * http.Client , endpoint string ) (* versions , error ) {
407
429
resp , err := httpClient .Get (endpoint )
408
430
if err != nil {
@@ -652,7 +674,7 @@ func (client *NginxClient) getIDOfHTTPServer(upstream string, name string) (int,
652
674
}
653
675
654
676
func (client * NginxClient ) get (path string , data interface {}) error {
655
- url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , APIVersion , path )
677
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client . version , path )
656
678
resp , err := client .httpClient .Get (url )
657
679
if err != nil {
658
680
return fmt .Errorf ("failed to get %v: %v" , path , err )
@@ -677,7 +699,7 @@ func (client *NginxClient) get(path string, data interface{}) error {
677
699
}
678
700
679
701
func (client * NginxClient ) post (path string , input interface {}) error {
680
- url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , APIVersion , path )
702
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client . version , path )
681
703
682
704
jsonInput , err := json .Marshal (input )
683
705
if err != nil {
@@ -699,7 +721,7 @@ func (client *NginxClient) post(path string, input interface{}) error {
699
721
}
700
722
701
723
func (client * NginxClient ) delete (path string , expectedStatusCode int ) error {
702
- path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , APIVersion , path )
724
+ path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client . version , path )
703
725
704
726
req , err := http .NewRequest (http .MethodDelete , path , nil )
705
727
if err != nil {
@@ -721,7 +743,7 @@ func (client *NginxClient) delete(path string, expectedStatusCode int) error {
721
743
}
722
744
723
745
func (client * NginxClient ) patch (path string , input interface {}, expectedStatusCode int ) error {
724
- path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , APIVersion , path )
746
+ path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client . version , path )
725
747
726
748
jsonInput , err := json .Marshal (input )
727
749
if err != nil {
@@ -1139,6 +1161,9 @@ func (client *NginxClient) GetStreamZoneSync() (*StreamZoneSync, error) {
1139
1161
// GetLocationZones returns http/location_zones stats.
1140
1162
func (client * NginxClient ) GetLocationZones () (* LocationZones , error ) {
1141
1163
var locationZones LocationZones
1164
+ if client .version < 5 {
1165
+ return & locationZones , nil
1166
+ }
1142
1167
err := client .get ("http/location_zones" , & locationZones )
1143
1168
if err != nil {
1144
1169
return nil , fmt .Errorf ("failed to get location zones: %v" , err )
@@ -1150,6 +1175,9 @@ func (client *NginxClient) GetLocationZones() (*LocationZones, error) {
1150
1175
// GetResolvers returns Resolvers stats.
1151
1176
func (client * NginxClient ) GetResolvers () (* Resolvers , error ) {
1152
1177
var resolvers Resolvers
1178
+ if client .version < 5 {
1179
+ return & resolvers , nil
1180
+ }
1153
1181
err := client .get ("resolvers" , & resolvers )
1154
1182
if err != nil {
1155
1183
return nil , fmt .Errorf ("failed to get resolvers: %v" , err )
@@ -1368,6 +1396,11 @@ func (client *NginxClient) UpdateStreamServer(upstream string, server StreamUpst
1368
1396
return nil
1369
1397
}
1370
1398
1399
+ // Version returns client's current N+ API version.
1400
+ func (client * NginxClient ) Version () int {
1401
+ return client .version
1402
+ }
1403
+
1371
1404
func addPortToServer (server string ) string {
1372
1405
if len (strings .Split (server , ":" )) == 2 {
1373
1406
return server
0 commit comments