resourcemanager

package
v0.0.0-...-ed06964 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 22 Imported by: 0

README

Resource Manager

ResourceManager provides reusable shared operations among resources. This includes the CRUD operations via HTTP, support for different output encodings (yaml or JSON), and Provisioning.

What is a Resource?

A Resource is, conceptually, any entity that can be created, read, updated and/or deleted. Some examples in Tracetest includes Test, Transaction, PollingProfile, etc.

How this works

A ResourceManager can be instantiated with a few arguments:

  • Resource name, singular and plural,
  • List of enabled operations,
  • Handler

The handler is a class that handles the persistence of entities. It must support at least the required methods for the enabled operations.

For example, a resource that wants to enable the create operation needs to provide a Handler that has a Create method.

The provisioning process is handled by an external Provisioner class, but the ResourceManager instance handles the actual provisioning, meaning the actual persistence of the provisioning data.

Here is a flow of a ResourceManager setup process:

---
title: Instantiating a new ResourceManager
---
stateDiagram-v2

  Setup: Instantiate ResourceManager
  
  app --> Setup
  Setup --> Registering
  
  state Setup {
    validateHandler: check that \n handler implements \n the registered operations
    bind: Bind handler methods to http handlers
    
    [*] --> validateHandler
    validateHandler --> bind
  }

  state Registering {
    registerRoutes: create mux subrouter \n with http handlers
    registerProvisioner: add new ResourceManager \n to provisioner registry
    [*] --> registerRoutes
    registerRoutes --> registerProvisioner
  }

Documentation

Index

Constants

View Source
const HeaderAugmented = "X-Tracetest-Augmented"

Variables

View Source
var ErrTypeNotSupported = errors.New("type not supported")

Functions

func CanBeAugmented

func CanBeAugmented() managerOption

func DisableDelete

func DisableDelete() managerOption

func WithIDGen

func WithIDGen(fn func() id.ID) managerOption

func WithOperations

func WithOperations(ops ...Operation) managerOption

func WithTracer

func WithTracer(tracer trace.Tracer) managerOption

Types

type Create

type Create[T ResourceSpec] interface {
	Create(context.Context, T) (T, error)
	IDSetter[T]
}

type Current

type Current[T ResourceSpec] interface {
	Current(context.Context) (T, error)
}

type Delete

type Delete[T ResourceSpec] interface {
	Delete(context.Context, id.ID) error
}

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func EncoderFromRequest

func EncoderFromRequest(r *http.Request) Encoder

func (Encoder) DecodeRequestBody

func (e Encoder) DecodeRequestBody(out interface{}) (err error)

func (Encoder) RequestContentType

func (e Encoder) RequestContentType() string

func (Encoder) ResponseContentType

func (e Encoder) ResponseContentType() string

func (Encoder) WriteEncodedResponse

func (e Encoder) WriteEncodedResponse(w http.ResponseWriter, code int, data any) error

type Get

type Get[T ResourceSpec] interface {
	Get(context.Context, id.ID) (T, error)
}

type GetAugmented

type GetAugmented[T ResourceSpec] interface {
	GetAugmented(context.Context, id.ID) (T, error)
}

type IDSetter

type IDSetter[T ResourceSpec] interface {
	SetID(T, id.ID) T
}

type List

type List[T ResourceSpec] interface {
	SortableHandler
	List(_ context.Context, take, skip int, query, sortBy, sortDirection string) ([]T, error)
	Count(_ context.Context, query string) (int, error)
}

type ListAugmented

type ListAugmented[T ResourceSpec] interface {
	ListAugmented(_ context.Context, take, skip int, query, sortBy, sortDirection string) ([]T, error)
}

type Manager

type Manager interface {
	EnabledOperations() []Operation
	Handler() any
	RegisterRoutes(*mux.Router) *mux.Router
	Provisioner
}

func New

func New[T ResourceSpec](resourceTypeSingular, resourceTypePlural string, handler any, opts ...managerOption) Manager

type Operation

type Operation string
const (
	OperationNoop   Operation = ""
	OperationList   Operation = "list"
	OperationCreate Operation = "create"
	OperationUpdate Operation = "update"
	OperationGet    Operation = "get"
	OperationDelete Operation = "delete"

	OperationGetAugmented  Operation = "getAugmented"
	OperationListAugmented Operation = "listAugmented"
)

type Provision

type Provision[T ResourceSpec] interface {
	Provision(context.Context, T) error
	IDSetter[T]
}

type Provisioner

type Provisioner interface {
	Provision(_ context.Context, values map[string]any) error
}

type Resource

type Resource[T ResourceSpec] struct {
	Type string `json:"type"`
	Spec T      `json:"spec"`
}

type ResourceList

type ResourceList[T ResourceSpec] struct {
	Count int           `json:"count" yamlstream:"count"`
	Items []Resource[T] `json:"items" yamlstream:"items"`
}

type ResourceSpec

type ResourceSpec interface {
	HasID() bool
	GetID() id.ID
	Validate() error
}

type SortableHandler

type SortableHandler interface {
	SortingFields() []string
}

type Update

type Update[T ResourceSpec] interface {
	Update(context.Context, T) (T, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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