generic

package
v0.0.0-...-5c79d48 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNonceViolation = errors.New("nonce-violation")

ErrNonceViolation is the error returned by FastUpdateNonceProtectedResource when a nonce-protected update fails due to concurrent modification. This error should be caught and re-mapped into an appropriate user-facing message for the given resource type.

Functions

func FastUpdateNonceProtectedResource

func FastUpdateNonceProtectedResource[T nonceProtectedResource](ctx context.Context, bk backend.Backend, key []byte, resource T) error

FastUpdateNonceProtectedResource is a helper for updating a resource that is protected by a nonce. The target resource must store its nonce value in a top-level 'nonce' field in order for correct nonce semantics to be observed.

Types

type MarshalFunc

type MarshalFunc[T Resource] func(T, ...services.MarshalOption) ([]byte, error)

MarshalFunc is a type signature for a marshaling function.

type Resource

type Resource interface {
	GetName() string
}

Resource represents a Teleport resource that may be generically persisted into the backend.

type Service

type Service[T Resource] struct {
	// contains filtered or unexported fields
}

Service is a generic service for interacting with resources in the backend.

func NewService

func NewService[T Resource](cfg *ServiceConfig[T]) (*Service[T], error)

NewService will return a new generic service with the given config. This will panic if the configuration is invalid.

func (*Service[T]) CreateResource

func (s *Service[T]) CreateResource(ctx context.Context, resource T) (T, error)

CreateResource creates a new resource.

func (*Service[T]) DeleteAllResources

func (s *Service[T]) DeleteAllResources(ctx context.Context) error

DeleteAllResources removes all resources.

func (*Service[T]) DeleteResource

func (s *Service[T]) DeleteResource(ctx context.Context, name string) error

DeleteResource removes the specified resource.

func (*Service[T]) GetResource

func (s *Service[T]) GetResource(ctx context.Context, name string) (resource T, err error)

GetResource returns the specified resource.

func (*Service[T]) GetResources

func (s *Service[T]) GetResources(ctx context.Context) ([]T, error)

GetResources returns a list of all resources.

func (*Service[T]) ListResources

func (s *Service[T]) ListResources(ctx context.Context, pageSize int, pageToken string) ([]T, string, error)

ListResources returns a paginated list of resources.

func (*Service[T]) ListResourcesReturnNextResource

func (s *Service[T]) ListResourcesReturnNextResource(ctx context.Context, pageSize int, pageToken string) ([]T, *T, error)

ListResourcesReturnNextResource returns a paginated list of resources. The next resource is returned, which allows consumers to construct the next pagination key as appropriate.

func (*Service[T]) MakeBackendItem

func (s *Service[T]) MakeBackendItem(resource T, name string) (backend.Item, error)

MakeBackendItem will check and make the backend item.

func (*Service[T]) MakeKey

func (s *Service[T]) MakeKey(name string) []byte

MakeKey will make a key for the service given a name.

func (*Service[T]) RunWhileLocked

func (s *Service[T]) RunWhileLocked(ctx context.Context, lockName string, ttl time.Duration, fn func(context.Context, backend.Backend) error) error

RunWhileLocked will run the given function in a backend lock. This is a wrapper around the backend.RunWhileLocked function.

func (*Service[T]) UpdateAndSwapResource

func (s *Service[T]) UpdateAndSwapResource(ctx context.Context, name string, modify func(T) error) (T, error)

UpdateAndSwapResource will get the resource from the backend, modify it, and swap the new value into the backend.

func (*Service[T]) UpdateResource

func (s *Service[T]) UpdateResource(ctx context.Context, resource T) (T, error)

UpdateResource updates an existing resource.

func (*Service[T]) UpsertResource

func (s *Service[T]) UpsertResource(ctx context.Context, resource T) (T, error)

UpsertResource upserts a resource.

func (*Service[T]) WithPrefix

func (s *Service[T]) WithPrefix(parts ...string) *Service[T]

WithPrefix will return a service with the given parts appended to the backend prefix.

type ServiceConfig

type ServiceConfig[T Resource] struct {
	Backend       backend.Backend
	ResourceKind  string
	PageLimit     uint
	BackendPrefix string
	MarshalFunc   MarshalFunc[T]
	UnmarshalFunc UnmarshalFunc[T]
}

ServiceConfig is the configuration for the service configuration.

func (*ServiceConfig[T]) CheckAndSetDefaults

func (c *ServiceConfig[T]) CheckAndSetDefaults() error

type UnmarshalFunc

type UnmarshalFunc[T Resource] func([]byte, ...services.MarshalOption) (T, error)

UnmarshalFunc is a type signature for an unmarshalling function.

Jump to

Keyboard shortcuts

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