1package 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	// Modules is a list of the licensed enterprise modules
40	Modules []string `json:"modules"`
41
42	// List of features enabled by the license
43	Features []string `json:"features"`
44}
45
46type LicenseReply struct {
47	Valid    bool
48	License  *License
49	Warnings []string
50}
51
52func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, error) {
53	var reply LicenseReply
54	if _, err := op.c.query("/v1/operator/license", &reply, q); err != nil {
55		return nil, err
56	} else {
57		return &reply, nil
58	}
59}
60
61func (op *Operator) LicenseGetSigned(q *QueryOptions) (string, error) {
62	r := op.c.newRequest("GET", "/v1/operator/license")
63	r.params.Set("signed", "1")
64	r.setQueryOptions(q)
65	_, resp, err := requireOK(op.c.doRequest(r))
66	if err != nil {
67		return "", err
68	}
69	defer resp.Body.Close()
70
71	data, err := ioutil.ReadAll(resp.Body)
72	if err != nil {
73		return "", err
74	}
75
76	return string(data), nil
77}
78
79// LicenseReset will reset the license to the builtin one if it is still valid.
80// If the builtin license is invalid, the current license stays active.
81func (op *Operator) LicenseReset(opts *WriteOptions) (*LicenseReply, error) {
82	var reply LicenseReply
83	r := op.c.newRequest("DELETE", "/v1/operator/license")
84	r.setWriteOptions(opts)
85	_, resp, err := requireOK(op.c.doRequest(r))
86	if err != nil {
87		return nil, err
88	}
89	defer resp.Body.Close()
90
91	if err := decodeBody(resp, &reply); err != nil {
92		return nil, err
93	}
94
95	return &reply, nil
96}
97
98func (op *Operator) LicensePut(license string, opts *WriteOptions) (*LicenseReply, error) {
99	var reply LicenseReply
100	r := op.c.newRequest("PUT", "/v1/operator/license")
101	r.setWriteOptions(opts)
102	r.body = strings.NewReader(license)
103	_, resp, err := requireOK(op.c.doRequest(r))
104	if err != nil {
105		return nil, err
106	}
107	defer resp.Body.Close()
108
109	if err := decodeBody(resp, &reply); err != nil {
110		return nil, err
111	}
112
113	return &reply, nil
114}
115