resources: Improve composition of Validate API in resources
This now appropriately calls the Base method.
This commit is contained in:
@@ -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
|
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`.
|
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
|
### Init
|
||||||
```golang
|
```golang
|
||||||
Init() error
|
Init() error
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ func (obj *ExecRes) Validate() error {
|
|||||||
return fmt.Errorf("Don't poll when we have a watch command.")
|
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.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ func (obj *FileRes) Validate() error {
|
|||||||
// return fmt.Errorf("Can't specify an empty source when creating a Dir.")
|
// 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.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ func (obj *HostnameRes) Validate() error {
|
|||||||
if obj.PrettyHostname == "" && obj.StaticHostname == "" && obj.TransientHostname == "" {
|
if obj.PrettyHostname == "" && obj.StaticHostname == "" && obj.TransientHostname == "" {
|
||||||
return ErrResourceInsufficientParameters
|
return ErrResourceInsufficientParameters
|
||||||
}
|
}
|
||||||
return nil
|
return obj.BaseRes.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init runs some startup code for this resource.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ func (obj *MsgRes) Validate() error {
|
|||||||
return fmt.Errorf("Fields cannot begin with _.")
|
return fmt.Errorf("Fields cannot begin with _.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return obj.BaseRes.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init runs some startup code for this resource.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func (obj *NoopRes) Default() Res {
|
|||||||
|
|
||||||
// Validate if the params passed in are valid data.
|
// Validate if the params passed in are valid data.
|
||||||
func (obj *NoopRes) Validate() error {
|
func (obj *NoopRes) Validate() error {
|
||||||
return nil
|
return obj.BaseRes.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init runs some startup code for this resource.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -85,7 +85,11 @@ func (obj *NspawnRes) Validate() error {
|
|||||||
if _, exists := validStates[obj.State]; !exists {
|
if _, exists := validStates[obj.State]; !exists {
|
||||||
return fmt.Errorf("Invalid State: %s", obj.State)
|
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.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func (obj *PasswordRes) Default() Res {
|
|||||||
|
|
||||||
// Validate if the params passed in are valid data.
|
// Validate if the params passed in are valid data.
|
||||||
func (obj *PasswordRes) Validate() error {
|
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
|
// Init generates a new password for this resource if one was not provided. It
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func (obj *PkgRes) Validate() error {
|
|||||||
return fmt.Errorf("State cannot be empty!")
|
return fmt.Errorf("State cannot be empty!")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return obj.BaseRes.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init runs some startup code for this resource.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -242,6 +242,11 @@ func (obj *BaseUID) Reversed() bool {
|
|||||||
return *obj.reversed
|
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.
|
// Init initializes structures like channels if created without New constructor.
|
||||||
func (obj *BaseRes) Init() error {
|
func (obj *BaseRes) Init() error {
|
||||||
if obj.kind == "" {
|
if obj.kind == "" {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func (obj *SvcRes) Validate() error {
|
|||||||
if obj.Startup != "enabled" && obj.Startup != "disabled" && obj.Startup != "" {
|
if obj.Startup != "enabled" && obj.Startup != "disabled" && obj.Startup != "" {
|
||||||
return fmt.Errorf("Startup must be either `enabled` or `disabled` or undefined.")
|
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.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (obj *TimerRes) Default() Res {
|
|||||||
|
|
||||||
// Validate the params that are passed to TimerRes.
|
// Validate the params that are passed to TimerRes.
|
||||||
func (obj *TimerRes) Validate() error {
|
func (obj *TimerRes) Validate() error {
|
||||||
return nil
|
return obj.BaseRes.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init runs some startup code for this resource.
|
// Init runs some startup code for this resource.
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ func (obj *VirtRes) Init() error {
|
|||||||
|
|
||||||
// Validate if the params passed in are valid data.
|
// Validate if the params passed in are valid data.
|
||||||
func (obj *VirtRes) Validate() error {
|
func (obj *VirtRes) Validate() error {
|
||||||
return nil
|
return obj.BaseRes.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (obj *VirtRes) connect() (conn *libvirt.Connect, err error) {
|
func (obj *VirtRes) connect() (conn *libvirt.Connect, err error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user