2022-07-06 19:29:46 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-26 18:03:56 +00:00
|
|
|
ErrVariableNotFound = "variable not found"
|
|
|
|
ErrVariableMissingItems = "variable missing Items field"
|
2022-07-06 19:29:46 +00:00
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Variables is used to access variables.
|
|
|
|
type Variables struct {
|
2022-07-06 19:29:46 +00:00
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Variables returns a new handle on the variables.
|
|
|
|
func (c *Client) Variables() *Variables {
|
|
|
|
return &Variables{client: c}
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Create is used to create a variable.
|
|
|
|
func (sv *Variables) Create(v *Variable, qo *WriteOptions) (*Variable, *WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
v.Path = cleanPathString(v.Path)
|
2022-08-26 18:03:56 +00:00
|
|
|
var out Variable
|
2022-08-25 21:38:15 +00:00
|
|
|
wm, err := sv.client.write("/v1/var/"+v.Path, v, &out, qo)
|
2022-07-06 19:29:46 +00:00
|
|
|
if err != nil {
|
2022-08-25 21:38:15 +00:00
|
|
|
return nil, wm, err
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
2022-08-25 21:38:15 +00:00
|
|
|
return &out, wm, nil
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// CheckedCreate is used to create a variable if it doesn't exist
|
2022-07-06 19:29:46 +00:00
|
|
|
// already. If it does, it will return a ErrCASConflict that can be unwrapped
|
|
|
|
// for more details.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) CheckedCreate(v *Variable, qo *WriteOptions) (*Variable, *WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
v.Path = cleanPathString(v.Path)
|
2022-08-26 18:03:56 +00:00
|
|
|
var out Variable
|
2022-08-25 21:38:15 +00:00
|
|
|
wm, err := sv.writeChecked("/v1/var/"+v.Path+"?cas=0", v, &out, qo)
|
2022-07-06 19:29:46 +00:00
|
|
|
if err != nil {
|
2022-08-25 21:38:15 +00:00
|
|
|
return nil, wm, err
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
return &out, wm, nil
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Read is used to query a single variable by path. This will error
|
2022-07-06 19:29:46 +00:00
|
|
|
// if the variable is not found.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) Read(path string, qo *QueryOptions) (*Variable, *QueryMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
path = cleanPathString(path)
|
2022-08-26 18:03:56 +00:00
|
|
|
var svar = new(Variable)
|
2022-07-06 19:29:46 +00:00
|
|
|
qm, err := sv.readInternal("/v1/var/"+path, &svar, qo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if svar == nil {
|
|
|
|
return nil, qm, errors.New(ErrVariableNotFound)
|
|
|
|
}
|
|
|
|
return svar, qm, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Peek is used to query a single variable by path, but does not error
|
2022-07-06 19:29:46 +00:00
|
|
|
// when the variable is not found
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) Peek(path string, qo *QueryOptions) (*Variable, *QueryMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
path = cleanPathString(path)
|
2022-08-26 18:03:56 +00:00
|
|
|
var svar = new(Variable)
|
2022-07-06 19:29:46 +00:00
|
|
|
qm, err := sv.readInternal("/v1/var/"+path, &svar, qo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return svar, qm, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Update is used to update a variable.
|
|
|
|
func (sv *Variables) Update(v *Variable, qo *WriteOptions) (*Variable, *WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
v.Path = cleanPathString(v.Path)
|
2022-08-26 18:03:56 +00:00
|
|
|
var out Variable
|
2022-08-25 21:38:15 +00:00
|
|
|
|
|
|
|
wm, err := sv.client.write("/v1/var/"+v.Path, v, &out, qo)
|
2022-07-06 19:29:46 +00:00
|
|
|
if err != nil {
|
2022-08-25 21:38:15 +00:00
|
|
|
return nil, wm, err
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
2022-08-25 21:38:15 +00:00
|
|
|
return &out, wm, nil
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// CheckedUpdate is used to updated a variable if the modify index
|
2022-07-06 19:29:46 +00:00
|
|
|
// matches the one on the server. If it does not, it will return an
|
|
|
|
// ErrCASConflict that can be unwrapped for more details.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) CheckedUpdate(v *Variable, qo *WriteOptions) (*Variable, *WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
v.Path = cleanPathString(v.Path)
|
2022-08-26 18:03:56 +00:00
|
|
|
var out Variable
|
2022-08-25 21:38:15 +00:00
|
|
|
wm, err := sv.writeChecked("/v1/var/"+v.Path+"?cas="+fmt.Sprint(v.ModifyIndex), v, &out, qo)
|
2022-07-06 19:29:46 +00:00
|
|
|
if err != nil {
|
2022-08-25 21:38:15 +00:00
|
|
|
return nil, wm, err
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
return &out, wm, nil
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Delete is used to delete a variable
|
|
|
|
func (sv *Variables) Delete(path string, qo *WriteOptions) (*WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
path = cleanPathString(path)
|
|
|
|
wm, err := sv.deleteInternal(path, qo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// CheckedDelete is used to conditionally delete a variable. If the
|
2022-07-06 19:29:46 +00:00
|
|
|
// existing variable does not match the provided checkIndex, it will return an
|
|
|
|
// ErrCASConflict that can be unwrapped for more details.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) CheckedDelete(path string, checkIndex uint64, qo *WriteOptions) (*WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
path = cleanPathString(path)
|
|
|
|
wm, err := sv.deleteChecked(path, checkIndex, qo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// List is used to dump all of the variables, can be used to pass prefix
|
2022-07-06 19:29:46 +00:00
|
|
|
// via QueryOptions rather than as a parameter
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) List(qo *QueryOptions) ([]*VariableMetadata, *QueryMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var resp []*VariableMetadata
|
2022-07-06 19:29:46 +00:00
|
|
|
qm, err := sv.client.query("/v1/vars", &resp, qo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// PrefixList is used to do a prefix List search over variables.
|
|
|
|
func (sv *Variables) PrefixList(prefix string, qo *QueryOptions) ([]*VariableMetadata, *QueryMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
if qo == nil {
|
|
|
|
qo = &QueryOptions{Prefix: prefix}
|
|
|
|
} else {
|
|
|
|
qo.Prefix = prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return sv.List(qo)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// GetItems returns the inner Items collection from a variable at a
|
2022-07-06 19:29:46 +00:00
|
|
|
// given path
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) GetItems(path string, qo *QueryOptions) (*VariableItems, *QueryMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
path = cleanPathString(path)
|
2022-08-26 18:03:56 +00:00
|
|
|
svar := new(Variable)
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
qm, err := sv.readInternal("/v1/var/"+path, &svar, qo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &svar.Items, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readInternal exists because the API's higher-level read method requires
|
2022-09-22 17:15:05 +00:00
|
|
|
// the status code to be 200 (OK). For Peek(), we do not consider 403 (Permission
|
|
|
|
// Denied or 404 (Not Found) an error, this function just returns a nil in those
|
|
|
|
// cases.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) readInternal(endpoint string, out **Variable, q *QueryOptions) (*QueryMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
r, err := sv.client.newRequest("GET", endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
|
2022-09-22 17:15:05 +00:00
|
|
|
checkFn := requireStatusIn(http.StatusOK, http.StatusNotFound, http.StatusForbidden)
|
2022-07-06 19:29:46 +00:00
|
|
|
rtt, resp, err := checkFn(sv.client.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
*out = nil
|
|
|
|
resp.Body.Close()
|
|
|
|
return qm, nil
|
|
|
|
}
|
|
|
|
|
2022-09-22 17:15:05 +00:00
|
|
|
if resp.StatusCode == http.StatusForbidden {
|
|
|
|
*out = nil
|
|
|
|
resp.Body.Close()
|
|
|
|
// On a 403, there is no QueryMeta to parse, but consul-template--the
|
|
|
|
// main consumer of the Peek() func that calls this method needs the
|
|
|
|
// value to be non-zero; so set them to a reasonable but artificial
|
|
|
|
// value. Index 1 doesn't say anything about the cluster, and there
|
|
|
|
// has to be a KnownLeader to get a 403.
|
|
|
|
qm.LastIndex = 1
|
|
|
|
qm.KnownLeader = true
|
|
|
|
return qm, nil
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:29:46 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := decodeBody(resp, out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return qm, nil
|
|
|
|
}
|
|
|
|
|
2022-09-22 17:15:05 +00:00
|
|
|
// deleteInternal exists because the API's higher-level delete method requires
|
2022-07-06 19:29:46 +00:00
|
|
|
// the status code to be 200 (OK). The SV HTTP API returns a 204 (No Content)
|
|
|
|
// on success.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) deleteInternal(path string, q *WriteOptions) (*WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
r, err := sv.client.newRequest("DELETE", fmt.Sprintf("/v1/var/%s", path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
|
|
|
|
checkFn := requireStatusIn(http.StatusOK, http.StatusNoContent)
|
|
|
|
rtt, resp, err := checkFn(sv.client.doRequest(r))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
parseWriteMeta(resp, wm)
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteChecked exists because the API's higher-level delete method requires
|
|
|
|
// the status code to be OK. The SV HTTP API returns a 204 (No Content) on
|
|
|
|
// success and a 409 (Conflict) on a CAS error.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) deleteChecked(path string, checkIndex uint64, q *WriteOptions) (*WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
r, err := sv.client.newRequest("DELETE", fmt.Sprintf("/v1/var/%s?cas=%v", path, checkIndex))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
checkFn := requireStatusIn(http.StatusOK, http.StatusNoContent, http.StatusConflict)
|
|
|
|
rtt, resp, err := checkFn(sv.client.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
parseWriteMeta(resp, wm)
|
|
|
|
|
|
|
|
// The only reason we should decode the response body is if
|
|
|
|
// it is a conflict response. Otherwise, there won't be one.
|
|
|
|
if resp.StatusCode == http.StatusConflict {
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
conflict := new(Variable)
|
2022-07-06 19:29:46 +00:00
|
|
|
if err := decodeBody(resp, &conflict); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, ErrCASConflict{
|
|
|
|
Conflict: conflict,
|
|
|
|
CheckIndex: checkIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeChecked exists because the API's higher-level write method requires
|
|
|
|
// the status code to be OK. The SV HTTP API returns a 200 (OK) on
|
|
|
|
// success and a 409 (Conflict) on a CAS error.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) writeChecked(endpoint string, in *Variable, out *Variable, q *WriteOptions) (*WriteMeta, error) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
r, err := sv.client.newRequest("PUT", endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.obj = in
|
|
|
|
|
|
|
|
checkFn := requireStatusIn(http.StatusOK, http.StatusNoContent, http.StatusConflict)
|
|
|
|
rtt, resp, err := checkFn(sv.client.doRequest(r))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-25 21:38:15 +00:00
|
|
|
defer resp.Body.Close()
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
parseWriteMeta(resp, wm)
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusConflict {
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
conflict := new(Variable)
|
2022-07-06 19:29:46 +00:00
|
|
|
if err := decodeBody(resp, &conflict); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, ErrCASConflict{
|
|
|
|
Conflict: conflict,
|
|
|
|
CheckIndex: in.ModifyIndex,
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 21:38:15 +00:00
|
|
|
if out != nil {
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 19:29:46 +00:00
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Variable specifies the metadata and contents to be stored in the
|
2022-07-06 19:29:46 +00:00
|
|
|
// encrypted Nomad backend.
|
2022-08-26 18:03:56 +00:00
|
|
|
type Variable struct {
|
|
|
|
// Namespace is the Nomad namespace associated with the variable
|
2022-09-09 21:55:20 +00:00
|
|
|
Namespace string `hcl:"namespace"`
|
2022-08-26 18:03:56 +00:00
|
|
|
// Path is the path to the variable
|
2022-09-09 21:55:20 +00:00
|
|
|
Path string `hcl:"path"`
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
// Raft indexes to track creation and modification
|
2022-09-09 21:55:20 +00:00
|
|
|
CreateIndex uint64 `hcl:"create_index"`
|
|
|
|
ModifyIndex uint64 `hcl:"modify_index"`
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
// Times provided as a convenience for operators expressed time.UnixNanos
|
2022-09-09 21:55:20 +00:00
|
|
|
CreateTime int64 `hcl:"create_time"`
|
|
|
|
ModifyTime int64 `hcl:"modify_time"`
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
Items VariableItems `hcl:"items"`
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VariableMetadata specifies the metadata for a variable and
|
2022-07-06 19:29:46 +00:00
|
|
|
// is used as the list object
|
2022-08-26 18:03:56 +00:00
|
|
|
type VariableMetadata struct {
|
|
|
|
// Namespace is the Nomad namespace associated with the variable
|
2022-09-09 21:55:20 +00:00
|
|
|
Namespace string `hcl:"namespace"`
|
2022-08-26 18:03:56 +00:00
|
|
|
// Path is the path to the variable
|
2022-09-09 21:55:20 +00:00
|
|
|
Path string `hcl:"path"`
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
// Raft indexes to track creation and modification
|
2022-09-09 21:55:20 +00:00
|
|
|
CreateIndex uint64 `hcl:"create_index"`
|
|
|
|
ModifyIndex uint64 `hcl:"modify_index"`
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
// Times provided as a convenience for operators expressed time.UnixNanos
|
2022-09-09 21:55:20 +00:00
|
|
|
CreateTime int64 `hcl:"create_time"`
|
|
|
|
ModifyTime int64 `hcl:"modify_time"`
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
type VariableItems map[string]string
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// NewVariable is a convenience method to more easily create a
|
|
|
|
// ready-to-use variable
|
|
|
|
func NewVariable(path string) *Variable {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
return &Variable{
|
2022-07-06 19:29:46 +00:00
|
|
|
Path: path,
|
2022-08-26 18:03:56 +00:00
|
|
|
Items: make(VariableItems),
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Copy returns a new deep copy of this Variable
|
|
|
|
func (sv1 *Variable) Copy() *Variable {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var out Variable = *sv1
|
|
|
|
out.Items = make(VariableItems)
|
2022-07-06 19:29:46 +00:00
|
|
|
for k, v := range sv1.Items {
|
|
|
|
out.Items[k] = v
|
|
|
|
}
|
|
|
|
return &out
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Metadata returns the VariableMetadata component of
|
|
|
|
// a Variable. This can be useful for comparing against
|
2022-07-06 19:29:46 +00:00
|
|
|
// a List result.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variable) Metadata() *VariableMetadata {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
return &VariableMetadata{
|
2022-07-06 19:29:46 +00:00
|
|
|
Namespace: sv.Namespace,
|
|
|
|
Path: sv.Path,
|
|
|
|
CreateIndex: sv.CreateIndex,
|
|
|
|
ModifyIndex: sv.ModifyIndex,
|
|
|
|
CreateTime: sv.CreateTime,
|
|
|
|
ModifyTime: sv.ModifyTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// IsZeroValue can be used to test if a Variable has been changed
|
2022-07-06 19:29:46 +00:00
|
|
|
// from the default values it gets at creation
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variable) IsZeroValue() bool {
|
|
|
|
return *sv.Metadata() == VariableMetadata{} && sv.Items == nil
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanPathString removes leading and trailing slashes since they
|
|
|
|
// would trigger go's path cleaning/redirection behavior in the
|
|
|
|
// standard HTTP router
|
|
|
|
func cleanPathString(path string) string {
|
|
|
|
return strings.Trim(path, " /")
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// AsJSON returns the Variable as a JSON-formatted string
|
|
|
|
func (sv Variable) AsJSON() string {
|
2022-07-06 19:29:46 +00:00
|
|
|
var b []byte
|
|
|
|
b, _ = json.Marshal(sv)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// AsPrettyJSON returns the Variable as a JSON-formatted string with
|
2022-07-06 19:29:46 +00:00
|
|
|
// indentation
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv Variable) AsPrettyJSON() string {
|
2022-07-06 19:29:46 +00:00
|
|
|
var b []byte
|
|
|
|
b, _ = json.MarshalIndent(sv, "", " ")
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrCASConflict struct {
|
|
|
|
CheckIndex uint64
|
2022-08-26 18:03:56 +00:00
|
|
|
Conflict *Variable
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrCASConflict) Error() string {
|
|
|
|
return fmt.Sprintf("cas conflict: expected ModifyIndex %v; found %v", e.CheckIndex, e.Conflict.ModifyIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doRequestWrapper is a function that wraps the client's doRequest method
|
|
|
|
// and can be used to provide error and response handling
|
|
|
|
type doRequestWrapper = func(time.Duration, *http.Response, error) (time.Duration, *http.Response, error)
|
|
|
|
|
|
|
|
// requireStatusIn is a doRequestWrapper generator that takes expected HTTP
|
|
|
|
// response codes and validates that the received response code is among them
|
|
|
|
func requireStatusIn(statuses ...int) doRequestWrapper {
|
|
|
|
fn := func(d time.Duration, resp *http.Response, e error) (time.Duration, *http.Response, error) {
|
|
|
|
statuses := statuses
|
|
|
|
if e != nil {
|
|
|
|
if resp != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
return d, nil, e
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, status := range statuses {
|
|
|
|
if resp.StatusCode == status {
|
|
|
|
return d, resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil, generateUnexpectedResponseCodeError(resp)
|
|
|
|
}
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateUnexpectedResponseCodeError creates a standardized error
|
|
|
|
// when the the API client's newRequest method receives an unexpected
|
2022-08-26 18:03:56 +00:00
|
|
|
// HTTP response code when accessing the variable's HTTP API
|
2022-07-06 19:29:46 +00:00
|
|
|
func generateUnexpectedResponseCodeError(resp *http.Response) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
return fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
|
|
|
|
}
|