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 closeResponseBody(resp)
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.
81//
82// DEPRECATED: Consul 1.10 removes the corresponding HTTP endpoint as licenses
83// are now set via agent configuration instead of through the API
84func (op *Operator) LicenseReset(opts *WriteOptions) (*LicenseReply, error) {
85	var reply LicenseReply
86	r := op.c.newRequest("DELETE", "/v1/operator/license")
87	r.setWriteOptions(opts)
88	_, resp, err := requireOK(op.c.doRequest(r))
89	if err != nil {
90		return nil, err
91	}
92	defer resp.Body.Close()
93	if err := decodeBody(resp, &reply); err != nil {
94		return nil, err
95	}
96	return &reply, nil
97}
98
99// LicensePut will configure the Consul Enterprise license for the target datacenter
100//
101// DEPRECATED: Consul 1.10 removes the corresponding HTTP endpoint as licenses
102// are now set via agent configuration instead of through the API
103func (op *Operator) LicensePut(license string, opts *WriteOptions) (*LicenseReply, error) {
104	var reply LicenseReply
105	r := op.c.newRequest("PUT", "/v1/operator/license")
106	r.setWriteOptions(opts)
107	r.body = strings.NewReader(license)
108	_, resp, err := requireOK(op.c.doRequest(r))
109	if err != nil {
110		return nil, err
111	}
112	defer resp.Body.Close()
113
114	if err := decodeBody(resp, &reply); err != nil {
115		return nil, err
116	}
117
118	return &reply, nil
119}
120