grizzly

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is used to signal a missing resource
	ErrNotFound = errors.New("not found")

	// ErrNotImplemented signals a feature that is not supported by a provider
	ErrNotImplemented = errors.New("not implemented")

	// ErrHandlerNotFound indicates that no handler was found for a particular resource Kind.
	ErrHandlerNotFound = errors.New("handler not found")
)
View Source
var (
	ResourceAdded      = EventType{ID: "resource-added", Severity: Notice, HumanReadable: "added"}
	ResourceNotChanged = EventType{ID: "resource-not-changed", Severity: Info, HumanReadable: "unchanged"}
	ResourceNotFound   = EventType{ID: "resource-not-found", Severity: Info, HumanReadable: "not found"}
	ResourceUpdated    = EventType{ID: "resource-updated", Severity: Notice, HumanReadable: "updated"}
	ResourcePulled     = EventType{ID: "resource-pulled", Severity: Notice, HumanReadable: "pulled"}
	ResourceFailure    = EventType{ID: "resource-failure", Severity: Error, HumanReadable: "failed"}
)

Functions

func Apply

func Apply(registry Registry, resources Resources, continueOnError bool, eventsRecorder eventsRecorder) error

Apply pushes resources to endpoints

func DetectEnvelope added in v0.4.0

func DetectEnvelope(data any) bool

DetectEnvelope identifies whether this resource is enveloped or not

func Diff

func Diff(registry Registry, resources Resources, onlySpec bool, outputFormat string) error

Diff compares resources to those at the endpoints

func EventToColoredText added in v0.4.1

func EventToColoredText(event Event) string

func EventToPlainText added in v0.4.1

func EventToPlainText(event Event) string

func Export

func Export(registry Registry, exportDir string, resources Resources, onlySpec bool, outputFormat string) error

Export renders Jsonnet resources then saves them to a directory

func Format added in v0.3.1

func Format(registry Registry, resourcePath string, resource *Resource, format string, onlySpec bool) ([]byte, string, string, error)

func Get

func Get(registry Registry, UID string, onlySpec bool, outputFormat string) error

Get retrieves a resource from a remote endpoint using its UID

func List

func List(registry Registry, resources Resources, format string) error

List outputs the keys resources found in resulting json.

func ListRemote added in v0.2.0

func ListRemote(registry Registry, targets []string, format string) error

ListRetmote outputs the keys of remote resources

func Pluraliser added in v0.4.1

func Pluraliser(count int, name string) string

Pluraliser returns a string describing the count of items, with a plural 's' appended if the count of items is greater than one.

func Pull added in v0.2.0

func Pull(registry Registry, resourcePath string, onlySpec bool, outputFormat string, targets []string, continueOnError bool, eventsRecorder eventsRecorder) error

Pull pulls remote resources and stores them in the local file system. The given resourcePath must be a directory, where all resources will be stored. If opts.JSONSpec is true, which is only applicable for dashboards, saves the spec as a JSON file.

func SendError added in v0.4.1

func SendError(w http.ResponseWriter, msg string, err error, code int)

func Serve added in v0.4.0

func Serve(registry Registry, parser WatchParser, resourcePath string, port int, openBrowser, onlySpec bool, outputFormat string) error

Serve starts an HTTP server that can be used to navigate Grizzly resources, as well as allowing visualisation of resources handed to Grizzly. If pure files, they can be saved too.

func Show

func Show(registry Registry, resources Resources, outputFormat string) error

Show displays resources

func Snapshot added in v0.4.1

func Snapshot(registry Registry, resources Resources, expiresSeconds int) error

Snapshot pushes resources to endpoints as snapshots, if supported

func ValidateEnvelope added in v0.4.0

func ValidateEnvelope(data any) error

ValidateEnvelope confirms that this resource is a complete enveloped resource

func Watch

func Watch(registry Registry, watchDir string, parser WatchParser, trailRecorder eventsRecorder) error

Watch watches a directory for changes then pushes Jsonnet resource to endpoints when changes are noticed.

func WriteFile added in v0.3.1

func WriteFile(filename string, content []byte) error

Types

type APIErr

type APIErr struct {
	Err  error
	Body []byte
}

APIErr encapsulates an error from the Grafana API

func (APIErr) Error

func (e APIErr) Error() string

type BaseHandler added in v0.4.0

type BaseHandler struct {
	Provider Provider
	// contains filtered or unexported fields
}

func NewBaseHandler added in v0.4.0

func NewBaseHandler(provider Provider, kind string, usesFolders bool) BaseHandler

func (*BaseHandler) APIVersion added in v0.4.0

func (h *BaseHandler) APIVersion() string

func (*BaseHandler) Detect added in v0.4.0

func (h *BaseHandler) Detect(map[string]any) bool

func (*BaseHandler) GetUID added in v0.4.0

func (h *BaseHandler) GetUID(resource Resource) (string, error)

func (*BaseHandler) Kind added in v0.4.0

func (h *BaseHandler) Kind() string

func (*BaseHandler) Prepare added in v0.4.0

func (h *BaseHandler) Prepare(existing, resource Resource) *Resource

func (*BaseHandler) Sort added in v0.4.0

func (h *BaseHandler) Sort(resources Resources) Resources

func (*BaseHandler) Unprepare added in v0.4.0

func (h *BaseHandler) Unprepare(resource Resource) *Resource

func (*BaseHandler) UsesFolders added in v0.4.0

func (h *BaseHandler) UsesFolders() bool

type ChainParser added in v0.4.1

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

func NewChainParser added in v0.4.1

func NewChainParser(formatParsers []FormatParser, continueOnError bool) *ChainParser

func (*ChainParser) Parse added in v0.4.1

func (parser *ChainParser) Parse(resourcePath string, options ParserOptions) (Resources, error)

type ErrorPrimitiveReached added in v0.4.0

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

ErrorPrimitiveReached occurs when walkJSON reaches the end of nested dicts without finding a valid resource

func (ErrorPrimitiveReached) Error added in v0.4.0

func (e ErrorPrimitiveReached) Error() string

func (ErrorPrimitiveReached) WithContainingObj added in v0.4.0

func (e ErrorPrimitiveReached) WithContainingObj(obj map[string]any, err error) ErrorPrimitiveReached

type Event added in v0.4.1

type Event struct {
	Type        EventType
	ResourceRef string
	Details     string
}

type EventFormatter added in v0.4.1

type EventFormatter func(event Event) string

type EventSeverity added in v0.4.1

type EventSeverity uint8
const (
	Info EventSeverity = iota
	Notice
	Error
)

type EventType added in v0.4.1

type EventType struct {
	Severity      EventSeverity
	ID            string
	HumanReadable string
}

type FilteredParser added in v0.4.1

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

func NewFilteredParser added in v0.4.1

func NewFilteredParser(registry Registry, decorated Parser, targets []string) *FilteredParser

func (*FilteredParser) Parse added in v0.4.1

func (parser *FilteredParser) Parse(resourcePath string, options ParserOptions) (Resources, error)

type FormatParser added in v0.4.1

type FormatParser interface {
	Accept(file string) bool
	Parse(file string, options ParserOptions) (Resources, error)
}

type Handler

type Handler interface {
	APIVersion() string
	Kind() string

	// ResourceFilePath returns the location on disk where a resource should be updated
	ResourceFilePath(resource Resource, filetype string) string

	// Parse parses a manifest object into a struct for this resource type
	Parse(m map[string]any) (*Resource, error)

	// Unprepare removes unnecessary elements from a remote resource ready for presentation/comparison
	Unprepare(resource Resource) *Resource

	// Prepare gets a resource ready for dispatch to the remote endpoint
	Prepare(existing, resource Resource) *Resource

	// Retrieves a UID for a resource
	GetUID(resource Resource) (string, error)

	// GetSpecUID retrieves a UID from the spec of a raw resource
	GetSpecUID(resource Resource) (string, error)

	// Get retrieves JSON for a resource from an endpoint, by UID
	GetByUID(UID string) (*Resource, error)

	// GetRemote retrieves a remote equivalent of a remote resource
	GetRemote(resource Resource) (*Resource, error)

	// ListRemote retrieves as list of UIDs of all remote resources
	ListRemote() ([]string, error)

	// Add pushes a new resource to the endpoint
	Add(resource Resource) error

	// Update pushes an existing resource to the endpoint
	Update(existing, resource Resource) error

	// Validate gets or build the uid of corresponding resource
	Validate(resource Resource) error

	// Sort sorts resources as defined by the handler
	Sort(resources Resources) Resources

	// UsesFolders identifies whether this resource lives within a folder
	UsesFolders() bool

	// Detects whether a spec-only resource is of this kind
	Detect(map[string]any) bool
}

Handler describes a handler for a single API resource handled by a single provider

type JSONParser added in v0.4.1

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

func NewJSONParser added in v0.4.1

func NewJSONParser(registry Registry) *JSONParser

func (*JSONParser) Accept added in v0.4.1

func (parser *JSONParser) Accept(file string) bool

func (*JSONParser) Parse added in v0.4.1

func (parser *JSONParser) Parse(file string, options ParserOptions) (Resources, error)

Parse evaluates a JSON file and parses it into resources

type JsonnetParser added in v0.4.1

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

func NewJsonnetParser added in v0.4.1

func NewJsonnetParser(registry Registry, jsonnetPaths []string) *JsonnetParser

func (*JsonnetParser) Accept added in v0.4.1

func (parser *JsonnetParser) Accept(file string) bool

func (*JsonnetParser) Parse added in v0.4.1

func (parser *JsonnetParser) Parse(file string, options ParserOptions) (Resources, error)

Parse evaluates a jsonnet file and parses it into an object tree

type ListenHandler

type ListenHandler interface {
	// Listen watches a resource and update local file on changes
	Listen(UID, filename string) error
}

ListenHandler describes a handler that has the ability to watch a single resource for changes, and write changes to that resource to a local file

type ParseError added in v0.4.1

type ParseError struct {
	File string
	Err  error
}

func (ParseError) Error added in v0.4.1

func (err ParseError) Error() string

type Parser

type Parser interface {
	Parse(resourcePath string, options ParserOptions) (Resources, error)
}

func DefaultParser added in v0.4.1

func DefaultParser(registry Registry, targets []string, jsonnetPaths []string, opts ...ParserOpt) Parser

type ParserOpt added in v0.4.1

type ParserOpt func(config *parsersConfig)

func ParserContinueOnError added in v0.4.1

func ParserContinueOnError(continueOnError bool) ParserOpt

type ParserOptions added in v0.4.1

type ParserOptions struct {
	DefaultResourceKind string
	DefaultFolderUID    string
}

type Provider

type Provider interface {
	Name() string
	Group() string
	Version() string
	APIVersion() string
	GetHandlers() []Handler
}

Provider describes a single Endpoint Provider

type ProxyEndpoint added in v0.4.0

type ProxyEndpoint struct {
	Method  string
	Url     string
	Handler func(http.ResponseWriter, *http.Request)
}

type ProxyHandler added in v0.4.0

type ProxyHandler interface {
	// RegisterHandlers registers HTTP handlers for proxy events
	GetProxyEndpoints(p Server) []ProxyEndpoint

	// ProxyURL returns a URL path for a resource on the proxy
	ProxyURL(Resource) (string, error)
}

ProxyHandler describes a handler that can be used to edit resources live via a proxied UI

type ProxyProvider added in v0.4.0

type ProxyProvider interface {
	SetupProxy() (*httputil.ReverseProxy, error)
}

type Registry

type Registry struct {
	Providers    []Provider
	Handlers     map[string]Handler
	HandlerOrder []Handler
}

ProviderSet records providers

func NewRegistry added in v0.4.0

func NewRegistry(providers []Provider) Registry

NewRegistry returns an empty registry

func (*Registry) Detect added in v0.4.0

func (r *Registry) Detect(data any) string

func (*Registry) GetHandler

func (r *Registry) GetHandler(kind string) (Handler, error)

GetHandler returns a single provider based upon a JSON path

func (*Registry) GetProxyProvider added in v0.4.0

func (r *Registry) GetProxyProvider() (*ProxyProvider, error)

func (*Registry) HandlerMatchesTarget added in v0.4.0

func (r *Registry) HandlerMatchesTarget(handler Handler, targets []string) bool

HandlerMatchesTarget identifies whether a handler is in a target list

func (*Registry) ResourceMatchesTarget added in v0.4.0

func (r *Registry) ResourceMatchesTarget(kind string, UID string, targets []string) bool

ResourceMatchesTarget identifies whether a resource is in a target list

func (*Registry) Sort added in v0.4.0

func (r *Registry) Sort(resources Resources) Resources

type Resource

type Resource struct {
	Body map[string]interface{}

	Source Source
}

Resource represents a single Resource destined for a single endpoint

func NewResource added in v0.2.0

func NewResource(apiVersion, kind, name string, spec map[string]interface{}) (Resource, error)

NewResource returns a new Resource object

func ResourceFromMap added in v0.4.0

func ResourceFromMap(data map[string]interface{}) (*Resource, error)

func (*Resource) APIVersion

func (r *Resource) APIVersion() string

APIVersion returns the group and version of the provider of the resource

func (*Resource) DeleteSpecKey added in v0.2.0

func (r *Resource) DeleteSpecKey(key string)

func (*Resource) GetMetadata added in v0.2.0

func (r *Resource) GetMetadata(key string) string

func (*Resource) GetSpecString added in v0.2.0

func (r *Resource) GetSpecString(key string) (string, bool)

func (*Resource) GetSpecValue added in v0.2.0

func (r *Resource) GetSpecValue(key string) interface{}

func (*Resource) HasMetadata added in v0.2.0

func (r *Resource) HasMetadata(key string) bool

func (*Resource) Kind

func (r *Resource) Kind() string

Kind returns the 'kind' of the resource, i.e. the type of the handler

func (*Resource) Name added in v0.2.0

func (r *Resource) Name() string

func (*Resource) Ref added in v0.4.1

func (r *Resource) Ref() ResourceRef

func (*Resource) SetMetadata added in v0.2.0

func (r *Resource) SetMetadata(key, value string)

func (*Resource) SetSource added in v0.4.1

func (r *Resource) SetSource(source Source)

func (*Resource) SetSpecString added in v0.2.0

func (r *Resource) SetSpecString(key, value string)

func (*Resource) SetSpecValue added in v0.2.0

func (r *Resource) SetSpecValue(key string, value interface{})

func (*Resource) Spec added in v0.2.0

func (r *Resource) Spec() map[string]interface{}

func (*Resource) SpecAsJSON added in v0.2.0

func (r *Resource) SpecAsJSON() (string, error)

func (Resource) String added in v0.2.0

func (r Resource) String() string

func (*Resource) YAML added in v0.2.0

func (r *Resource) YAML() (string, error)

YAML Gets the string representation for this resource

type ResourceRef added in v0.4.1

type ResourceRef struct {
	Kind string
	Name string
}

func NewResourceRef added in v0.4.1

func NewResourceRef(kind string, name string) ResourceRef

func (ResourceRef) Equal added in v0.4.1

func (ref ResourceRef) Equal(other ResourceRef) bool

func (ResourceRef) String added in v0.4.1

func (ref ResourceRef) String() string

type Resources

type Resources []Resource

Resources represents a set of resources

func (Resources) Filter added in v0.4.1

func (r Resources) Filter(predicate func(Resource) bool) Resources

func (Resources) Find added in v0.4.1

func (r Resources) Find(ref ResourceRef) (Resource, bool)

func (Resources) Len added in v0.2.0

func (r Resources) Len() int

type Server added in v0.4.1

type Server struct {
	Port         int
	Registry     Registry
	Parser       WatchParser
	UserAgent    string
	ResourcePath string
	OpenBrowser  bool
	OnlySpec     bool
	OutputFormat string
	// contains filtered or unexported fields
}

func NewGrizzlyServer added in v0.4.0

func NewGrizzlyServer(registry Registry, parser WatchParser, resourcePath string, port int, openBrowser bool, onlySpec bool, outputFormat string) (*Server, error)

func (*Server) ProxyRequestHandler added in v0.4.1

func (p *Server) ProxyRequestHandler(w http.ResponseWriter, r *http.Request)

ProxyRequestHandler handles the http request using proxy

func (*Server) RootHandler added in v0.4.1

func (p *Server) RootHandler(w http.ResponseWriter, _ *http.Request)

func (*Server) Start added in v0.4.1

func (p *Server) Start() error

type SnapshotHandler added in v0.4.1

type SnapshotHandler interface {
	// Snapshot pushes a resource as a snapshot with an expiry
	Snapshot(resource Resource, expiresSeconds int) error
}

SnapshotHandler describes a handler that has the ability to push a resource as a snapshot

type Source added in v0.4.1

type Source struct {
	Format     string
	Location   string
	Path       string
	Rewritable bool
}

Source represents the on disk (etc) location of a resource

type Summary added in v0.4.1

type Summary struct {
	EventCounts map[EventType]int
}

func (Summary) AsString added in v0.4.1

func (summary Summary) AsString(resourceLabel string) string

type WatchParser added in v0.2.0

type WatchParser interface {
	Name() string
	Parse() (Resources, error)
}

WatchParser encapsulates the action of parsing a resource (jsonnet or otherwise)

type WriterRecorder added in v0.4.1

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

func NewWriterRecorder added in v0.4.1

func NewWriterRecorder(out io.Writer, eventFormatter EventFormatter) *WriterRecorder

func (*WriterRecorder) Record added in v0.4.1

func (recorder *WriterRecorder) Record(event Event)

func (*WriterRecorder) Summary added in v0.4.1

func (recorder *WriterRecorder) Summary() Summary

type YAMLParser added in v0.4.1

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

func NewYAMLParser added in v0.4.1

func NewYAMLParser(registry Registry) *YAMLParser

func (*YAMLParser) Accept added in v0.4.1

func (parser *YAMLParser) Accept(file string) bool

func (*YAMLParser) Parse added in v0.4.1

func (parser *YAMLParser) Parse(file string, options ParserOptions) (Resources, error)

Parse evaluates a YAML file and parses it into resources

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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