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 (
|
2024-03-25 20:31:06 +00:00
|
|
|
"context"
|
2024-04-03 16:54:50 +00:00
|
|
|
_ "encoding/json"
|
2024-03-25 20:31:06 +00:00
|
|
|
_ "fmt"
|
|
|
|
_ "gopkg.in/yaml.v3"
|
|
|
|
_ "net/url"
|
2024-05-06 00:48:54 +00:00
|
|
|
"gitea.rosskeen.house/rosskeen.house/machine"
|
2024-03-20 16:15:27 +00:00
|
|
|
)
|
|
|
|
|
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
|
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-03-20 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
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-03-25 20:31:06 +00:00
|
|
|
Update() error
|
2024-03-20 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceDeleter interface {
|
2024-03-25 20:31:06 +00:00
|
|
|
Delete() 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-03-20 19:23:31 +00:00
|
|
|
func NewResource(uri string) 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
|
|
|
|
|
|
|
func StorageMachine() machine.Stater {
|
2024-04-21 06:13:17 +00:00
|
|
|
// start_destroy -> absent -> start_create -> present -> start_destroy
|
|
|
|
stater := machine.New("absent")
|
2024-05-06 00:48:54 +00:00
|
|
|
stater.AddStates("absent", "start_create", "present", "start_delete", "start_read", "start_update")
|
|
|
|
stater.AddTransition("create", "absent", "start_create")
|
2024-04-21 06:13:17 +00:00
|
|
|
stater.AddTransition("created", "start_create", "present")
|
2024-05-06 00:48:54 +00:00
|
|
|
stater.AddTransition("read", "*", "start_read")
|
|
|
|
stater.AddTransition("state_read", "start_read", "present")
|
|
|
|
stater.AddTransition("update", "*", "start_update")
|
|
|
|
stater.AddTransition("updated", "start_update", "present")
|
|
|
|
stater.AddTransition("delete", "*", "start_delete")
|
2024-04-21 06:13:17 +00:00
|
|
|
stater.AddTransition("deleted", "start_delete", "absent")
|
2024-05-06 00:48:54 +00:00
|
|
|
return stater
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProcessMachine() machine.Stater {
|
|
|
|
return nil
|
2024-04-21 06:13:17 +00:00
|
|
|
}
|