2023-03-28 21:12:41 +01:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-08-02 16:20:38 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-11-10 10:26:01 -06:00
|
|
|
"io"
|
2021-08-31 09:36:35 -07:00
|
|
|
"strings"
|
2019-08-02 16:20:38 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type License struct {
|
|
|
|
// The unique identifier of the license
|
|
|
|
LicenseID string `json:"license_id"`
|
|
|
|
|
|
|
|
// The customer ID associated with the license
|
|
|
|
CustomerID string `json:"customer_id"`
|
|
|
|
|
|
|
|
// If set, an identifier that should be used to lock the license to a
|
|
|
|
// particular site, cluster, etc.
|
|
|
|
InstallationID string `json:"installation_id"`
|
|
|
|
|
|
|
|
// The time at which the license was issued
|
|
|
|
IssueTime time.Time `json:"issue_time"`
|
|
|
|
|
|
|
|
// The time at which the license starts being valid
|
|
|
|
StartTime time.Time `json:"start_time"`
|
|
|
|
|
|
|
|
// The time after which the license expires
|
|
|
|
ExpirationTime time.Time `json:"expiration_time"`
|
|
|
|
|
|
|
|
// The time at which the license ceases to function and can
|
|
|
|
// no longer be used in any capacity
|
|
|
|
TerminationTime time.Time `json:"termination_time"`
|
|
|
|
|
2023-02-23 13:06:09 -06:00
|
|
|
// Whether the license will ignore termination
|
|
|
|
IgnoreTermination bool `json:"ignore_termination"`
|
|
|
|
|
2019-08-02 16:20:38 -04:00
|
|
|
// The product the license is valid for
|
|
|
|
Product string `json:"product"`
|
|
|
|
|
|
|
|
// License Specific Flags
|
|
|
|
Flags map[string]interface{} `json:"flags"`
|
|
|
|
|
2020-04-17 13:39:27 -04:00
|
|
|
// Modules is a list of the licensed enterprise modules
|
|
|
|
Modules []string `json:"modules"`
|
|
|
|
|
2019-08-02 16:20:38 -04:00
|
|
|
// List of features enabled by the license
|
|
|
|
Features []string `json:"features"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LicenseReply struct {
|
|
|
|
Valid bool
|
|
|
|
License *License
|
|
|
|
Warnings []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, error) {
|
|
|
|
var reply LicenseReply
|
|
|
|
if _, err := op.c.query("/v1/operator/license", &reply, q); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return &reply, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *Operator) LicenseGetSigned(q *QueryOptions) (string, error) {
|
|
|
|
r := op.c.newRequest("GET", "/v1/operator/license")
|
|
|
|
r.params.Set("signed", "1")
|
|
|
|
r.setQueryOptions(q)
|
2021-10-28 21:54:23 +05:30
|
|
|
_, resp, err := op.c.doRequest(r)
|
2019-08-02 16:20:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-06-14 18:49:32 -04:00
|
|
|
defer closeResponseBody(resp)
|
2021-10-28 21:54:23 +05:30
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-08-02 16:20:38 -04:00
|
|
|
|
2022-11-10 10:26:01 -06:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2019-08-02 16:20:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:24:02 -04:00
|
|
|
// LicenseReset will reset the license to the builtin one if it is still valid.
|
|
|
|
// If the builtin license is invalid, the current license stays active.
|
2021-05-10 10:15:35 -04:00
|
|
|
//
|
|
|
|
// DEPRECATED: Consul 1.10 removes the corresponding HTTP endpoint as licenses
|
|
|
|
// are now set via agent configuration instead of through the API
|
2021-08-31 09:36:35 -07:00
|
|
|
func (op *Operator) LicenseReset(opts *WriteOptions) (*LicenseReply, error) {
|
|
|
|
var reply LicenseReply
|
|
|
|
r := op.c.newRequest("DELETE", "/v1/operator/license")
|
|
|
|
r.setWriteOptions(opts)
|
2021-10-28 21:54:23 +05:30
|
|
|
_, resp, err := op.c.doRequest(r)
|
2021-08-31 09:36:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-28 21:54:23 +05:30
|
|
|
defer closeResponseBody(resp)
|
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-31 09:36:35 -07:00
|
|
|
if err := decodeBody(resp, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &reply, nil
|
2019-08-12 15:24:02 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 10:15:35 -04:00
|
|
|
// LicensePut will configure the Consul Enterprise license for the target datacenter
|
|
|
|
//
|
|
|
|
// DEPRECATED: Consul 1.10 removes the corresponding HTTP endpoint as licenses
|
|
|
|
// are now set via agent configuration instead of through the API
|
2021-08-31 09:36:35 -07:00
|
|
|
func (op *Operator) LicensePut(license string, opts *WriteOptions) (*LicenseReply, error) {
|
|
|
|
var reply LicenseReply
|
|
|
|
r := op.c.newRequest("PUT", "/v1/operator/license")
|
|
|
|
r.setWriteOptions(opts)
|
|
|
|
r.body = strings.NewReader(license)
|
2021-10-28 21:54:23 +05:30
|
|
|
_, resp, err := op.c.doRequest(r)
|
2021-08-31 09:36:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-28 21:54:23 +05:30
|
|
|
defer closeResponseBody(resp)
|
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-31 09:36:35 -07:00
|
|
|
|
|
|
|
if err := decodeBody(resp, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &reply, nil
|
2019-08-02 16:20:38 -04:00
|
|
|
}
|