91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
// The resource package handles CRUD operations on resources and YAML (de)serialization
|
|
package resource
|
|
|
|
import (
|
|
"context"
|
|
_ "encoding/json"
|
|
_ "fmt"
|
|
_ "gopkg.in/yaml.v3"
|
|
_ "net/url"
|
|
"gitea.rosskeen.house/rosskeen.house/machine"
|
|
)
|
|
|
|
type ResourceSelector func(r *Declaration) bool
|
|
|
|
type Resource interface {
|
|
Type() string
|
|
StateMachine() machine.Stater
|
|
URI() string
|
|
SetURI(string) error
|
|
ResolveId(context.Context) string
|
|
ResourceLoader
|
|
StateTransformer
|
|
ResourceReader
|
|
ResourceValidator
|
|
Clone() Resource
|
|
}
|
|
|
|
type ResourceValidator interface {
|
|
Validate() error
|
|
}
|
|
|
|
type ResourceCreator interface {
|
|
Create(context.Context) error
|
|
}
|
|
|
|
type ResourceReader interface {
|
|
Read(context.Context) ([]byte, error)
|
|
}
|
|
|
|
type ResourceUpdater interface {
|
|
Update() error
|
|
}
|
|
|
|
type ResourceDeleter interface {
|
|
Delete() error
|
|
}
|
|
|
|
type ResourceDecoder struct {
|
|
}
|
|
|
|
func NewResource(uri string) Resource {
|
|
r, e := ResourceTypes.New(uri)
|
|
if e == nil {
|
|
return r
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func StorageMachine(sub machine.Subscriber) machine.Stater {
|
|
// start_destroy -> absent -> start_create -> present -> start_destroy
|
|
stater := machine.New("absent")
|
|
stater.AddStates("absent", "start_create", "present", "start_delete", "start_read", "start_update")
|
|
stater.AddTransition("create", "absent", "start_create")
|
|
if e := stater.AddSubscription("create", sub); e != nil {
|
|
return nil
|
|
}
|
|
stater.AddTransition("created", "start_create", "present")
|
|
stater.AddTransition("read", "*", "start_read")
|
|
if e := stater.AddSubscription("read", sub); e != nil {
|
|
return nil
|
|
}
|
|
stater.AddTransition("state_read", "start_read", "present")
|
|
stater.AddTransition("update", "*", "start_update")
|
|
if e := stater.AddSubscription("update", sub); e != nil {
|
|
return nil
|
|
}
|
|
stater.AddTransition("updated", "start_update", "present")
|
|
stater.AddTransition("delete", "*", "start_delete")
|
|
if e := stater.AddSubscription("delete", sub); e != nil {
|
|
return nil
|
|
}
|
|
stater.AddTransition("deleted", "start_delete", "absent")
|
|
return stater
|
|
}
|
|
|
|
func ProcessMachine() machine.Stater {
|
|
return nil
|
|
}
|