resources: Improve composition of Validate API in resources

This now appropriately calls the Base method.
This commit is contained in:
James Shubin
2017-01-16 19:28:00 -05:00
parent 51c83116a2
commit 2a6df875ec
13 changed files with 31 additions and 11 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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.

View File

@@ -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.

View File

@@ -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.

View File

@@ -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.

View File

@@ -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.

View File

@@ -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

View File

@@ -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.

View File

@@ -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 == "" {

View File

@@ -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.

View File

@@ -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.

View File

@@ -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) {