jx/internal/resource/resource.go

218 lines
6.1 KiB
Go
Raw Normal View History

2024-03-20 19:23:31 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-03-22 17:39:06 +00:00
// The resource package handles CRUD operations on resources and YAML (de)serialization
2024-03-20 16:15:27 +00:00
package resource
import (
_ "encoding/json"
2024-03-25 20:31:06 +00:00
_ "fmt"
_ "gopkg.in/yaml.v3"
2024-05-06 00:48:54 +00:00
"gitea.rosskeen.house/rosskeen.house/machine"
"decl/internal/data"
"errors"
)
var (
ErrInvalidResourceURI error = errors.New("Invalid resource URI")
ErrResourceStateAbsent = errors.New("Resource state absent")
ErrUnableToFindResource = errors.New("Unable to find resource - not loaded")
2024-03-20 16:15:27 +00:00
)
type ResourceReference string
/*
2024-04-23 22:35:08 +00:00
type ResourceSelector func(r *Declaration) bool
2024-03-20 16:15:27 +00:00
type Resource interface {
2024-03-25 20:31:06 +00:00
Type() string
2024-05-06 00:48:54 +00:00
StateMachine() machine.Stater
2024-03-25 20:31:06 +00:00
URI() string
2024-04-19 07:52:10 +00:00
SetURI(string) error
UseConfig(config ConfigurationValueGetter)
2024-03-25 20:31:06 +00:00
ResolveId(context.Context) string
ResourceLoader
StateTransformer
ResourceReader
2024-04-09 19:30:05 +00:00
ResourceValidator
2024-04-19 07:52:10 +00:00
Clone() Resource
2024-07-17 08:34:57 +00:00
SetResourceMapper(resources ResourceMapper)
2024-03-20 16:15:27 +00:00
}
type ContentReader interface {
ContentReaderStream() (*transport.Reader, error)
}
type ContentWriter interface {
ContentWriterStream() (*transport.Writer, error)
}
2024-07-17 08:34:57 +00:00
type ContentReadWriter interface {
ContentReader
ContentWriter
}
2024-03-20 19:23:31 +00:00
type ResourceValidator interface {
2024-03-25 20:31:06 +00:00
Validate() error
2024-03-20 16:15:27 +00:00
}
type ResourceCreator interface {
2024-03-25 20:31:06 +00:00
Create(context.Context) error
2024-03-20 16:15:27 +00:00
}
type ResourceReader interface {
2024-03-25 20:31:06 +00:00
Read(context.Context) ([]byte, error)
2024-03-20 16:15:27 +00:00
}
type ResourceUpdater interface {
2024-05-24 05:11:51 +00:00
Update(context.Context) error
2024-03-20 16:15:27 +00:00
}
type ResourceDeleter interface {
2024-05-24 05:11:51 +00:00
Delete(context.Context) error
2024-03-20 16:15:27 +00:00
}
2024-03-20 19:23:31 +00:00
type ResourceDecoder struct {
2024-03-20 16:15:27 +00:00
}
2024-05-24 05:11:51 +00:00
type ResourceCrudder struct {
ResourceCreator
ResourceReader
ResourceUpdater
ResourceDeleter
}
*/
2024-05-24 05:11:51 +00:00
func NewResource(uri string) data.Resource {
2024-03-25 20:31:06 +00:00
r, e := ResourceTypes.New(uri)
if e == nil {
return r
}
return nil
2024-03-20 16:15:27 +00:00
}
2024-05-06 00:48:54 +00:00
/*
2024-07-17 08:34:57 +00:00
// Return a Content ReadWriter for the resource referred to.
func (r ResourceReference) Lookup(look data.ResourceMapper) data.ContentReadWriter {
2024-07-17 08:34:57 +00:00
slog.Info("ResourceReference.Lookup()", "resourcereference", r, "resourcemapper", look)
if look != nil {
if v,ok := look.Get(string(r)); ok {
return v.(data.ContentReadWriter)
2024-07-17 08:34:57 +00:00
}
}
return r
}
func (r ResourceReference) Dereference(look data.ResourceMapper) data.Resource {
2024-07-17 08:34:57 +00:00
slog.Info("ResourceReference.Dereference()", "resourcereference", r, "resourcemapper", look)
if look != nil {
if v,ok := look.Get(string(r)); ok {
return v.(*Declaration).Attributes
}
}
return nil
}
func (r ResourceReference) Parse() *url.URL {
u, e := url.Parse(string(r))
if e == nil {
return u
}
return nil
}
func (r ResourceReference) Exists() bool {
return transport.ExistsURI(string(r))
}
func (r ResourceReference) ContentReaderStream() (*transport.Reader, error) {
return transport.NewReaderURI(string(r))
}
func (r ResourceReference) ContentWriterStream() (*transport.Writer, error) {
return transport.NewWriterURI(string(r))
}
*/
2024-05-09 07:39:45 +00:00
func StorageMachine(sub machine.Subscriber) machine.Stater {
2024-04-21 06:13:17 +00:00
// start_destroy -> absent -> start_create -> present -> start_destroy
2024-05-22 19:07:18 +00:00
stater := machine.New("unknown")
stater.AddStates("unkonwn", "absent", "start_create", "present", "start_delete", "start_read", "start_update")
stater.AddTransition("create", machine.States("unknown", "absent"), "start_create")
2024-05-13 05:41:12 +00:00
if e := stater.AddSubscription("create", sub); e != nil {
2024-05-14 19:42:28 +00:00
return nil
2024-05-13 05:41:12 +00:00
}
2024-05-22 19:07:18 +00:00
stater.AddTransition("created", machine.States("start_create"), "present")
if e := stater.AddSubscription("created", sub); e != nil {
return nil
}
stater.AddTransition("exists", machine.States("unknown", "absent"), "present")
if e := stater.AddSubscription("exists", sub); e != nil {
return nil
}
stater.AddTransition("notexists", machine.States("*"), "absent")
if e := stater.AddSubscription("notexists", sub); e != nil {
return nil
}
stater.AddTransition("read", machine.States("*"), "start_read")
2024-05-13 05:41:12 +00:00
if e := stater.AddSubscription("read", sub); e != nil {
2024-05-14 19:42:28 +00:00
return nil
2024-05-13 05:41:12 +00:00
}
2024-05-22 19:07:18 +00:00
stater.AddTransition("state_read", machine.States("start_read"), "present")
stater.AddTransition("update", machine.States("*"), "start_update")
2024-05-13 05:41:12 +00:00
if e := stater.AddSubscription("update", sub); e != nil {
2024-05-14 19:42:28 +00:00
return nil
2024-05-13 05:41:12 +00:00
}
2024-05-22 19:07:18 +00:00
stater.AddTransition("updated", machine.States("start_update"), "present")
stater.AddTransition("delete", machine.States("*"), "start_delete")
2024-05-13 05:41:12 +00:00
if e := stater.AddSubscription("delete", sub); e != nil {
2024-05-14 19:42:28 +00:00
return nil
2024-05-13 05:41:12 +00:00
}
2024-05-22 19:07:18 +00:00
stater.AddTransition("deleted", machine.States("start_delete"), "absent")
if e := stater.AddSubscription("deleted", sub); e != nil {
return nil
}
2024-05-06 00:48:54 +00:00
return stater
}
2024-05-22 19:07:18 +00:00
func ProcessMachine(sub machine.Subscriber) machine.Stater {
// "enum": [ "created", "restarting", "running", "paused", "exited", "dead" ]
stater := machine.New("unknown")
stater.AddStates("unkonwn", "absent", "start_create", "present", "created", "restarting", "running", "paused", "exited", "dead", "start_delete", "start_read", "start_update")
stater.AddTransition("create", machine.States("unknown", "absent"), "start_create")
if e := stater.AddSubscription("create", sub); e != nil {
return nil
}
stater.AddTransition("created", machine.States("start_create"), "present")
if e := stater.AddSubscription("created", sub); e != nil {
return nil
}
stater.AddTransition("exists", machine.States("unknown", "absent"), "present")
if e := stater.AddSubscription("exists", sub); e != nil {
return nil
}
stater.AddTransition("notexists", machine.States("*"), "absent")
if e := stater.AddSubscription("notexists", sub); e != nil {
return nil
}
stater.AddTransition("read", machine.States("*"), "start_read")
if e := stater.AddSubscription("read", sub); e != nil {
return nil
}
stater.AddTransition("state_read", machine.States("start_read"), "present")
stater.AddTransition("update", machine.States("*"), "start_update")
if e := stater.AddSubscription("update", sub); e != nil {
return nil
}
stater.AddTransition("updated", machine.States("start_update"), "present")
stater.AddTransition("delete", machine.States("*"), "start_delete")
if e := stater.AddSubscription("delete", sub); e != nil {
return nil
}
stater.AddTransition("deleted", machine.States("start_delete"), "absent")
if e := stater.AddSubscription("deleted", sub); e != nil {
return nil
}
return stater
2024-04-21 06:13:17 +00:00
}