|
9 | 9 | "io"
|
10 | 10 | "net/http"
|
11 | 11 | "reflect"
|
| 12 | + "regexp" |
12 | 13 | "slices"
|
| 14 | + "strconv" |
13 | 15 | "strings"
|
14 | 16 | "sync"
|
15 | 17 | "time"
|
@@ -41,11 +43,12 @@ var (
|
41 | 43 | )
|
42 | 44 |
|
43 | 45 | var (
|
44 |
| - ErrParameterRequired = errors.New("parameter is required") |
45 |
| - ErrServerNotFound = errors.New("server not found") |
46 |
| - ErrServerExists = errors.New("server already exists") |
47 |
| - ErrNotSupported = errors.New("not supported") |
48 |
| - ErrInvalidTimeout = errors.New("invalid timeout") |
| 46 | + ErrParameterRequired = errors.New("parameter is required") |
| 47 | + ErrServerNotFound = errors.New("server not found") |
| 48 | + ErrServerExists = errors.New("server already exists") |
| 49 | + ErrNotSupported = errors.New("not supported") |
| 50 | + ErrInvalidTimeout = errors.New("invalid timeout") |
| 51 | + ErrPlusVersionNotFound = errors.New("plus version not found in the input string") |
49 | 52 | )
|
50 | 53 |
|
51 | 54 | // NginxClient lets you access NGINX Plus API.
|
@@ -193,6 +196,20 @@ type NginxInfo struct {
|
193 | 196 | ParentProcessID uint64 `json:"ppid"`
|
194 | 197 | }
|
195 | 198 |
|
| 199 | +// LicenseReporting contains information about license status for NGINX Plus. |
| 200 | +type LicenseReporting struct { |
| 201 | + Healthy bool |
| 202 | + Fails uint64 |
| 203 | + Grace uint64 |
| 204 | +} |
| 205 | + |
| 206 | +// NginxLicense contains licensing information about NGINX Plus. |
| 207 | +type NginxLicense struct { |
| 208 | + ActiveTill uint64 `json:"active_till"` |
| 209 | + Eval bool |
| 210 | + Reporting LicenseReporting |
| 211 | +} |
| 212 | + |
196 | 213 | // Caches is a map of cache stats by cache zone.
|
197 | 214 | type Caches = map[string]HTTPCache
|
198 | 215 |
|
@@ -1553,6 +1570,30 @@ func (client *NginxClient) GetNginxInfo(ctx context.Context) (*NginxInfo, error)
|
1553 | 1570 | return &info, nil
|
1554 | 1571 | }
|
1555 | 1572 |
|
| 1573 | +// GetNginxLicense returns Nginx License data with a context. |
| 1574 | +func (client *NginxClient) GetNginxLicense(ctx context.Context) (*NginxLicense, error) { |
| 1575 | + var data NginxLicense |
| 1576 | + |
| 1577 | + info, err := client.GetNginxInfo(ctx) |
| 1578 | + if err != nil { |
| 1579 | + return nil, fmt.Errorf("failed to get nginx info: %w", err) |
| 1580 | + } |
| 1581 | + release, err := extractPlusVersionValues(info.Build) |
| 1582 | + if err != nil { |
| 1583 | + return nil, fmt.Errorf("failed to get nginx plus release: %w", err) |
| 1584 | + } |
| 1585 | + |
| 1586 | + if (client.apiVersion < 9) || (release < 33) { |
| 1587 | + return &data, nil |
| 1588 | + } |
| 1589 | + |
| 1590 | + err = client.get(ctx, "license", &data) |
| 1591 | + if err != nil { |
| 1592 | + return nil, fmt.Errorf("failed to get license: %w", err) |
| 1593 | + } |
| 1594 | + return &data, nil |
| 1595 | +} |
| 1596 | + |
1556 | 1597 | // GetCaches returns Cache stats with a context.
|
1557 | 1598 | func (client *NginxClient) GetCaches(ctx context.Context) (*Caches, error) {
|
1558 | 1599 | var caches Caches
|
@@ -1988,3 +2029,22 @@ func (client *NginxClient) GetWorkers(ctx context.Context) ([]*Workers, error) {
|
1988 | 2029 | }
|
1989 | 2030 | return workers, nil
|
1990 | 2031 | }
|
| 2032 | + |
| 2033 | +var rePlus = regexp.MustCompile(`-r(\d+)`) |
| 2034 | + |
| 2035 | +// extractPlusVersionValues. |
| 2036 | +func extractPlusVersionValues(input string) (int, error) { |
| 2037 | + var rValue int |
| 2038 | + matches := rePlus.FindStringSubmatch(input) |
| 2039 | + |
| 2040 | + if len(matches) < 1 { |
| 2041 | + return 0, fmt.Errorf("%w [%s]", ErrPlusVersionNotFound, input) |
| 2042 | + } |
| 2043 | + |
| 2044 | + rValue, err := strconv.Atoi(matches[1]) |
| 2045 | + if err != nil { |
| 2046 | + return 0, fmt.Errorf("failed to convert NGINX Plus release to integer: %w", err) |
| 2047 | + } |
| 2048 | + |
| 2049 | + return rValue, nil |
| 2050 | +} |
0 commit comments