|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// The functions below are the CRUD function types for a Resource. |
| 9 | +type CreateFunc func(*ResourceData) error |
| 10 | +type ReadFunc func(*ResourceData) error |
| 11 | +type UpdateFunc func(*ResourceData) error |
| 12 | +type DeleteFunc func(*ResourceData) error |
| 13 | + |
| 14 | +// Resource represents a thing in Terraform that has a set of configurable |
| 15 | +// attributes and generally also has a lifecycle (create, read, update, |
| 16 | +// delete). |
| 17 | +// |
| 18 | +// The Resource schema is an abstraction that allows provider writers to |
| 19 | +// worry only about CRUD operations while off-loading validation, diff |
| 20 | +// generation, etc. to this higher level library. |
| 21 | +type Resource struct { |
| 22 | + Schema map[string]*Schema |
| 23 | + |
| 24 | + Create CreateFunc |
| 25 | + Read ReadFunc |
| 26 | + Update UpdateFunc |
| 27 | + Delete DeleteFunc |
| 28 | +} |
| 29 | + |
| 30 | +// InternalValidate should be called to validate the structure |
| 31 | +// of the resource. |
| 32 | +// |
| 33 | +// This should be called in a unit test for any resource to verify |
| 34 | +// before release that a resource is properly configured for use with |
| 35 | +// this library. |
| 36 | +func (r *Resource) InternalValidate() error { |
| 37 | + if r == nil { |
| 38 | + return errors.New("resource is nil") |
| 39 | + } |
| 40 | + |
| 41 | + for k, v := range r.Schema { |
| 42 | + if v.Type == TypeInvalid { |
| 43 | + return fmt.Errorf("%s: Type must be specified", k) |
| 44 | + } |
| 45 | + |
| 46 | + if v.Optional && v.Required { |
| 47 | + return fmt.Errorf("%s: Optional or Required must be set, not both", k) |
| 48 | + } |
| 49 | + |
| 50 | + if v.Required && v.Computed { |
| 51 | + return fmt.Errorf("%s: Cannot be both Required and Computed", k) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
0 commit comments