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 := op.c.doRequest(r)
66	if err != nil {
67		return "", err
68	}
69	defer closeResponseBody(resp)
70	if err := requireOK(resp); err != nil {
71		return "", err
72	}
73
74	data, err := ioutil.ReadAll(resp.Body)
75	if err != nil {
76		return "", err
77	}
78
79	return string(data), nil
80}
81
82// LicenseReset will reset the license to the builtin one if it is still valid.
83// If the builtin license is invalid, the current license stays active.
84//
85// DEPRECATED: Consul 1.10 removes the corresponding HTTP endpoint as licenses
86// are now set via agent configuration instead of through the API
87func (op *Operator) LicenseReset(opts *WriteOptions) (*LicenseReply, error) {
88	var reply LicenseReply
89	r := op.c.newRequest("DELETE", "/v1/operator/license")
90	r.setWriteOptions(opts)
91	_, resp, err := op.c.doRequest(r)
92	if err != nil {
93		return nil, err
94	}
95	defer closeResponseBody(resp)
96	if err := requireOK(resp); err != nil {
97		return nil, err
98	}
99	if err := decodeBody(resp, &reply); err != nil {
100		return nil, err
101	}
102	return &reply, nil
103}
104
105// LicensePut will configure the Consul Enterprise license for the target datacenter
106//
107// DEPRECATED: Consul 1.10 removes the corresponding HTTP endpoint as licenses
108// are now set via agent configuration instead of through the API
109func (op *Operator) LicensePut(license string, opts *WriteOptions) (*LicenseReply, error) {
110	var reply LicenseReply
111	r := op.c.newRequest("PUT", "/v1/operator/license")
112	r.setWriteOptions(opts)
113	r.body = strings.NewReader(license)
114	_, resp, err := op.c.doRequest(r)
115	if err != nil {
116		return nil, err
117	}
118	defer closeResponseBody(resp)
119	if err := requireOK(resp); err != nil {
120		return nil, err
121	}
122
123	if err := decodeBody(resp, &reply); err != nil {
124		return nil, err
125	}
126
127	return &reply, nil
128}
129