diff --git a/docs/resource-guide.md b/docs/resource-guide.md index 7840d02e..04fc6881 100644 --- a/docs/resource-guide.md +++ b/docs/resource-guide.md @@ -106,6 +106,17 @@ specifications, it should generate an error. If you notice that this method is quite large, it might be an indication that you should reconsider the parameter list and interface to this resource. This method is called _before_ `Init`. +#### Example +```golang +// Validate reports any problems with the struct definition. +func (obj *FooRes) Validate() error { + if obj.Answer != 42 { // validate whatever you want + return fmt.Errorf("expected an answer of 42") + } + return obj.BaseRes.Validate() // remember to call the base method! +} +``` + ### Init ```golang Init() error diff --git a/resources/exec.go b/resources/exec.go index 32771e78..20cb45c0 100644 --- a/resources/exec.go +++ b/resources/exec.go @@ -85,7 +85,7 @@ func (obj *ExecRes) Validate() error { return fmt.Errorf("Don't poll when we have a watch command.") } - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/file.go b/resources/file.go index 5ee49efd..56e3f885 100644 --- a/resources/file.go +++ b/resources/file.go @@ -107,7 +107,7 @@ func (obj *FileRes) Validate() error { // return fmt.Errorf("Can't specify an empty source when creating a Dir.") //} - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/hostname.go b/resources/hostname.go index 2078e357..f13a5132 100644 --- a/resources/hostname.go +++ b/resources/hostname.go @@ -92,7 +92,7 @@ func (obj *HostnameRes) Validate() error { if obj.PrettyHostname == "" && obj.StaticHostname == "" && obj.TransientHostname == "" { return ErrResourceInsufficientParameters } - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/msg.go b/resources/msg.go index 78174e05..67daeaef 100644 --- a/resources/msg.go +++ b/resources/msg.go @@ -89,7 +89,7 @@ func (obj *MsgRes) Validate() error { return fmt.Errorf("Fields cannot begin with _.") } } - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/noop.go b/resources/noop.go index 7b4b149d..b4341b6b 100644 --- a/resources/noop.go +++ b/resources/noop.go @@ -53,7 +53,7 @@ func (obj *NoopRes) Default() Res { // Validate if the params passed in are valid data. func (obj *NoopRes) Validate() error { - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/nspawn.go b/resources/nspawn.go index 04f7a59c..63230490 100644 --- a/resources/nspawn.go +++ b/resources/nspawn.go @@ -85,7 +85,11 @@ func (obj *NspawnRes) Validate() error { if _, exists := validStates[obj.State]; !exists { return fmt.Errorf("Invalid State: %s", obj.State) } - return obj.svc.Validate() + + if err := obj.svc.Validate(); err != nil { // composite resource + return errwrap.Wrapf(err, "validate failed for embedded svc") + } + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/password.go b/resources/password.go index 2e6281dc..859eb8ae 100644 --- a/resources/password.go +++ b/resources/password.go @@ -76,7 +76,7 @@ func (obj *PasswordRes) Default() Res { // Validate if the params passed in are valid data. func (obj *PasswordRes) Validate() error { - return nil + return obj.BaseRes.Validate() } // Init generates a new password for this resource if one was not provided. It diff --git a/resources/pkg.go b/resources/pkg.go index 06a8db37..8b1bcb8c 100644 --- a/resources/pkg.go +++ b/resources/pkg.go @@ -73,7 +73,7 @@ func (obj *PkgRes) Validate() error { return fmt.Errorf("State cannot be empty!") } - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/resources.go b/resources/resources.go index f17ba0e9..26639977 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -242,6 +242,11 @@ func (obj *BaseUID) Reversed() bool { return *obj.reversed } +// Validate reports any problems with the struct definition. +func (obj *BaseRes) Validate() error { + return nil +} + // Init initializes structures like channels if created without New constructor. func (obj *BaseRes) Init() error { if obj.kind == "" { diff --git a/resources/svc.go b/resources/svc.go index fc096ff7..cdb3a765 100644 --- a/resources/svc.go +++ b/resources/svc.go @@ -69,7 +69,7 @@ func (obj *SvcRes) Validate() error { if obj.Startup != "enabled" && obj.Startup != "disabled" && obj.Startup != "" { return fmt.Errorf("Startup must be either `enabled` or `disabled` or undefined.") } - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/timer.go b/resources/timer.go index 6e5647a2..36c14ac4 100644 --- a/resources/timer.go +++ b/resources/timer.go @@ -62,7 +62,7 @@ func (obj *TimerRes) Default() Res { // Validate the params that are passed to TimerRes. func (obj *TimerRes) Validate() error { - return nil + return obj.BaseRes.Validate() } // Init runs some startup code for this resource. diff --git a/resources/virt.go b/resources/virt.go index 7fc47e0f..dc034404 100644 --- a/resources/virt.go +++ b/resources/virt.go @@ -122,7 +122,7 @@ func (obj *VirtRes) Init() error { // Validate if the params passed in are valid data. func (obj *VirtRes) Validate() error { - return nil + return obj.BaseRes.Validate() } func (obj *VirtRes) connect() (conn *libvirt.Connect, err error) {