etcdlight

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package etcdlight is a lightweight implementation of the v2 etcd API. This package is here because the module the official Etcd client package is in has a lot of dependencies and we wish to keep sebase depencency light.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsErrorCode

func IsErrorCode(err error, code ...int) bool

IsErrorCode returns true if err is of type *ErrorResponse and the ErrorCode field matches one of the codes given.

Types

type ErrorResponse

type ErrorResponse struct {
	// StatusCode is set from the HTTP request.
	StatusCode int

	// ErrorCode is returned from Etcd. It will be 0 if unset or JSON decoding failed.
	ErrorCode int
	// Message is returned from Etcd, but if JSON decoding failed it contains the
	// HTTP body instead.
	Message string

	// Index is the Etcd index returned as part of the error from Etcd.
	Index uint64
}

Type ErrorResponse is used to report errors from Etcd.

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

type KAPI

type KAPI struct {
	Client  *http.Client
	BaseURL *url.URL
}

func NewKAPI

func NewKAPI(burl string) (*KAPI, error)

NewKAPI returns a *KAPI with http.DefaultClient and the given URL. It returns errors from url.Parse unwrapped.

func (*KAPI) Get

func (cl *KAPI) Get(ctx context.Context, key string, recursive bool) (*Response, error)

Get retrieves values. Only the current (latest) values can be retrieved. If recursive is true multiple key-values might be retrieved. Key should start with /. Errors from http.Client.Get and json.DecoderDecode are returned unwrapped. Returns a *ErrorResponse error if the HTTP status code was not 200 or 201.

func (*KAPI) MkDir

func (cl *KAPI) MkDir(ctx context.Context, key string, ttl time.Duration) error

MkDir creates a directory at key, possibly with a ttl. Pass 0 as ttl to not expire. Returns errors from http.PostForm unwrapped. Returns a *ErrorResponse error if the HTTP status code was not 200 or 201. Key should start with /.

func (*KAPI) RefreshDir

func (cl *KAPI) RefreshDir(ctx context.Context, key string, ttl time.Duration) error

RefreshDir updates the ttl for a directory. It must already exist. Returns errors from http.PostForm unwrapped. Returns a *ErrorResponse error if the HTTP status code was not 200 or 201. Key should start with /.

func (*KAPI) RmDir

func (cl *KAPI) RmDir(ctx context.Context, key string, recursive bool) error

RmDir removes the directory given, possibly recursively. Returns errors from http.Client.Do unwrapped. Returns a *ErrorResponse error if the HTTP status code was not 200 or 201. Key should start with /.

func (*KAPI) Set

func (cl *KAPI) Set(ctx context.Context, key string, value string, exclusive bool, ttl time.Duration) error

Set sets the value at key, possibly with a ttl. Pass 0 as ttl to not expire. If exclusive is true then Set will fail if the key already exists. Key should start with /. Returns errors from http.PostForm unwrapped. Returns a *ErrorResponse error if the HTTP status code was not 200 or 201.

func (*KAPI) URLForKey

func (cl *KAPI) URLForKey(key string) *url.URL

URLForKey returns a copy of cl.BaseURL with key appended to the path.

func (*KAPI) Watch

func (cl *KAPI) Watch(key string, recursive bool, afterIndex uint64) Watcher

Watch creates a watcher that can be used repeatedly to watch for changes. It will watch for changes after afterIndex. If 0 is used for that argument it will watch for the next future change from the time Next is called. This call doesn't do any network request, it only creates the Watcher object. Call Next to do the actual request. Key should start with /.

type KV

type KV struct {
	// Key is the full path of the key, starting with /.
	Key   string
	Value string

	ModifiedIndex uint64
}

Type KV represents a value in etcd. Only leaf values can be represented.

type Response

type Response struct {
	// The action that was taken, such as "update".
	Action string

	// Max index encountered in either header or parsed ModifiedIndex fields.
	MaxIndex uint64

	// Values matching the given key.
	Values []KV

	// PrevValues are currenly only set by Watch. It contains previous
	// values on updates.
	PrevValues []KV
}

Type Response is a parsed successful response from etcd.

func ReadResponse

func ReadResponse(resp *http.Response, key string) (*Response, error)

ReadResponse parses a response from etcd. If the HTTP response code is not 200 or 201, an *ErrorResponse error is returned. If key is empty string body is decoded but not inspected. If key is not empty string, any non-directory nodes with that prefix is returned in values. This function consumes and closes resp.Body. In addition to *ErrorResponse, it might return errors from reading the body or json.Unmarshal.

type Watcher

type Watcher interface {
	Next(ctx context.Context) (*Response, error)
}

Type Watcher is the watcher interface. Each call to Next does a new request waiting for the next value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL