blob: 25aa702e8ade1c4bbcb32c0ed693fae65be0b07f [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +00001package api
2
3import (
4 "io/ioutil"
5 "strings"
6 "time"
7)
8
9type License struct {
10 // The unique identifier of the license
11 LicenseID string `json:"license_id"`
12
13 // The customer ID associated with the license
14 CustomerID string `json:"customer_id"`
15
16 // If set, an identifier that should be used to lock the license to a
17 // particular site, cluster, etc.
18 InstallationID string `json:"installation_id"`
19
20 // The time at which the license was issued
21 IssueTime time.Time `json:"issue_time"`
22
23 // The time at which the license starts being valid
24 StartTime time.Time `json:"start_time"`
25
26 // The time after which the license expires
27 ExpirationTime time.Time `json:"expiration_time"`
28
29 // The time at which the license ceases to function and can
30 // no longer be used in any capacity
31 TerminationTime time.Time `json:"termination_time"`
32
33 // The product the license is valid for
34 Product string `json:"product"`
35
36 // License Specific Flags
37 Flags map[string]interface{} `json:"flags"`
38
39 // List of features enabled by the license
40 Features []string `json:"features"`
41}
42
43type LicenseReply struct {
44 Valid bool
45 License *License
46 Warnings []string
47}
48
49func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, error) {
50 var reply LicenseReply
51 if _, err := op.c.query("/v1/operator/license", &reply, q); err != nil {
52 return nil, err
53 } else {
54 return &reply, nil
55 }
56}
57
58func (op *Operator) LicenseGetSigned(q *QueryOptions) (string, error) {
59 r := op.c.newRequest("GET", "/v1/operator/license")
60 r.params.Set("signed", "1")
61 r.setQueryOptions(q)
62 _, resp, err := requireOK(op.c.doRequest(r))
63 if err != nil {
64 return "", err
65 }
66 defer resp.Body.Close()
67
68 data, err := ioutil.ReadAll(resp.Body)
69 if err != nil {
70 return "", err
71 }
72
73 return string(data), nil
74}
75
76// LicenseReset will reset the license to the builtin one if it is still valid.
77// If the builtin license is invalid, the current license stays active.
78func (op *Operator) LicenseReset(opts *WriteOptions) (*LicenseReply, error) {
79 var reply LicenseReply
80 r := op.c.newRequest("DELETE", "/v1/operator/license")
81 r.setWriteOptions(opts)
82 _, resp, err := requireOK(op.c.doRequest(r))
83 if err != nil {
84 return nil, err
85 }
86 defer resp.Body.Close()
87
88 if err := decodeBody(resp, &reply); err != nil {
89 return nil, err
90 }
91
92 return &reply, nil
93}
94
95func (op *Operator) LicensePut(license string, opts *WriteOptions) (*LicenseReply, error) {
96 var reply LicenseReply
97 r := op.c.newRequest("PUT", "/v1/operator/license")
98 r.setWriteOptions(opts)
99 r.body = strings.NewReader(license)
100 _, resp, err := requireOK(op.c.doRequest(r))
101 if err != nil {
102 return nil, err
103 }
104 defer resp.Body.Close()
105
106 if err := decodeBody(resp, &reply); err != nil {
107 return nil, err
108 }
109
110 return &reply, nil
111}