152 lines
4.3 KiB
Go
152 lines
4.3 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 (
|
|
_ "encoding/json"
|
|
_ "fmt"
|
|
_ "gopkg.in/yaml.v3"
|
|
"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")
|
|
)
|
|
|
|
type ResourceReference string
|
|
|
|
func NewResource(uri string) data.Resource {
|
|
r, e := ResourceTypes.New(uri)
|
|
if e == nil {
|
|
return r
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ResourceConstructor(res data.Resource, uri data.URIParser) (err error) {
|
|
if uri != nil {
|
|
return uri.ConstructResource(res)
|
|
} else {
|
|
if ri, ok := res.(data.ResourceInitializer); ok {
|
|
return ri.Init(uri)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// Common resource states
|
|
func ResourceMachine(sub machine.Subscriber) machine.Stater {
|
|
stater := machine.New("unknown")
|
|
stater.AddStates("unkonwn", "inconsistent", "absent", "start_create", "present", "start_delete", "start_read", "start_update", "start_stat", "created")
|
|
|
|
stater.AddTransition("create", machine.States("unknown", "inconsistent", "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", "inconsistent", "absent", "start_stat"), "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("update-failed", machine.States("start_update"), "inconsistent")
|
|
if e := stater.AddSubscription("update-failed", sub); e != nil {
|
|
return nil
|
|
}
|
|
|
|
stater.AddTransition("create-failed", machine.States("start_create"), "inconsistent")
|
|
if e := stater.AddSubscription("create-failed", sub); e != nil {
|
|
return nil
|
|
}
|
|
|
|
return stater
|
|
}
|
|
|
|
func StorageMachine(sub machine.Subscriber) machine.Stater {
|
|
// start_destroy -> absent -> start_create -> present -> start_destroy
|
|
|
|
stater := ResourceMachine(sub)
|
|
|
|
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("stat", machine.States("*"), "start_stat")
|
|
if e := stater.AddSubscription("stat", sub); e != nil {
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func ProcessMachine(sub machine.Subscriber) machine.Stater {
|
|
// "enum": [ "created", "restarting", "running", "paused", "exited", "dead" ]
|
|
|
|
stater := ResourceMachine(sub)
|
|
|
|
stater.AddStates("restarting", "running", "paused", "exited", "dead")
|
|
|
|
|
|
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("stat", machine.States("*"), "start_stat")
|
|
if e := stater.AddSubscription("stat", 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
|
|
}
|