add document state transformer interface
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run

This commit is contained in:
Matthew Rich 2024-09-25 04:49:24 +00:00
parent 452695c8f5
commit f1a56b0968
5 changed files with 49 additions and 23 deletions

View File

@ -150,7 +150,7 @@ func (d *Declaration) Resource() data.Resource {
return d.Attributes
}
func (d *Declaration) Apply() (result error) {
func (d *Declaration) Apply(stateTransition string) (result error) {
defer func() {
if r := recover(); r != nil {
slog.Info("Declaration.Apply()", "error", r, "stacktrace", string(debug.Stack()))
@ -161,9 +161,13 @@ func (d *Declaration) Apply() (result error) {
}
}()
if stateTransition == "" {
stateTransition = d.Transition
}
stater := d.Attributes.StateMachine()
slog.Info("Declaration.Apply()", "machine", stater, "machine.state", stater.CurrentState(), "uri", d.Attributes.URI())
switch d.Transition {
switch stateTransition {
case "construct":
if doc, ok := DocumentRegistry.DeclarationMap[d]; ok {
d.SetDocument(doc)

View File

@ -9,7 +9,6 @@ _ "encoding/json"
"github.com/stretchr/testify/assert"
_ "log"
_ "os"
"path/filepath"
_ "decl/internal/types"
"decl/internal/codec"
"testing"
@ -18,7 +17,7 @@ _ "decl/internal/types"
/*
func TestYamlLoadDecl(t *testing.T) {
file := filepath.Join(TempDir, "fooread.txt")
file := TempDir.FilePath("fooread.txt")
resourceAttributes := make(map[string]any)
decl := fmt.Sprintf(`
@ -44,7 +43,7 @@ func TestNewResourceDeclaration(t *testing.T) {
}
func TestNewResourceDeclarationType(t *testing.T) {
file := filepath.Join(TempDir, "fooread.txt")
file := TempDir.FilePath("fooread.txt")
decl := fmt.Sprintf(`
type: foo
@ -61,6 +60,35 @@ func TestNewResourceDeclarationType(t *testing.T) {
assert.Equal(t, TypeName("foo"), resourceDeclaration.Type)
assert.NotNil(t, resourceDeclaration.Attributes)
}
/* XXX
func TestEventTypeLoad(t *testing.T) {
file := TempDir.FilePath("fooread.txt")
fooTemplate := `
type: foo
on:
load: |
%s
attributes:
name: "%s"
`
for _, v := range []struct{ decl string; expected string } {
{ decl: fmt.Sprintf(fooTemplate, "print('Hello world!')", file), expected: "" },
{ decl: fmt.Sprintf(fooTemplate, "print('Hello world!') fail", file), expected: "Lua error: luaL_loadstring(): failed loading, [string \"print('Hello world!') fail...\"]:2: '=' expected near '<eof>', return code: 3" },
} {
resourceDeclaration := NewDeclaration()
resourceDeclaration.ResourceTypes = TestResourceTypes
assert.NotNil(t, resourceDeclaration)
e := resourceDeclaration.LoadString(v.decl, codec.FormatYaml)
assert.Nil(t, e)
assert.Equal(t, v.expected, resourceDeclaration.Error)
}
}
*/
/*
func TestDeclarationNewResource(t *testing.T) {
resourceDeclaration := NewDeclaration()
@ -98,7 +126,7 @@ func TestDeclarationJson(t *testing.T) {
"type": "user",
"attributes": {
"name": "testuser",
"uid": "10012"
"uid": "10012"
}
}
`

View File

@ -268,16 +268,13 @@ func (d *Document) Apply(state string) error {
if idx < 0 { idx = - idx }
slog.Info("Document.Apply() applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI(), "resource", d.ResourceDeclarations[idx].Resource())
if state != "" {
d.ResourceDeclarations[idx].Transition = state
}
d.ResourceDeclarations[idx].SetConfig(d.config)
slog.Info("Document.Apply() applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI(), "resource", d.ResourceDeclarations[idx].Resource())
if d.ResourceDeclarations[idx].Requires.Check() {
if e := d.ResourceDeclarations[idx].Apply(); e != nil {
if e := d.ResourceDeclarations[idx].Apply(state); e != nil {
slog.Error("Document.Apply() error applying resource", "index", idx, "uri", d.ResourceDeclarations[idx].Resource().URI())
d.ResourceDeclarations[idx].Error = e.Error()
@ -560,8 +557,7 @@ func (d *Document) UnmarshalYAML(value *yaml.Node) error {
}
d.assignConfigurationsDocument()
d.assignResourcesDocument()
d.loadImports()
return nil
return d.loadImports()
}
func (d *Document) UnmarshalJSON(data []byte) error {
@ -572,7 +568,6 @@ func (d *Document) UnmarshalJSON(data []byte) error {
}
d.assignConfigurationsDocument()
d.assignResourcesDocument()
d.loadImports()
return nil
return d.loadImports()
}

View File

@ -20,8 +20,7 @@ var ProcessTestUserName string
var ProcessTestGroupName string
func TestMain(m *testing.M) {
var err error
err = TempDir.Create()
err := TempDir.Create()
if err != nil || TempDir == "" {
log.Fatal(err)
}

View File

@ -15,12 +15,12 @@ _ "fmt"
)
type MockResource struct {
InjectURI func() string `json:"-" yaml:"-"`
InjectType func() string `json:"-" yaml:"-"`
InjectResolveId func(ctx context.Context) string `json:"-" yaml:"-"`
InjectLoadDecl func(string) error `json:"-" yaml:"-"`
InjectValidate func() error `json:"-" yaml:"-"`
InjectApply func() error `json:"-" yaml:"-"`
InjectURI func() string `json:"-" yaml:"-"`
InjectType func() string `json:"-" yaml:"-"`
InjectResolveId func(ctx context.Context) string `json:"-" yaml:"-"`
InjectLoadDecl func(string) error `json:"-" yaml:"-"`
InjectValidate func() error `json:"-" yaml:"-"`
InjectApply func() error `json:"-" yaml:"-"`
InjectJSON func() ([]byte, error) `json:"-" yaml:"-"`
InjectYAML func() ([]byte, error) `json:"-" yaml:"-"`
InjectPB func() ([]byte, error) `json:"-" yaml:"-"`
@ -32,7 +32,7 @@ type MockResource struct {
InjectRead func(context.Context) ([]byte, error) `json:"-" yaml:"-"`
InjectUpdate func(context.Context) error `json:"-" yaml:"-"`
InjectDelete func(context.Context) error `json:"-" yaml:"-"`
InjectStateMachine func() machine.Stater `json:"-" yaml:"-"`
InjectStateMachine func() machine.Stater `json:"-" yaml:"-"`
InjectSetResourceMapper func(data.ResourceMapper) `json:"-" yaml:"-"`
InjectUseConfig func(data.ConfigurationValueGetter) `json:"-" yaml:"-"`
InjectNotify func(*machine.EventMessage) `json:"-" yaml:"-"`