lang, engine: Add a facility for resources to export constants
Since we focus on safety, it would be nice to reduce the chance of any
runtime errors if we made a typo for a resource parameter. With this
patch, each resource can export constants into the global namespace so
that typos would cause a compile error.
Of course in the future if we had a more advanced type system, then we
could support precise types for each individual resource param, but in
an attempt to keep things simple, we'll leave that for another day. It
would add complexity too if we ever wanted to store a parameter
externally.
Lastly, we might consider adding "special case" parsing so that directly
specified fields would parse intelligently. For example, we could allow:
file "/tmp/hello" {
state => exists, # magic sugar!
}
This isn't supported for now, but if it works after all the other parser
changes have been made, it might be something to consider.
This commit is contained in:
@@ -35,16 +35,46 @@ import (
|
||||
"github.com/purpleidea/mgmt/engine"
|
||||
"github.com/purpleidea/mgmt/engine/traits"
|
||||
engineUtil "github.com/purpleidea/mgmt/engine/util"
|
||||
"github.com/purpleidea/mgmt/lang/funcs/vars"
|
||||
"github.com/purpleidea/mgmt/lang/interfaces"
|
||||
"github.com/purpleidea/mgmt/lang/types"
|
||||
"github.com/purpleidea/mgmt/recwatch"
|
||||
"github.com/purpleidea/mgmt/util"
|
||||
"github.com/purpleidea/mgmt/util/errwrap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
engine.RegisterResource("file", func() engine.Res { return &FileRes{} })
|
||||
engine.RegisterResource(KindFile, func() engine.Res { return &FileRes{} })
|
||||
|
||||
// const.res.file.state.exists = "exists"
|
||||
// const.res.file.state.absent = "absent"
|
||||
vars.RegisterResourceParams(KindFile, map[string]map[string]func() interfaces.Var{
|
||||
ParamFileState: {
|
||||
FileStateExists: func() interfaces.Var {
|
||||
return &types.StrValue{
|
||||
V: FileStateExists,
|
||||
}
|
||||
},
|
||||
FileStateAbsent: func() interfaces.Var {
|
||||
return &types.StrValue{
|
||||
V: FileStateAbsent,
|
||||
}
|
||||
},
|
||||
// TODO: consider removing this field entirely
|
||||
"undefined": func() interfaces.Var {
|
||||
return &types.StrValue{
|
||||
V: FileStateUndefined, // empty string
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const (
|
||||
// KindFile is the kind string used to identify this resource.
|
||||
KindFile = "file"
|
||||
// ParamFileState is the name of the state field parameter.
|
||||
ParamFileState = "state"
|
||||
// FileStateExists is the string that represents that the file should be
|
||||
// present.
|
||||
FileStateExists = "exists"
|
||||
@@ -975,7 +1005,7 @@ func (obj *FileRes) sourceCheckApply(apply bool) (bool, error) {
|
||||
// programming error
|
||||
return false, fmt.Errorf("not a Res")
|
||||
}
|
||||
if res.Kind() != "file" {
|
||||
if res.Kind() != KindFile {
|
||||
continue // only interested in files
|
||||
}
|
||||
if res.Name() == obj.Name() {
|
||||
@@ -1582,7 +1612,7 @@ func (obj *FileRes) Reversed() (engine.ReversibleRes, error) {
|
||||
func (obj *FileRes) GraphQueryAllowed(opts ...engine.GraphQueryableOption) error {
|
||||
options := &engine.GraphQueryableOptions{} // default options
|
||||
options.Apply(opts...) // apply the options
|
||||
if options.Kind != "file" {
|
||||
if options.Kind != KindFile {
|
||||
return fmt.Errorf("only other files can access my information")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -228,7 +228,7 @@ func TestResources1(t *testing.T) {
|
||||
p := "/tmp/whatever"
|
||||
s := "hello, world\n"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
contents := s
|
||||
res.Content = &contents
|
||||
|
||||
@@ -292,7 +292,7 @@ func TestResources1(t *testing.T) {
|
||||
res := r.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/emptyfile"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
|
||||
timeline := []Step{
|
||||
NewStartupStep(1000 * 60), // startup
|
||||
@@ -316,7 +316,7 @@ func TestResources1(t *testing.T) {
|
||||
res := r.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/existingfile"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
content := "some existing text\n"
|
||||
|
||||
timeline := []Step{
|
||||
@@ -811,14 +811,14 @@ func TestResources2(t *testing.T) {
|
||||
testCases := []test{}
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
// content => "some new text\n",
|
||||
//}
|
||||
r1 := makeRes("file", "r1")
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
content := "some new text\n"
|
||||
res.Content = &content
|
||||
|
||||
@@ -850,7 +850,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
//res.State = "exists" // not specified!
|
||||
//res.State = FileStateExists // not specified!
|
||||
content := "some new text\n"
|
||||
res.Content = &content
|
||||
|
||||
@@ -883,7 +883,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
//res.State = "exists" // not specified!
|
||||
//res.State = FileStateExists // not specified!
|
||||
content := "some new text\n"
|
||||
res.Content = &content
|
||||
|
||||
@@ -907,14 +907,14 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// state => "absent",
|
||||
// state => $const.res.file.state.absent,
|
||||
//}
|
||||
// and no existing file exists!
|
||||
r1 := makeRes("file", "r1")
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
res.State = "absent"
|
||||
res.State = FileStateAbsent
|
||||
|
||||
timeline := []func() error{
|
||||
fileRemove(p), // nothing here
|
||||
@@ -936,14 +936,14 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// state => "absent",
|
||||
// state => $const.res.file.state.absent,
|
||||
//}
|
||||
// and a file already exists!
|
||||
r1 := makeRes("file", "r1")
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
res.State = "absent"
|
||||
res.State = FileStateAbsent
|
||||
|
||||
timeline := []func() error{
|
||||
fileWrite(p, "whatever"),
|
||||
@@ -966,7 +966,7 @@ func TestResources2(t *testing.T) {
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// content => "some new text\n",
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
//
|
||||
// Meta:reverse => true,
|
||||
//}
|
||||
@@ -974,7 +974,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
content := "some new text\n"
|
||||
res.Content = &content
|
||||
original := "this is the original state\n" // original state
|
||||
@@ -1035,7 +1035,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
//res.State = "exists" // unspecified
|
||||
//res.State = FileStateExists // unspecified
|
||||
content := "some new text\n"
|
||||
res.Content = &content
|
||||
original := "this is the original state\n" // original state
|
||||
@@ -1100,7 +1100,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
//res.State = "exists" // unspecified
|
||||
//res.State = FileStateExists // unspecified
|
||||
content := "some new text\n"
|
||||
res.Content = &content
|
||||
var r2 engine.Res // future reversed resource
|
||||
@@ -1149,7 +1149,7 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// state => "absent",
|
||||
// state => $const.res.file.state.absent,
|
||||
//
|
||||
// Meta:reverse => true,
|
||||
//}
|
||||
@@ -1157,7 +1157,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
res.State = "absent"
|
||||
res.State = FileStateAbsent
|
||||
original := "this is the original state\n" // original state
|
||||
var r2 engine.Res // future reversed resource
|
||||
|
||||
@@ -1207,7 +1207,7 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
// fragments => [
|
||||
// "/tmp/frag1",
|
||||
// "/tmp/fragdir1/",
|
||||
@@ -1220,7 +1220,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somefile"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
res.Fragments = []string{
|
||||
"/tmp/frag1",
|
||||
"/tmp/fragdir1/",
|
||||
@@ -1272,7 +1272,7 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somefile" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
// source => "/tmp/somefiletocopy",
|
||||
//}
|
||||
r1 := makeRes("file", "r1")
|
||||
@@ -1281,7 +1281,7 @@ func TestResources2(t *testing.T) {
|
||||
p2 := "/tmp/somefiletocopy"
|
||||
content := "hello this is some file to copy\n"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
res.Source = p2
|
||||
|
||||
timeline := []func() error{
|
||||
@@ -1308,13 +1308,13 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somedir/" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
//}
|
||||
r1 := makeRes("file", "r1")
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somedir/"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
|
||||
timeline := []func() error{
|
||||
fileAbsent(p), // ensure it's absent
|
||||
@@ -1337,7 +1337,7 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somedir/" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
// source => /tmp/somedirtocopy/,
|
||||
// recurse => true,
|
||||
//}
|
||||
@@ -1346,7 +1346,7 @@ func TestResources2(t *testing.T) {
|
||||
p := "/tmp/somedir/"
|
||||
p2 := "/tmp/somedirtocopy/"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
res.Source = p2
|
||||
res.Recurse = true
|
||||
|
||||
@@ -1408,7 +1408,7 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somedir/" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
// recurse => true,
|
||||
// purge => true,
|
||||
//}
|
||||
@@ -1416,7 +1416,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somedir/"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
res.Recurse = true
|
||||
res.Purge = true
|
||||
|
||||
@@ -1474,7 +1474,7 @@ func TestResources2(t *testing.T) {
|
||||
}
|
||||
{
|
||||
//file "/tmp/somedir/" {
|
||||
// state => "exists",
|
||||
// state => $const.res.file.state.exists,
|
||||
// recurse => true,
|
||||
// purge => true,
|
||||
//}
|
||||
@@ -1489,7 +1489,7 @@ func TestResources2(t *testing.T) {
|
||||
res := r1.(*FileRes) // if this panics, the test will panic
|
||||
p := "/tmp/somedir/"
|
||||
res.Path = p
|
||||
res.State = "exists"
|
||||
res.State = FileStateExists
|
||||
res.Recurse = true
|
||||
res.Purge = true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user